public void DrawElement(Event e, Object selectedObject, int index, out List <Object> newSelection, out bool dropAccepted) { dropAccepted = false; EditorGUILayout.BeginVertical(); Texture2D texture = null; if (selectedObject != null) { texture = AssetPreview.GetAssetPreview(selectedObject); if (texture == null) { if (AssetPreview.IsLoadingAssetPreview(selectedObject.GetInstanceID())) { // Not loaded yet, so tell it to repaint Repaint(); } else { //texture = AssetPreview.GetMiniTypeThumbnail(selectedObject.GetType()); texture = AssetPreview.GetMiniThumbnail(selectedObject); } } } Rect previewRect; Rect insertAfterRect; if (UseCells) { GUIStyle style = new GUIStyle(GUI.skin.box); style.padding = new RectOffset(0, 0, 0, 0); style.alignment = TextAnchor.MiddleCenter; if (texture != null) { GUILayout.Box(texture, style, GUILayout.Width(width), GUILayout.Height(width)); } else { GUILayout.Box("Drag an object here", style, GUILayout.Width(width), GUILayout.Height(width)); } previewRect = GUILayoutUtility.GetLastRect(); insertAfterRect = new Rect(previewRect.xMax, previewRect.y, 8, previewRect.height); selectedObject = EditorGUILayout.ObjectField(selectedObject, TypeFilter, false, GUILayout.Width(width)); } else { selectedObject = EditorGUILayout.ObjectField(selectedObject, TypeFilter, false); previewRect = GUILayoutUtility.GetLastRect(); insertAfterRect = new Rect(previewRect.xMin, previewRect.yMax, previewRect.width, 8); } if (dropInTargetIndex == index) { if (dropInType == DropInType.InsertAfter) { DrawOutline(insertAfterRect, 2, Color.blue); } else { DrawOutline(previewRect, 2, Color.blue); } } bool mouseInRect = previewRect.Contains(e.mousePosition); bool mouseInInsertAfterRect = insertAfterRect.Contains(e.mousePosition); if (mouseInRect && e.type == EventType.MouseDown) { mouseDownIndex = index; } if (mouseInRect && e.type == EventType.MouseUp && !isDragging) { if (e.button == 0) { OnItemClick(selectedObject); } else { if (selectedObject == null) { deferredIndexToRemove = index; } else { selectedObject = null; } Repaint(); } } if (mouseInRect && e.type == EventType.MouseDown && !isDragging) { if (e.button == 0) { if (e.clickCount == 2 && selectedObject != null) { if (selectedObject.GetType() == typeof(SceneAsset)) { if (EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo()) { EditorSceneManager.OpenScene(AssetDatabase.GetAssetPath(selectedObject), OpenSceneMode.Single); } } else if (typeof(RuntimeAnimatorController).IsAssignableFrom(selectedObject.GetType())) { SetAnimatorWindow(selectedObject); } else if (selectedObject.GetType() == typeof(DefaultAsset)) { // Something without a strong Unity type, could be a folder if (ProjectWindowUtil.IsFolder(selectedObject.GetInstanceID())) { SetProjectWindowFolder(AssetDatabase.GetAssetPath(selectedObject)); } } else { AssetDatabase.OpenAsset(selectedObject); } } } } if (e.type == EventType.DragUpdated || e.type == EventType.DragPerform) { if (DragAndDrop.objectReferences.Length > 0 && (DragAndDrop.objectReferences[0].GetType() == TypeFilter || DragAndDrop.objectReferences[0].GetType().IsSubclassOf(TypeFilter))) { if (mouseInRect || mouseInInsertAfterRect) { DragAndDrop.visualMode = DragAndDropVisualMode.Copy; if (e.type == EventType.DragPerform) { DragAndDrop.AcceptDrag(); selectedObject = DragAndDrop.objectReferences[0]; dropAccepted = true; } else { dropInTargetIndex = index; dropInType = mouseInInsertAfterRect ? DropInType.InsertAfter : DropInType.Replace; } Repaint(); } } } EditorGUILayout.EndVertical(); newSelection = new List <Object> { selectedObject }; }
void OnGUI() { Event e = Event.current; //Debug.Log(e.type); #if UNITY_5_4_OR_NEWER int columnCount = (int)(Screen.width / EditorGUIUtility.pixelsPerPoint) / (width + SPACING); #else int columnCount = Screen.width / (width + SPACING); #endif List <Object> selectedObjects = tabs[activeTab].selectedObjects; scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition); GUILayout.Space(8); //GUILayout.Box(new GUIContent(), GUILayout.ExpandWidth(true)); // Make sure there's an empty one at the end to drag into if (selectedObjects.Count == 0 || selectedObjects[selectedObjects.Count - 1] != null) { selectedObjects.Add(null); } if (e.rawType == EventType.MouseUp || e.rawType == EventType.MouseMove || e.rawType == EventType.DragUpdated || e.rawType == EventType.ScrollWheel) { dropInTargetIndex = -1; dropInType = DropInType.Replace; } List <Object> deferredInsertObjects = null; int deferredInsertIndex = -1; for (int i = 0; i < selectedObjects.Count; i++) { int columnIndex = i % columnCount; if (UseCells && columnIndex == 0) { EditorGUILayout.BeginHorizontal(); GUILayout.Space(8); } List <Object> newSelectedObjects = null; bool dropAccepted = false; DrawElement(e, selectedObjects[i], i, out newSelectedObjects, out dropAccepted); if (dropAccepted || newSelectedObjects.Count > 1 || newSelectedObjects[0] != selectedObjects[i]) { if (dropInType == DropInType.InsertAfter) { // Defer the insert until after we've drawn the UI so we don't mismatch UI mid-draw deferredInsertIndex = i + 1; deferredInsertObjects = newSelectedObjects; } else { selectedObjects[i] = newSelectedObjects[0]; if (newSelectedObjects.Count > 1) { deferredInsertIndex = i + 1; newSelectedObjects.RemoveAt(0); deferredInsertObjects = newSelectedObjects; } Save(activeTab); } } if (UseCells) { GUILayout.Space(4); } else { GUILayout.Space(8); } if (UseCells && (columnIndex == columnCount - 1 || i == selectedObjects.Count - 1)) // If last in row { GUILayout.FlexibleSpace(); EditorGUILayout.EndHorizontal(); } } if (e.type == EventType.MouseDrag && !isDragging && mouseDownIndex != -1 && selectedObjects[mouseDownIndex] != null) { isDragging = true; DragAndDrop.PrepareStartDrag(); DragAndDrop.objectReferences = new Object[] { selectedObjects[mouseDownIndex] }; // DragAndDrop.paths = new string[] { selectedObjects[mouseDownIndex] }; DragAndDrop.StartDrag(selectedObjects[mouseDownIndex].name); } if (e.rawType == EventType.MouseUp || e.rawType == EventType.MouseMove || e.rawType == EventType.DragPerform || e.rawType == EventType.DragExited) { isDragging = false; mouseDownIndex = -1; } EditorGUILayout.EndScrollView(); GUILayout.FlexibleSpace(); GUIStyle boxStyle = new GUIStyle(GUI.skin.box); boxStyle.margin = new RectOffset(0, 0, 0, 0); RectOffset padding = boxStyle.padding; padding.top += 1; boxStyle.padding = padding; GUILayout.Box(new GUIContent(), boxStyle, GUILayout.ExpandWidth(true)); Rect lastRect = GUILayoutUtility.GetLastRect(); Rect buttonRect = lastRect; buttonRect.y += 1; buttonRect.width = (buttonRect.width - 90) / 9f; GUIStyle activeStyle = EditorStyles.toolbarButton; buttonRect.height = activeStyle.CalcHeight(new GUIContent(), 20); for (int i = 0; i < 9; i++) { string tabName = (i + 1).ToString(); if (!string.IsNullOrEmpty(tabs[i].Name)) { tabName = tabs[i].Name; } bool oldValue = (activeTab == i); if (oldValue == true && editingTabName) { GUI.SetNextControlName("PaletteTabName"); tabs[activeTab].Name = GUI.TextField(buttonRect, tabs[activeTab].Name); if (!hotControl.HasValue) { hotControl = GUIUtility.hotControl; GUI.FocusControl("PaletteTabName"); } else { if (GUIUtility.hotControl != hotControl.Value || // Clicked off it Event.current.type == EventType.KeyDown && Event.current.character == (char)10) // Return pressed { editingTabName = false; hotControl = null; } } } else { bool newValue = GUI.Toggle(buttonRect, oldValue, tabName, activeStyle); if (newValue != oldValue) { if (newValue == true) { activeTab = i; Repaint(); editingTabName = false; } else if (newValue == false) { editingTabName = true; hotControl = null; } } } buttonRect.x += buttonRect.width; } // Debug.Log(GUI.GetNameOfFocusedControl()); // if(GUI.GetNameOfFocusedControl() != "PaletteTabName") // { // editingTabName = false; // } Rect sliderRect = lastRect; sliderRect.xMax -= 10; sliderRect.xMin = sliderRect.xMax - 60; // User configurable tile size width = (int)GUI.HorizontalSlider(sliderRect, width, 49, 100); // Delete at the end of the OnGUI so we don't mismatch any UI groups if (deferredIndexToRemove != -1) { selectedObjects.RemoveAt(deferredIndexToRemove); deferredIndexToRemove = -1; Save(activeTab); } // Insert at the end of the OnGUI so we don't mismatch any UI groups if (deferredInsertObjects != null) { selectedObjects.InsertRange(deferredInsertIndex, deferredInsertObjects); Save(activeTab); } // Carried out a DragPerform, so reset drop in states if (e.rawType == EventType.DragPerform) { dropInTargetIndex = -1; dropInType = DropInType.Replace; Repaint(); } }
void OnGUI() { Event e = Event.current; //Debug.Log(e.type); #if UNITY_5_4_OR_NEWER int columnCount = (int)(Screen.width / EditorGUIUtility.pixelsPerPoint) / (width + SPACING); #else int columnCount = Screen.width / (width + SPACING); #endif scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition); GUILayout.Space(8); //GUILayout.Box(new GUIContent(), GUILayout.ExpandWidth(true)); // Make sure there's an empty one at the end to drag into if (selectedObjects.Count == 0 || selectedObjects[selectedObjects.Count - 1] != null) { selectedObjects.Add(null); } if (e.rawType == EventType.MouseUp || e.rawType == EventType.MouseMove || e.rawType == EventType.DragUpdated) { dropInTargetIndex = -1; dropInType = DropInType.Replace; } Object deferredInsertObject = null; int deferredInsertIndex = -1; for (int i = 0; i < selectedObjects.Count; i++) { int columnIndex = i % columnCount; if (columnIndex == 0) { EditorGUILayout.BeginHorizontal(); GUILayout.Space(8); } Object newSelectedObject = null; bool dropAccepted = false; DrawElement(e, selectedObjects[i], i, out newSelectedObject, out dropAccepted); if (dropAccepted || newSelectedObject != selectedObjects[i]) { if (dropInType == DropInType.InsertAfter) { // Defer the insert until after we've drawn the UI so we don't mismatch UI mid-draw deferredInsertIndex = i + 1; deferredInsertObject = newSelectedObject; } else if (newSelectedObject != selectedObjects[i]) { selectedObjects[i] = newSelectedObject; Save(); } } GUILayout.Space(4); if (columnIndex == columnCount - 1 || i == selectedObjects.Count - 1) // If last in row { GUILayout.FlexibleSpace(); EditorGUILayout.EndHorizontal(); } } if (e.type == EventType.MouseDrag && !isDragging && mouseDownIndex != -1 && selectedObjects[mouseDownIndex] != null) { isDragging = true; DragAndDrop.PrepareStartDrag(); DragAndDrop.objectReferences = new Object[] { selectedObjects[mouseDownIndex] }; // DragAndDrop.paths = new string[] { selectedObjects[mouseDownIndex] }; DragAndDrop.StartDrag(selectedObjects[mouseDownIndex].name); } if (e.rawType == EventType.MouseUp || e.rawType == EventType.MouseMove || e.rawType == EventType.DragPerform || e.rawType == EventType.DragExited) { isDragging = false; mouseDownIndex = -1; } EditorGUILayout.EndScrollView(); GUILayout.FlexibleSpace(); GUIStyle boxStyle = new GUIStyle(GUI.skin.box); boxStyle.margin = new RectOffset(0, 0, 0, 0); GUILayout.Box(new GUIContent(), boxStyle, GUILayout.ExpandWidth(true)); Rect lastRect = GUILayoutUtility.GetLastRect(); lastRect.xMax -= 10; lastRect.xMin = lastRect.xMax - 60; // User configurable tile size width = (int)GUI.HorizontalSlider(lastRect, width, 50, 100); // Delete at the end of the OnGUI so we don't mismatch any UI groups if (deferredIndexToRemove != -1) { selectedObjects.RemoveAt(deferredIndexToRemove); deferredIndexToRemove = -1; Save(); } // Insert at the end of the OnGUI so we don't mismatch any UI groups if (deferredInsertObject != null) { selectedObjects.Insert(deferredInsertIndex, deferredInsertObject); Save(); } // Carried out a DragPerform, so reset drop in states if (e.rawType == EventType.DragPerform) { dropInTargetIndex = -1; dropInType = DropInType.Replace; } }