// DrawListItem for ReorderableList protected void DrawListItem(EditorReorderableList <UnityEngine.Object> list, UnityEngine.Object item) { UnityEngine.Object unityItem = item; if (item is ShelfSceneReference) { unityItem = (item as ShelfSceneReference).Resolve(); } var originalColor = GUI.color; GUI.color = (unityItem != null ? originalColor : new Color(1.0f, 1.0f, 1.0f, 0.5f)); var originalBackground = GUI.backgroundColor; if (list.IsSelected(item)) { GUI.backgroundColor = new Color(0.8f, 0.8f, 0.8f, 1f); } var mainRect = EditorGUILayout.BeginHorizontal(GUI.skin.box); { GUILayout.Label(ListItemContent(list, item), GUILayout.ExpandWidth(true), GUILayout.Height(SHELF_ITEM_HEIGHT)); // Add component to game object for mono scripts if (item is MonoScript && Selection.activeGameObject != null && GUILayout.Button("a", "label", GUILayout.Width(10))) { var comp = Selection.activeGameObject.AddComponent((item as MonoScript).GetClass()); // RegisterCreatedObjectUndo only works for non-persistent objects (?!) if (!EditorUtility.IsPersistent(comp)) { Undo.RegisterCreatedObjectUndo(comp, "Add Component"); } // Instantiate prefabs } else if (item is GameObject && EditorUtility.IsPersistent(item) && GUILayout.Button("n", GUILayout.Width(20))) { TextEditor textEditor = new TextEditor(); textEditor.text = item.name; textEditor.OnFocus(); textEditor.Copy(); } if (GUILayout.Button("x", GUILayout.Width(20))) { list.HandleListItemRemove(item); } } EditorGUILayout.EndHorizontal(); GUI.color = originalColor; GUI.backgroundColor = originalBackground; list.HandleListItemClick(mainRect, item); }
// ListItemContent for ReorderableList protected GUIContent ListItemContent(EditorReorderableList <UnityEngine.Object> list, UnityEngine.Object item) { var path = AssetDatabase.GetAssetPath(item); // ShelfSceneReference instances if (item is ShelfSceneReference) { var sceneRef = (item as ShelfSceneReference); var reference = sceneRef.Resolve(); // Handle references that are currently out of scope Type type = (reference != null ? reference.GetType() : typeof(UnityEngine.Object)); var content = new GUIContent(EditorGUIUtility.ObjectContent(null, type)); if (reference == null) { content.text = sceneRef.name; content.tooltip = sceneRef.scenePath + "/" + sceneRef.componentName; } else { content.text = reference.name; } return(content); // Project Assets } else if (!string.IsNullOrEmpty(path)) { return(new GUIContent( Path.GetFileNameWithoutExtension(path), AssetDatabase.GetCachedIcon(path) )); // Generic references } else { // Workaround for ObjectContent not returning icon for game objects // passed as first argument. Works if only the type is passed. var type = item.GetType(); var content = new GUIContent(EditorGUIUtility.ObjectContent(null, type)); if (item is GameObject) { content.text = item.name; } else { content.text = string.Format("{0} ({1})", item.name, type.Name); } return(content); } }
// Default delegate for drawing list items protected static void DrawListItem(EditorReorderableList <T> list, T item) { var originalBackground = GUI.backgroundColor; if (list.Selection.Contains(item)) { GUI.backgroundColor = new Color(0.8f, 0.8f, 0.8f, 1f); } EditorGUILayout.BeginHorizontal(GUI.skin.box); GUILayout.Label(list.listItemContentCallback(list, item), GUILayout.ExpandWidth(true)); if (GUILayout.Button("x", GUIStyle.none, GUILayout.Width(10))) { list.HandleListItemRemove(item); } EditorGUILayout.EndHorizontal(); GUI.backgroundColor = originalBackground; list.HandleListItemClick(GUILayoutUtility.GetLastRect(), item); }
// Default delegate for drawing the empty list item protected static void DrawEmptyListItem(EditorReorderableList <T> list) { // Initialize style if (emptyBoxStyle == null) { emptyBoxStyle = new GUIStyle(GUI.skin.box); emptyBoxStyle.normal.background = null; } // Draw empty box EditorGUILayout.BeginVertical(emptyBoxStyle); if (float.IsNaN(list.emptyListItemHeight)) { GUILayout.Label(""); } else { GUILayout.Space(list.emptyListItemHeight); } EditorGUILayout.EndVertical(); }
// ListItemClick for ReorderableList protected void ListItemClick(EditorReorderableList <UnityEngine.Object> list, UnityEngine.Object item) { if (item is ShelfSceneReference) { var reference = (item as ShelfSceneReference).Resolve(); Selection.activeObject = reference; } else { var path = AssetDatabase.GetAssetPath(item); var isDirectory = false; if (!string.IsNullOrEmpty(path)) { path = Application.dataPath + "/../" + path; isDirectory = Directory.Exists(path); } if (isDirectory) { // Try to show the folder's contents in Unity 4's two-column // project view instead of the parent folder's contents. bool success = false; var objectBrowserType = typeof(EditorWindow).Assembly.GetType("UnityEditor.ProjectBrowser"); if (objectBrowserType != null) { //var objectBrowser = EditorWindow.GetWindow(objectBrowserType); var browsers = Resources.FindObjectsOfTypeAll(objectBrowserType); if (browsers.Length == 0) { Debug.LogError("Shelf: Project browser not open, please open it to jump to folders."); } else { var field = objectBrowserType.GetField("m_ViewMode", bindingFlags); if (field != null) { var viewMode = (int)field.GetValue(browsers[0]); if (viewMode == 1) { var method = objectBrowserType.GetMethod("ShowFolderContents", bindingFlags); if (method != null) { (browsers[0] as EditorWindow).Focus(); method.Invoke(browsers[0], new object[] { item.GetInstanceID(), true }); success = true; } } } } } // Fall back to just pinging the folder if (!success) { EditorGUIUtility.PingObject(item); } } else if (path.EndsWith(".unity")) { var goahead = true; goahead = EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo(); if (goahead) { OpenSceneDelayed(AssetDatabase.GetAssetPath(item)); } } else { Selection.activeObject = item; } } }
// ListItemName for ReorderableList protected string ListItemName(EditorReorderableList <UnityEngine.Object> list, UnityEngine.Object item) { return(ListItemContent(list, item).text); }
// Preferences DrawListItem for ReorderableList protected static void PreferencesDrawListItem(EditorReorderableList <ShelfLayer> list, ShelfLayer item) { EditorGUILayout.BeginHorizontal(GUI.skin.box); { // Stop editing an items name if enter or return is pressed if (editItemName == item && Event.current.type == EventType.KeyDown && (Event.current.keyCode == KeyCode.Return || Event.current.keyCode == KeyCode.KeypadEnter)) { editItemName = null; Event.current.Use(); } // Layer name if (editItemName != item) { GUILayout.Label(item.name, GUILayout.ExpandWidth(true)); } else { var newName = GUILayout.TextField(item.name, GUILayout.ExpandWidth(true)); if (newName != item.name) { Undo.RecordObject(Data, "Rename Shelf Layer Name"); item.name = newName; EditorUtility.SetDirty(Data); } } // Rename button var content = new GUIContent("...", "Rename Layer"); if (GUILayout.Button(content, "label", GUILayout.Width(15))) { if (editItemName == item) { editItemName = null; } else { editItemName = item; } } // Delete button content = new GUIContent("x", "Delete Layer"); var delete = GUILayout.Button(content, "label", GUILayout.Width(10)); if (delete && item.objects != null && item.objects.Count > 0) { delete = EditorUtility.DisplayDialog( "Shelf", "Are you shure you want to delete '" + item.name + "'?", "Delete", "Cancel" ); } if (delete) { list.RemoveItem(item); } } EditorGUILayout.EndHorizontal(); }
// Preferences ListItemName for ReorderableList protected static string PreferencesListItemName(EditorReorderableList <ShelfLayer> list, ShelfLayer item) { return(item.name); }
// Preferences ReorderableList.objectReferenceCallback protected static UnityEngine.Object[] PreferencesLayersReferences(EditorReorderableList <ShelfLayer> list, ShelfLayer[] layers) { // Use the editor itself as stand-in for proper drag events return(new UnityEngine.Object[] { Data }); }
protected static void ShelfPreferences() { #if ENABLE_HACKS && PREFERENCES_WINDOW_REDRAW_HACK // HACK: Get private PreferencesWindow to type to get a // reference of the preferences window to trigger its redraw if (!inlinePrefs && preferencesWindow == null) { var prefWindowType = typeof(EditorApplication).Assembly.GetType("UnityEditor.PreferencesWindow"); if (prefWindowType != null) { preferencesWindow = EditorWindow.GetWindow(prefWindowType); } } #endif if (Data == null) { return; } // Initialize reorderable list used to edit shelf layers if (preferencesList == null) { preferencesList = new EditorReorderableList <ShelfLayer>(); preferencesList.allowMultiSelection = false; preferencesList.undoTarget = Data; preferencesList.objectReferencesCallback = PreferencesLayersReferences; preferencesList.listItemNameCallback = PreferencesListItemName; preferencesList.listItemDrawCallback = PreferencesDrawListItem; preferencesList.List = Data.layers; } EditorGUILayout.LabelField("Drag layers to reorder:"); if (preferencesList.OnGUI()) { #if ENABLE_HACKS && PREFERENCES_WINDOW_REDRAW_HACK if (!inlinePrefs && preferencesWindow != null) { preferencesWindow.Repaint(); } #endif if (editorInstance != null) { editorInstance.Repaint(); } } // Make sure the selected shelf index stays valid if (Data.currentLayer >= Data.layers.Count) { Data.currentLayer = Data.layers.Count - 1; } EditorGUILayout.Space(); // Add new layer if (GUILayout.Button("Add Layer")) { Data.layers.Add(new ShelfLayer() { name = "New Layer", objects = new List <UnityEngine.Object>() }); EditorUtility.SetDirty(Data); if (editorInstance != null) { editorInstance.Repaint(); } } EditorGUILayout.Space(); var enablePopup = EditorPrefs.GetBool(POPUP_PREFS_KEY, true); enablePopup = EditorGUILayout.Toggle("Enable Popup Shelf", enablePopup); EditorPrefs.SetBool(POPUP_PREFS_KEY, enablePopup); }
// Default delegate to remove list items protected static void ListItemRemove(EditorReorderableList <T> list, int index) { list.RegisterUndo("Remove List Item"); list.List.RemoveAt(index); }
// Get the GUIContent used for the list items protected static GUIContent ListItemContent(EditorReorderableList <T> list, T item) { return(new GUIContent(list.listItemNameCallback(list, item))); }
// Get the name used for the list item protected static string ListItemName(EditorReorderableList <T> list, T item) { return(item.ToString()); }