Skip to content

Instantly share code, notes, and snippets.

@rakkarage
Created March 27, 2018 16:33
Show Gist options
  • Save rakkarage/868a39472848425346f99d19af5b226d to your computer and use it in GitHub Desktop.
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
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