public void HandleDragAndDropArea(Rect r) { Event e = Event.current; Rect dropArea = r; if (showDropArea) { GUI.Box(dropArea, string.Empty, EditorStyles.helpBox); GUI.Label(dropArea, "DROP IT LIKE IT'S HOT!", EditorStyles.centeredGreyMiniLabel); } switch (e.type) { case EventType.DragUpdated: case EventType.DragPerform: showDropArea = true; if (!dropArea.Contains(e.mousePosition)) { return; } DragAndDrop.visualMode = DragAndDropVisualMode.Link; if (e.type == EventType.DragPerform) { DragAndDrop.AcceptDrag(); showDropArea = false; foreach (Object dragged_object in DragAndDrop.objectReferences) { if (dragged_object is GameObject) { GameObject go = (GameObject)dragged_object; PaintObject po = new PaintObject(go); po.SetName(dragged_object.name); paintObjects.Add(po); listSize++; } } } break; case EventType.DragExited: showDropArea = false; break; } }
public void DropAreaGUI(Rect r) { var e = Event.current; var dropArea = r; GUI.Box(dropArea, string.Empty); GUI.Label(dropArea, "+", EditorStyles.centeredGreyMiniLabel); switch (e.type) { case EventType.DragUpdated: case EventType.DragPerform: if (!dropArea.Contains(e.mousePosition)) { return; } DragAndDrop.visualMode = DragAndDropVisualMode.Copy; if (e.type == EventType.DragPerform) { DragAndDrop.AcceptDrag(); foreach (Object dragged_object in DragAndDrop.objectReferences) { if (dragged_object is GameObject) { GameObject go = (GameObject)dragged_object; PaintObject po = new PaintObject(go); po.setName(dragged_object.name); paintObjects.Add(po); listSize++; } } } break; } }
void OnGUI() { EditorGUILayout.BeginHorizontal("Toolbar", GUILayout.ExpandWidth(true)); if (GUILayout.Button("Load", "ToolbarButton")) { string path = EditorUtility.OpenFilePanel("Select Palette", "", "asset"); path = path.Replace(Application.dataPath, "Assets"); if (path.Length != 0) { activePalette = (PaintPalette)AssetDatabase.LoadAssetAtPath(path, typeof(PaintPalette)); LoadPalette(activePalette); if (!palettes.Contains(activePalette)) { palettes.Add(activePalette); } } Debug.Log("<color=cyan>[Prefab Painter] </color>Palette loaded."); } if (activePalette != null) { GUILayout.Label(new GUIContent("Active: " + activePalette.name), "ToolbarButton"); } else { GUILayout.Label(new GUIContent("Active: none"), "ToolbarButton"); } GUILayout.Space(5f); GUILayout.FlexibleSpace(); if (GUILayout.Button(new GUIContent("", saveIcon, "Save active prefabs as palette."), "ToolbarButton")) { if (activePalette != null && palettes.Contains(activePalette)) { OverridePalette(activePalette); } else { CreateNewPalette(); } } if (GUILayout.Button(new GUIContent("", deleteIcon, "Remove currently loaded palette."), "ToolbarButton")) { Debug.Log("Remove Palette"); } if (GUILayout.Button(new GUIContent("Palettes", "Load in a palette."), "ToolbarPopup")) { GenericMenu menu = new GenericMenu(); if (palettes.Count > 0) { for (int i = 0; i < palettes.Count; i++) { AddMenuItemForPalette(menu, palettes[i].name, palettes[i]); } } menu.AddItem(new GUIContent("New Palette"), creatingNewPalette, OnNewPaletteSelected); menu.AddItem(new GUIContent("Clear List"), false, OnClearList); menu.ShowAsContext(); } EditorGUILayout.EndHorizontal(); EditorGUILayout.Space(); scrollPos = EditorGUILayout.BeginScrollView(scrollPos, false, false); float tempSize = brushSize; int tempDensity = brushDensity; BrushSettingsFold = BeginFold("Brush Settings", BrushSettingsFold); if (BrushSettingsFold) { paintMask = EditorGUILayout.MaskField( new GUIContent("Paint Layer", "On which layer the tool will paint."), paintMask, layerNames.ToArray()); brushSize = EditorGUILayout.FloatField("Brush Size", brushSize); brushDensity = EditorGUILayout.IntField("Brush Density", brushDensity); paintGroupName = EditorGUILayout.TextField("Paint Group Name", paintGroupName); } EndFold(); listSize = Mathf.Max(0, listSize); for (int i = 0; i < paintObjects.Count; i++) { PaintObject.Display(paintObjects[i]); } if (GUILayout.Button("Add Prefab")) { listSize++; } if (GUILayout.Button("Remove Prefab") && listSize != 0) { listSize--; } EditorGUILayout.Space(); CheckForChanges(tempSize, tempDensity); EditorGUILayout.EndScrollView(); }
public void DisplayPrefabs(List <PaintObject> prefabs) { int numberOfPrefabs = prefabs.Count; int windowWidth = (int)EditorGUIUtility.currentViewWidth; int y; if (selectedPrefab != null) { y = 215; } else { y = 110; } for (int i = 0; i < numberOfPrefabs; i++) { var e = Event.current; GameObject go = prefabs[i].GetGameObject(); int columns = Mathf.FloorToInt(windowWidth / (50 + 20) + 1); int rows = Mathf.FloorToInt(numberOfPrefabs / columns); prefabListHeight = rows * 50f; int posX = 5 + 50 * (i - (Mathf.FloorToInt(i / columns)) * columns); int posY = y + 50 * Mathf.FloorToInt(i / columns) + 10; Rect r = new Rect(posX, posY, 50, 50); Rect border = new Rect(r.x + 2, r.y + 6, r.width - 4, r.height - 4); if (r.Contains(Event.current.mousePosition) && e.type == EventType.MouseDown && e.button == 0) { selectedPrefab = prefabs[i]; Repaint(); } if (greyTexture != null) { if (prefabs[i] == selectedPrefab && selectedPrefab != null) { EditorGUI.DrawPreviewTexture(border, blueTexture, null, ScaleMode.ScaleToFit, 0f); } else { EditorGUI.DrawPreviewTexture(border, greyTexture, null, ScaleMode.ScaleToFit, 0f); } } border.x += 2; border.y += 2; border.width -= 4; border.height -= 4; Texture2D preview = AssetPreview.GetAssetPreview(go); if (preview != null) { EditorGUI.DrawPreviewTexture(border, preview, null, ScaleMode.ScaleToFit, 0f); } } int c = Mathf.FloorToInt(windowWidth / (50 + 20) + 1); int xRect = 5 + 50 * (numberOfPrefabs - (Mathf.FloorToInt(numberOfPrefabs / c)) * c); int yRect = y + 50 * Mathf.FloorToInt(numberOfPrefabs / c) + 10; DropAreaGUI(new Rect(xRect + 2, yRect + 6, 46, 46)); }
public static void Display(PaintObject obj) { if (obj == null) { return; } Texture2D background = new Texture2D(128, 128); for (int y = 0; y < 128; y++) { for (int x = 0; x < 128; x++) { background.SetPixel(x, y, Color.grey); } } background.Apply(); EditorGUILayout.BeginVertical(PrefabPainter.BoxStyle); GUILayout.Space(3); EditorGUI.BeginChangeCheck(); GameObject gameObject = obj.prefab; if (EditorGUI.EndChangeCheck()) { if (obj.gameObjectEditor != null) { Object.DestroyImmediate(obj.gameObjectEditor); } } GUIStyle bgColor = new GUIStyle(); bgColor.normal.background = background; if (gameObject != null) { if (obj.gameObjectEditor == null) { obj.gameObjectEditor = Editor.CreateEditor(gameObject); } obj.gameObjectEditor.OnInteractivePreviewGUI(GUILayoutUtility.GetRect(50, 50), bgColor); } EditorGUI.BeginChangeCheck(); gameObject = (GameObject)EditorGUILayout.ObjectField("", obj.prefab, typeof(GameObject), true); obj.prefab = gameObject; if (EditorGUI.EndChangeCheck()) { if (obj.gameObjectEditor != null) { Object.DestroyImmediate(obj.gameObjectEditor); } } if (obj.prefab != null) { obj.scale.x = EditorGUILayout.FloatField("Min Size", obj.scale.x); obj.scale.y = EditorGUILayout.FloatField("Max Size", obj.scale.y); GUILayout.Label("Random Rotation :"); EditorGUILayout.BeginHorizontal(); obj.randomRotationX = GUILayout.Toggle(obj.randomRotationX, "X"); obj.randomRotationY = GUILayout.Toggle(obj.randomRotationY, "Y"); obj.randomRotationZ = GUILayout.Toggle(obj.randomRotationZ, "Z"); EditorGUILayout.EndHorizontal(); } GUILayout.Space(3); EditorGUILayout.EndVertical(); GUILayout.Space(0); }
public void DisplayPrefabs(List <PaintObject> prefabs) { int numberOfPrefabs = prefabs.Count; int windowWidth = (int)EditorGUIUtility.currentViewWidth; int prefabBoxSize = 100; int y; if (selectedPrefab != null) { y = 215; } else { y = 110; } //Skip logo y += 120; int c = Mathf.FloorToInt(windowWidth / (prefabBoxSize + 20) + 1); Rect r2 = EditorGUILayout.GetControlRect(); int columns = Mathf.FloorToInt(windowWidth / (prefabBoxSize + 38) + 1); int rows = Mathf.FloorToInt(numberOfPrefabs / columns); prefabListHeight = rows * prefabBoxSize; for (int i = 0; i < numberOfPrefabs; i++) { var e = Event.current; GameObject go = prefabs[i].GetGameObject(); columns = Mathf.FloorToInt(windowWidth / (prefabBoxSize + 38) + 1); rows = Mathf.FloorToInt(numberOfPrefabs / columns); prefabListHeight = rows * prefabBoxSize; int posX = 5 + prefabBoxSize * (i - (Mathf.FloorToInt(i / columns)) * columns); int posY = y + prefabBoxSize * Mathf.FloorToInt(i / columns); Rect r = new Rect(posX, posY, prefabBoxSize, prefabBoxSize); Rect border = new Rect(r.x + 2, r.y + 6, r.width - 4, r.height - 4); if (e.type == EventType.MouseDown && e.button == 0) { if (r.Contains(Event.current.mousePosition)) { selectedPrefab = prefabs[i]; Repaint(); } } GUIStyle style = new GUIStyle(GUI.skin.label) { alignment = TextAnchor.MiddleCenter }; if (prefabs[i] == selectedPrefab && selectedPrefab != null) { EditorGUI.DrawPreviewTexture(border, blueTexture, null, ScaleMode.ScaleToFit, 0f); } else { EditorGUI.DrawPreviewTexture(border, greyTexture, null, ScaleMode.ScaleToFit, 0f); } EditorGUI.LabelField(border, "No Preview", style); border.x += 2; border.y += 2; border.width -= 4; border.height -= 4; Texture2D preview = AssetPreview.GetAssetPreview(go); if (preview != null) { EditorGUI.DrawPreviewTexture(border, preview, null, ScaleMode.ScaleToFit, 0f); } float closeButtonSize = 20; GUIStyle style1 = new GUIStyle(GUI.skin.button); style1.normal.textColor = Color.red; style1.alignment = TextAnchor.MiddleCenter; style1.contentOffset = new Vector2(0.5f, 0); border.x = r.x + (border.width - (closeButtonSize - 4)); border.width = closeButtonSize; border.height = closeButtonSize; if (GUI.Button(border, "X", style1)) { prefabs.Remove(prefabs[i]); LoadPalette(activePalette); } } r2.y = y; r2.height = prefabListHeight + prefabBoxSize + 7; //Handle and show drag and drop area HandleDragAndDropArea(r2); }