//----------------------------------------------
// NGUI: Next-Gen UI kit
// Copyright © 2011-2015 Tasharen Entertainment
//----------------------------------------------
using UnityEditor;
using UnityEngine;
using System.Collections.Generic;
///
/// EditorGUILayout.ObjectField doesn't support custom components, so a custom wizard saves the day.
/// Unfortunately this tool only shows components that are being used by the scene, so it's a "recently used" selection tool.
///
public class ComponentSelector : ScriptableWizard
{
public delegate void OnSelectionCallback (Object obj);
System.Type mType;
string mTitle;
OnSelectionCallback mCallback;
Object[] mObjects;
bool mSearched = false;
Vector2 mScroll = Vector2.zero;
string[] mExtensions = null;
static string GetName (System.Type t)
{
string s = t.ToString();
s = s.Replace("UnityEngine.", "");
if (s.StartsWith("UI")) s = s.Substring(2);
return s;
}
///
/// Draw a button + object selection combo filtering specified types.
///
static public void Draw (string buttonName, T obj, OnSelectionCallback cb, bool editButton, params GUILayoutOption[] options) where T : Object
{
GUILayout.BeginHorizontal();
bool show = NGUIEditorTools.DrawPrefixButton(buttonName);
T o = EditorGUILayout.ObjectField(obj, typeof(T), false, options) as T;
if (editButton && o != null && o is MonoBehaviour)
{
Component mb = o as Component;
if (Selection.activeObject != mb.gameObject && GUILayout.Button("Edit", GUILayout.Width(40f)))
Selection.activeObject = mb.gameObject;
}
else if (o != null && GUILayout.Button("X", GUILayout.Width(20f)))
{
o = null;
}
GUILayout.EndHorizontal();
if (show) Show(cb);
else cb(o);
}
///
/// Draw a button + object selection combo filtering specified types.
///
static public void Draw (T obj, OnSelectionCallback cb, bool editButton, params GUILayoutOption[] options) where T : Object
{
Draw(NGUITools.GetTypeName(), obj, cb, editButton, options);
}
///
/// Show the selection wizard.
///
static public void Show (OnSelectionCallback cb) where T : Object { Show(cb, new string[] {".prefab"}); }
///
/// Show the selection wizard.
///
static public void Show (OnSelectionCallback cb, string[] extensions) where T : Object
{
System.Type type = typeof(T);
string title = (type == typeof(UIAtlas) ? "Select an " : "Select a ") + GetName(type);
ComponentSelector comp = ScriptableWizard.DisplayWizard(title);
comp.mTitle = title;
comp.mType = type;
comp.mCallback = cb;
comp.mExtensions = extensions;
comp.mObjects = Resources.FindObjectsOfTypeAll(typeof(T));
if (comp.mObjects == null || comp.mObjects.Length == 0)
{
comp.Search();
}
else
{
// Remove invalid fonts (Lucida Grande etc)
if (typeof(T) == typeof(Font))
{
for (int i = 0; i < comp.mObjects.Length; ++i)
{
Object obj = comp.mObjects[i];
if (obj.name == "Arial") continue;
string path = AssetDatabase.GetAssetPath(obj);
if (string.IsNullOrEmpty(path)) comp.mObjects[i] = null;
}
}
System.Array.Sort(comp.mObjects,
delegate(Object a, Object b)
{
if (a == null) return (b == null) ? 0 : 1;
if (b == null) return -1;
return a.name.CompareTo(b.name);
});
}
}
///
/// Search the entire project for required assets.
///
void Search ()
{
mSearched = true;
if (mExtensions != null)
{
string[] paths = AssetDatabase.GetAllAssetPaths();
bool isComponent = mType.IsSubclassOf(typeof(Component));
List