Created
March 27, 2018 16:33
-
-
Save rakkarage/868a39472848425346f99d19af5b226d to your computer and use it in GitHub Desktop.
simply batch add a bunch of ui images to scene from array of sprites
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEditor; | |
using UnityEngine; | |
using UnityEngine.UI; | |
namespace ca.HenrySoftware.Deko | |
{ | |
public class AdderEditor : EditorWindow | |
{ | |
public RectTransform Parent; | |
public Sprite[] Sprites; | |
[MenuItem("FreePixelFood/Adder")] | |
static public void Init() | |
{ | |
EditorWindow.GetWindow(typeof(AdderEditor)).Show(); | |
} | |
void OnGUI() | |
{ | |
Parent = EditorGUILayout.ObjectField("Parent", Parent, typeof(RectTransform), true) as RectTransform; | |
ScriptableObject target = this; | |
SerializedObject so = new SerializedObject(target); | |
SerializedProperty spritesProperty = so.FindProperty("Sprites"); | |
EditorGUILayout.PropertyField(spritesProperty, true); | |
if (GUILayout.Button("Create Images")) | |
{ | |
for (var i = 0; i < Sprites.Length; i++) | |
{ | |
var sprite = Sprites[i]; | |
var o = new GameObject(); | |
o.name = "Sprite" + i.ToString(); | |
Image image = o.AddComponent<Image>(); | |
image.sprite = sprite; | |
o.transform.SetParent(Parent); | |
o.transform.localScale = Vector3.one; | |
o.SetActive(true); | |
} | |
} | |
so.ApplyModifiedProperties(); | |
AssetDatabase.SaveAssets(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment