Created
September 7, 2011 13:43
-
-
Save mindlace/1200597 to your computer and use it in GitHub Desktop.
Class that lets you edit key/value pair thingies
This file contains hidden or 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
[System.Serializable] | |
public class PrefabsAndGhosts | |
{ | |
public static string unnamedPrefabName = "New Prefab"; | |
public bool ghostsExpanded = false; | |
public bool prefabsExpanded = false; | |
public const string UniqueIndicator = "-"; | |
public PrefabRef newPrefab = new PrefabRef(); | |
PrefabRef[] prefabs = new PrefabRef[] {}; | |
List<PrefabRef> deleteEntries = new List<PrefabRef>(); | |
public bool DrawPrefabsFoldout(Object parent, List<PrefabRef> prefabList) { | |
prefabsExpanded = EditorGUILayout.Foldout(prefabsExpanded, "Prefabs"); | |
if (prefabsExpanded) { | |
DrawPrefabs(parent, prefabList); | |
} | |
return prefabsExpanded; | |
} | |
public void DrawPrefabs(Object parent, List<PrefabRef> prefabList) { | |
// we need to avoid mutating the list between the layout and repaint event | |
// this is a quick hack to accomplish that: | |
// Todo: be more explicit about handling layout / Repaint events. | |
bool isDirty = false; | |
if (Event.current.type == EventType.layout) { | |
if (deleteEntries.Count > 0) { | |
Undo.RegisterUndo(EditorUtility.CollectDeepHierarchy(new Object[] {parent}), "Delete Prefab"); | |
foreach (PrefabRef pr in deleteEntries) { | |
prefabList.Remove(pr); | |
isDirty = true; | |
} | |
deleteEntries.Clear(); | |
} | |
prefabs = prefabList.ToArray(); | |
} | |
List<string> similarNames = new List<string>(); | |
string withFocus = string.Empty; | |
for (int i=0; i< prefabs.Length; i++) { | |
EditorGUILayout.BeginHorizontal(); | |
EditorGUIUtility.LookLikeInspector(); | |
GUILayout.Space(12); | |
PrefabRef pr = prefabs[i]; | |
// only for backward compatibility, delete soon | |
if (string.IsNullOrEmpty(pr.name)) { | |
pr.name = pr.prefab.name; | |
} | |
if (string.IsNullOrEmpty(pr.newName)) { | |
pr.newName = pr.name; | |
} | |
GUI.SetNextControlName(parent.name + "_prefabName"+i); | |
string changedName = EditorGUILayout.TextField(pr.newName); | |
if (changedName != pr.newName) { | |
Undo.RegisterUndo(EditorUtility.CollectDeepHierarchy(new Object[] {parent}), "Change Prefab Name"); | |
pr.newName = changedName; | |
} | |
withFocus = GUI.GetNameOfFocusedControl(); | |
if (withFocus != parent.name + "_prefabName"+i && pr.newName != pr.name) { | |
foreach (PrefabRef pre in prefabs) { | |
if (pre.name == pre.newName && pre.name.Contains(pr.newName)) similarNames.Add(pre.name); | |
} | |
string newName = pr.newName; | |
pr.newName = pr.name; | |
Undo.RegisterUndo(EditorUtility.CollectDeepHierarchy(new Object[] {parent}), "Change Prefab Name"); | |
pr.name = Util.MakeUnique(newName, UniqueIndicator, similarNames); | |
pr.newName = pr.name; | |
similarNames.Clear(); | |
} | |
GUI.SetNextControlName(parent.name + "_prefab"+i); | |
GameObject updatePrefab = (GameObject) EditorGUILayout.ObjectField(pr.prefab, typeof(GameObject), false); | |
withFocus = GUI.GetNameOfFocusedControl(); | |
if (updatePrefab != pr.prefab) { | |
Undo.RegisterUndo(EditorUtility.CollectDeepHierarchy(new Object[] {parent}), "Change Prefab Reference"); | |
if (pr.name.StartsWith(pr.prefab.name)) { | |
foreach (PrefabRef pre in prefabs) { | |
if (pre != pr && pre.name.Contains(updatePrefab.name)) similarNames.Add(pre.name); | |
} | |
pr.name = Util.MakeUnique(updatePrefab.name, UniqueIndicator, similarNames); | |
pr.newName = pr.name; | |
similarNames.Clear(); | |
} | |
pr.prefab = updatePrefab; | |
} | |
EditorGUIUtility.LookLikeControls(); | |
if (GUILayout.Button("-", GUILayout.Width(21))) { | |
deleteEntries.Add(pr); | |
} | |
EditorGUILayout.EndHorizontal(); | |
} | |
EditorGUILayout.BeginHorizontal(); | |
GUILayout.Space(12); | |
GUI.SetNextControlName(parent.name +"_newPrefabName"); | |
string nameToUse = (string.IsNullOrEmpty(newPrefab.name)) ? unnamedPrefabName : newPrefab.name; | |
string chosenPrefabName = EditorGUILayout.TextField(nameToUse); | |
GUI.SetNextControlName(parent.name +"_newPrefab"); | |
GameObject chosenPrefab = (GameObject) EditorGUILayout.ObjectField(newPrefab.prefab, typeof(GameObject), false); | |
withFocus = GUI.GetNameOfFocusedControl(); | |
if (withFocus != parent.name +"_newPrefabName" && chosenPrefab != null) { | |
if (chosenPrefabName == unnamedPrefabName || | |
( newPrefab.prefab != null | |
&& chosenPrefab != newPrefab.prefab | |
&& chosenPrefabName.Contains(newPrefab.name) | |
) | |
) { | |
chosenPrefabName = chosenPrefab.name; | |
} | |
} | |
foreach (PrefabRef pr in prefabs) { | |
if (pr.name.Contains(chosenPrefabName)) similarNames.Add(pr.name); | |
} | |
newPrefab.name = Util.MakeUnique(chosenPrefabName, UniqueIndicator, similarNames); | |
newPrefab.newName = newPrefab.name; | |
similarNames.Clear(); | |
if (newPrefab.prefab != chosenPrefab) { | |
newPrefab.prefab = chosenPrefab; | |
} | |
if (chosenPrefab == null) { | |
GUI.enabled = false; | |
} else { | |
GUI.enabled = true; | |
} | |
bool makeNew = GUILayout.Button("+", GUILayout.Width(21)); | |
GUI.enabled = true; | |
if (makeNew) { | |
Undo.RegisterUndo(EditorUtility.CollectDeepHierarchy(new Object[] {parent}), "Add Prefab"); | |
newPrefab.prefab = chosenPrefab; | |
prefabList.Add(newPrefab); | |
newPrefab = new PrefabRef(); | |
isDirty = true; | |
} | |
EditorGUILayout.EndHorizontal(); | |
if (isDirty) { | |
EditorUtility.SetDirty(parent); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment