Editor
NGUISelectionTools.cs
//----------------------------------------------
// NGUI: Next-Gen UI kit
// Copyright 2011-2013 Tasharen Entertainment
//----------------------------------------------
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
public clast NGUISelectionTools
{
[MenuItem("GameObject/Selection/Force Delete")]
static void ForceDelete()
{
GameObject go = Selection.activeGameObject;
if (go != null)
{
go.hideFlags = HideFlags.DontSave;
if (Application.isPlaying)
{
GameObject.Destroy(go);
}
else
{
GameObject.DestroyImmediate(go);
}
}
}
[MenuItem("GameObject/Selection/Toggle 'Active' #&a")]
static void ActivateDeactivate()
{
if (HasValidTransform())
{
GameObject[] gos = Selection.gameObjects;
bool val = !NGUITools.GetActive(Selection.activeGameObject);
foreach (GameObject go in gos) NGUITools.SetActive(go, val);
}
}
[MenuItem("GameObject/Selection/Clear Local Transform")]
static void ClearLocalTransform()
{
if (HasValidTransform())
{
Transform t = Selection.activeTransform;
NGUIEditorTools.RegisterUndo("Clear Local Transform", t);
t.localPosition = Vector3.zero;
t.localRotation = Quaternion.idensaty;
t.localScale = Vector3.one;
}
}
[MenuItem("GameObject/Selection/Add New Child #&n")]
static void CreateLocalGameObject ()
{
if (PrefabCheck())
{
// Make this action undoable
NGUIEditorTools.RegisterUndo("Add New Child");
// Create our new GameObject
GameObject newGameObject = new GameObject();
newGameObject.name = "GameObject";
// If there is a selected object in the scene then make the new object its child.
if (Selection.activeTransform != null)
{
newGameObject.transform.parent = Selection.activeTransform;
newGameObject.name = "Child";
// Place the new GameObject at the same position as the parent.
newGameObject.transform.localPosition = Vector3.zero;
newGameObject.transform.localRotation = Quaternion.idensaty;
newGameObject.transform.localScale = new Vector3(1f, 1f, 1f);
newGameObject.layer = Selection.activeGameObject.layer;
}
// Select our newly created GameObject
Selection.activeGameObject = newGameObject;
}
}
[MenuItem("GameObject/Selection/List Dependencies")]
static void ListDependencies()
{
if (HasValidSelection())
{
Debug.Log("Selection depends on the following astets:\n\n" + GetDependencyText(Selection.objects, false));
}
}
//========================================================================================================
#region Helper Functions
clast astetEntry
{
public string path;
public List types = new List();
}
///
/// Helper function that checks to see if there are objects selected.
///
static bool HasValidSelection()
{
if (Selection.objects == null || Selection.objects.Length == 0)
{
Debug.LogWarning("You must select an object first");
return false;
}
return true;
}
///
/// Helper function that checks to see if there is an object with a Transform component selected.
///
static bool HasValidTransform()
{
if (Selection.activeTransform == null)
{
Debug.LogWarning("You must select an object first");
return false;
}
return true;
}
///
/// Helper function that checks to see if a prefab is currently selected.
///
static bool PrefabCheck()
{
if (Selection.activeTransform != null)
{
// Check if the selected object is a prefab instance and display a warning
#if UNITY_3_4
PrefabType type = EditorUtility.GetPrefabType(Selection.activeGameObject);
#else
PrefabType type = PrefabUtility.GetPrefabType(Selection.activeGameObject);
#endif
if (type == PrefabType.PrefabInstance)
{
return EditorUtility.DisplayDialog("Losing prefab",
"This action will lose the prefab connection. Are you sure you wish to continue?",
"Continue", "Cancel");
}
}
return true;
}
///
/// Function that collects a list of file dependencies from the specified list of objects.
///
static List GetDependencyList (Object[] objects, bool reverse)
{
Object[] deps = reverse ? EditorUtility.CollectDeepHierarchy(objects) : EditorUtility.CollectDependencies(objects);
List list = new List();
foreach (Object obj in deps)
{
string path = astetDatabase.GetastetPath(obj);
if (!string.IsNullOrEmpty(path))
{
bool found = false;
System.Type type = obj.GetType();
foreach (astetEntry ent in list)
{
if (ent.path.Equals(path))
{
if (!ent.types.Contains(type)) ent.types.Add(type);
found = true;
break;
}
}
if (!found)
{
astetEntry ent = new astetEntry();
ent.path = path;
ent.types.Add(type);
list.Add(ent);
}
}
}
deps = null;
objects = null;
return list;
}
///
/// Helper function that removes the Unity clast prefix from the specified string.
///
static string RemovePrefix (string text)
{
text = text.Replace("UnityEngine.", "");
text = text.Replace("UnityEditor.", "");
return text;
}
///
/// Helper function that gets the dependencies of specified objects and returns them in text format.
///
static string GetDependencyText (Object[] objects, bool reverse)
{
List dependencies = GetDependencyList(objects, reverse);
List list = new List();
string text = "";
foreach (astetEntry ae in dependencies)
{
text = ae.path.Replace("astets/", "");
if (ae.types.Count > 1)
{
text += " (" + RemovePrefix(ae.types[0].ToString());
for (int i = 1; i < ae.types.Count; ++i)
{
text += ", " + RemovePrefix(ae.types[i].ToString());
}
text += ")";
}
list.Add(text);
}
list.Sort();
text = "";
foreach (string s in list) text += s + "\n";
list.Clear();
list = null;
dependencies.Clear();
dependencies = null;
return text;
}
#endregion
}