void DisplayAddRemoveList <T> (ref T[] itemArray, string header) where T : UnityEngine.Object { EditorGUILayout.BeginVertical(EditorStyles.helpBox); EditorGUILayout.LabelField(header, EditorStyles.miniLabel); if (itemArray == null) { itemArray = new T[0]; } if (itemArray.Length == 0) { EditorGUILayout.LabelField("(No items)"); } List <T> itemList = new List <T> (itemArray); int indexToRemove = -1; bool showMatPreview = false; if (typeof(T) == typeof(Material)) { showMatPreview = true; } for (int i = 0; i < itemList.Count; i++) { if (showMatPreview) { EditorGUILayout.BeginHorizontal(); } if (GUILayout.Button(itemList [i].name)) { if (EditorUtility.DisplayDialog("Confirm Remove", "Remove " + itemList [i].name + "?", "Yes", "Cancel")) { indexToRemove = i; } } if (showMatPreview) { Material mat = itemList [i] as Material; if (showMatPreviews && mat != null) { Editor matEditor = MaterialEditor.CreateEditor(mat); matEditor.OnPreviewGUI(GUILayoutUtility.GetRect(25, 25), GUIStyle.none); GameObject.DestroyImmediate(matEditor); } EditorGUILayout.EndHorizontal(); } } if (indexToRemove >= 0) { itemList.RemoveAt(indexToRemove); } GUI.color = Color.white; T newGameObject = (T)EditorGUILayout.ObjectField("Add new:", null, typeof(T), true); if (newGameObject != null) { itemList.Add(newGameObject); } itemArray = itemList.ToArray(); EditorGUILayout.EndVertical(); }
void DisplayMatIndexes(ref int[] matIndexArray, Renderer[] renderers) { EditorGUILayout.BeginVertical(EditorStyles.helpBox); EditorGUILayout.LabelField("Target Material Indexes:", EditorStyles.miniLabel); if (renderers == null || renderers.Length == 0) { EditorGUILayout.LabelField("(No renderers)"); return; } if (matIndexArray == null) { matIndexArray = new int[0]; } if (matIndexArray.Length != renderers.Length) { Array.Resize <int> (ref matIndexArray, renderers.Length); } for (int i = 0; i < renderers.Length; i++) { Material[] sharedMats = renderers [i].sharedMaterials; string[] options = new string[sharedMats.Length]; for (int j = 0; j < sharedMats.Length; j++) { string option = "(NULL)"; if (sharedMats [j] != null) { option = sharedMats [j].name + "(" + j.ToString() + ")"; } options [j] = option; } EditorGUILayout.BeginHorizontal(); int newMatIndex = EditorGUILayout.Popup(renderers[i].name, matIndexArray [i], options); if (showMatPreviews && sharedMats [newMatIndex] != null) { Editor matEditor = MaterialEditor.CreateEditor(sharedMats[newMatIndex]); matEditor.OnPreviewGUI(GUILayoutUtility.GetRect(25, 25), GUIStyle.none); GameObject.DestroyImmediate(matEditor); } matIndexArray [i] = newMatIndex; EditorGUILayout.EndHorizontal(); } EditorGUILayout.EndVertical(); }
public void Fill(List <Object> objects, FillMode fillMode = FillMode.Default) { objects = new List <Object>(objects); if (fillMode == FillMode.Add) { components.RemoveAll(item => item == null); foreach (var component in components) { if (!objects.Contains(component)) { objects.Add(component); } } } Dispose(); components = objects; folouts.Clear(); foreach (var component in components) { Foldout folout = new Foldout(string.Format("{0}", component.GetType().Name)); folout.Value = components.Count == 1 ? true : false; folout.name = folout.Title; folouts.Add(folout); folout.imageElement.image = EditorGUIUtility.ObjectContent(component, component.GetType()).image; folout.headerElement.RegisterCallback <MouseUpEvent>((evt) => { if (evt.button == 1) { Rect rect = new Rect(folout.headerElement.layout); rect.position = evt.mousePosition; HierarchyEditor.DisplayObjectContextMenu(rect, component, 0); evt.StopPropagation(); } }); Label objectName = new Label(string.Format(" [{0}]", component.name)); objectName.StyleTextColor(objectNameColor); objectName.RegisterCallback <MouseUpEvent>((evt) => { if (evt.button == 0) { EditorGUIUtility.PingObject(component); Selection.activeObject = component; evt.StopPropagation(); } }); folout.headerElement.Add(objectName); Image remove = new Image(); remove.image = EditorGUIUtility.IconContent("winbtn_win_close").image; remove.StyleSize(13, 13); remove.StylePosition(Position.Absolute); remove.StyleRight(8); remove.StyleAlignSelf(Align.Center); remove.RegisterCallback <MouseUpEvent>((evt) => { if (evt.button == 0) { if (component != null) { components.Remove(component); } else { components.RemoveAll(item => item == null); } Fill(new List <Object>(components)); evt.StopPropagation(); } }); folout.headerElement.Add(remove); bool isMat = component is Material; Editor editor = null; if (isMat) { editor = MaterialEditor.CreateEditor(component) as MaterialEditor; } else { editor = Editor.CreateEditor(component); } VisualElement inspector = editor.CreateInspectorGUI(); if (inspector == null) { inspector = new IMGUIContainer(() => { bool tempState = EditorGUIUtility.wideMode; float tempWidth = EditorGUIUtility.labelWidth; EditorGUIUtility.wideMode = true; if (component is Transform) { EditorGUIUtility.labelWidth = 64; } if (editor.target != null) { if (isMat) { MaterialEditor maEditor = editor as MaterialEditor; EditorGUILayout.BeginVertical(); if (maEditor.PropertiesGUI()) { maEditor.PropertiesChanged(); } EditorGUILayout.EndVertical(); } else { editor.OnInspectorGUI(); } objectName.StyleTextColor(objectNameColor); } else { objectName.StyleTextColor(Color.red); EditorGUILayout.HelpBox("Reference not found.", MessageType.Info); } EditorGUIUtility.wideMode = tempState; EditorGUIUtility.labelWidth = tempWidth; }); } inspector.style.marginLeft = 16; inspector.style.marginRight = 2; inspector.style.marginTop = 4; folout.Add(inspector); editors.Add(editor); scrollView.Add(folout); if (isMat) { var preview = new IMGUIContainer(() => { editor.DrawPreview(new Rect(0, 0, inspector.layout.size.x, Mathf.Clamp(inspector.layout.width / 2, 64, 200))); }); preview.StyleMarginTop(4); preview.StretchToParentWidth(); inspector.RegisterCallback <GeometryChangedEvent>((callback) => { preview.StyleHeight(Mathf.Clamp(inspector.layout.width / 2, 64, 200)); }); preview.StylePosition(Position.Relative); preview.name = "Material Preview"; folout.Add(preview); } } Repaint(); }