190 lines
6.6 KiB
C#
190 lines
6.6 KiB
C#
using UnityEditor;
|
|
using UnityEngine;
|
|
using System.Linq;
|
|
using System.Collections.Generic;
|
|
|
|
public class HeaderCreatorWindow : EditorWindow
|
|
{
|
|
private string objectName = "New GameObject";
|
|
private string headerDescription = "";
|
|
private int selectedIdentifier = 1;
|
|
|
|
private static HierarchyDecoratorSettings settings;
|
|
private Vector2 scrollPosition;
|
|
private Dictionary<int, GUIStyle> colorStyles;
|
|
|
|
[MenuItem("GameObject/Create Custom Header", false, 0)]
|
|
[MenuItem("GameObject/Create Empty", false, 0)]
|
|
public static void ShowWindow()
|
|
{
|
|
HeaderCreatorWindow window = GetWindow<HeaderCreatorWindow>(true, "Create Object");
|
|
// ZMIANA: Zwiêkszamy minimaln¹ wysokoœæ okna, aby zmieœci³ siê nowy przycisk
|
|
window.minSize = new Vector2(320, 340);
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
settings = AssetDatabase.LoadAssetAtPath<HierarchyDecoratorSettings>("Assets/Scripts/Editor/HierarchyDecoratorSettings.asset");
|
|
|
|
if (settings == null)
|
|
{
|
|
Debug.LogError("HierarchyDecoratorSettings not found!");
|
|
Close();
|
|
return;
|
|
}
|
|
|
|
if (settings.colorSchemes.Count > 0)
|
|
{
|
|
selectedIdentifier = settings.colorSchemes.First().identifier;
|
|
InitializeButtonStyles();
|
|
}
|
|
}
|
|
|
|
private void OnGUI()
|
|
{
|
|
if (settings == null) return;
|
|
|
|
EditorGUILayout.LabelField("Create a new object", EditorStyles.boldLabel);
|
|
objectName = EditorGUILayout.TextField("Name", objectName);
|
|
|
|
EditorGUILayout.Space(10);
|
|
|
|
EditorGUILayout.LabelField("For Custom Header:", EditorStyles.miniBoldLabel);
|
|
headerDescription = EditorGUILayout.TextField("Description (Optional)", headerDescription);
|
|
|
|
if (settings.colorSchemes.Count == 0)
|
|
{
|
|
EditorGUILayout.HelpBox("No color schemes defined in HierarchyDecoratorSettings. You can only create a simple empty object.", MessageType.Warning);
|
|
}
|
|
else
|
|
{
|
|
EditorGUILayout.LabelField("Color Scheme", EditorStyles.boldLabel);
|
|
DrawColorSelectionGrid();
|
|
}
|
|
|
|
EditorGUILayout.Space();
|
|
|
|
GUI.enabled = settings.colorSchemes.Count > 0;
|
|
if (GUILayout.Button("Create Custom Header", GUILayout.Height(35)))
|
|
{
|
|
CreateHeaderObject();
|
|
Close();
|
|
}
|
|
GUI.enabled = true;
|
|
|
|
EditorGUILayout.Space(5);
|
|
|
|
if (GUILayout.Button("Create Simple Empty (using Name only)", GUILayout.Height(30)))
|
|
{
|
|
CreateSimpleEmptyObject();
|
|
Close();
|
|
}
|
|
|
|
// ZMIANA: Dodajemy nowy przycisk do tworzenia separatora "= Clean"
|
|
EditorGUILayout.Space(10); // Dodajemy trochê wiêcej miejsca
|
|
|
|
// Ustawiamy inny kolor t³a dla tego specjalnego przycisku, aby siê wyró¿nia³
|
|
GUI.backgroundColor = new Color(0.4f, 0.4f, 0.45f);
|
|
|
|
if (GUILayout.Button("Create Clean Separator", GUILayout.Height(30)))
|
|
{
|
|
CreateCleanSeparator();
|
|
Close();
|
|
}
|
|
|
|
// WA¯NE: Resetujemy kolor t³a do domyœlnego, aby nie wp³ywa³ na inne elementy GUI
|
|
GUI.backgroundColor = Color.white;
|
|
}
|
|
|
|
private void DrawColorSelectionGrid()
|
|
{
|
|
scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition, GUILayout.Height(80));
|
|
int columns = 5;
|
|
int buttonsInRow = 0;
|
|
|
|
GUILayout.BeginHorizontal();
|
|
foreach (var scheme in settings.colorSchemes)
|
|
{
|
|
GUIStyle buttonStyle = colorStyles[scheme.identifier];
|
|
string buttonText = scheme.identifier.ToString();
|
|
|
|
if (GUILayout.Button(buttonText, buttonStyle, GUILayout.MinWidth(40), GUILayout.Height(30)))
|
|
{
|
|
selectedIdentifier = scheme.identifier;
|
|
}
|
|
|
|
if (Event.current.type == EventType.Repaint && scheme.identifier == selectedIdentifier)
|
|
{
|
|
Rect buttonRect = GUILayoutUtility.GetLastRect();
|
|
EditorGUI.DrawRect(buttonRect, new Color(0.5f, 0.5f, 0.5f, 0.4f));
|
|
}
|
|
|
|
buttonsInRow++;
|
|
if (buttonsInRow >= columns)
|
|
{
|
|
GUILayout.EndHorizontal();
|
|
GUILayout.BeginHorizontal();
|
|
buttonsInRow = 0;
|
|
}
|
|
}
|
|
GUILayout.EndHorizontal();
|
|
EditorGUILayout.EndScrollView();
|
|
}
|
|
|
|
private void CreateHeaderObject()
|
|
{
|
|
string finalName = $"{selectedIdentifier}={objectName.Trim()}";
|
|
if (!string.IsNullOrEmpty(headerDescription.Trim()))
|
|
{
|
|
finalName += $"={headerDescription.Trim()}";
|
|
}
|
|
GameObject header = new GameObject(finalName);
|
|
Undo.RegisterCreatedObjectUndo(header, "Create Custom Header");
|
|
GameObjectUtility.SetParentAndAlign(header, Selection.activeObject as GameObject);
|
|
Selection.activeObject = header;
|
|
}
|
|
|
|
private void CreateSimpleEmptyObject()
|
|
{
|
|
GameObject emptyObj = new GameObject(objectName);
|
|
Undo.RegisterCreatedObjectUndo(emptyObj, "Create Simple Empty Object");
|
|
GameObjectUtility.SetParentAndAlign(emptyObj, Selection.activeObject as GameObject);
|
|
Selection.activeObject = emptyObj;
|
|
}
|
|
|
|
// ZMIANA: Nowa funkcja do tworzenia separatora "= Clean"
|
|
private void CreateCleanSeparator()
|
|
{
|
|
// Nazwa jest sta³a i nie zale¿y od pól w oknie
|
|
GameObject cleanSeparator = new GameObject("= Clean");
|
|
Undo.RegisterCreatedObjectUndo(cleanSeparator, "Create Clean Separator");
|
|
GameObjectUtility.SetParentAndAlign(cleanSeparator, Selection.activeObject as GameObject);
|
|
Selection.activeObject = cleanSeparator;
|
|
}
|
|
|
|
private void InitializeButtonStyles()
|
|
{
|
|
colorStyles = new Dictionary<int, GUIStyle>();
|
|
foreach (var scheme in settings.colorSchemes)
|
|
{
|
|
GUIStyle style = new GUIStyle(EditorStyles.miniButton);
|
|
Texture2D backgroundTexture = MakeTex(1, 1, scheme.activeBackgroundColor);
|
|
style.normal.background = backgroundTexture;
|
|
style.active.background = backgroundTexture;
|
|
style.normal.textColor = scheme.activeTextColor;
|
|
style.alignment = TextAnchor.MiddleCenter;
|
|
style.fontStyle = FontStyle.Bold;
|
|
colorStyles.Add(scheme.identifier, style);
|
|
}
|
|
}
|
|
|
|
private Texture2D MakeTex(int width, int height, Color col)
|
|
{
|
|
Color[] pix = new Color[width * height];
|
|
for (int i = 0; i < pix.Length; ++i) pix[i] = col;
|
|
Texture2D result = new Texture2D(width, height);
|
|
result.SetPixels(pix);
|
|
result.Apply();
|
|
return result;
|
|
}
|
|
} |