74 lines
2.5 KiB
C#
74 lines
2.5 KiB
C#
using System.Linq;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
|
|
namespace AlicizaX.UI.Editor
|
|
{
|
|
public class UIGenerateEditorWindow : EditorWindow
|
|
{
|
|
private GameObject selectedObject;
|
|
private Vector2 windowPosition;
|
|
private float windowWidth;
|
|
private float windowHeight;
|
|
private string[] menuItems;
|
|
|
|
[MenuItem("GameObject/UI生成绑定", priority = 10)]
|
|
public static void ShowWindow()
|
|
{
|
|
GameObject selectedObject = Selection.gameObjects.FirstOrDefault();
|
|
if (selectedObject == null) return;
|
|
|
|
var uiScriptConfigs = UIGenerateConfiguration.Instance.UIScriptGenerateConfigs;
|
|
if (uiScriptConfigs == null || uiScriptConfigs.Count == 0) return;
|
|
|
|
var window = GetWindow<UIGenerateEditorWindow>("", true);
|
|
window.selectedObject = selectedObject;
|
|
|
|
window.menuItems = uiScriptConfigs.Select(config => $"{config.ProjectName}").ToArray();
|
|
|
|
var windowWidth = 300;
|
|
var windowHeight = (window.menuItems.Length * 35f);
|
|
|
|
Vector3 objectWorldPosition = selectedObject.transform.position;
|
|
Vector3 screenPosition = HandleUtility.WorldToGUIPoint(objectWorldPosition);
|
|
var windowPosition = new Vector2(screenPosition.x, screenPosition.y - windowHeight - 5f);
|
|
|
|
window.minSize = new Vector2(windowWidth, windowHeight);
|
|
window.maxSize = new Vector2(windowWidth, windowHeight);
|
|
|
|
window.position = new Rect(windowPosition, new Vector2(windowWidth, windowHeight));
|
|
window.Show();
|
|
}
|
|
|
|
private void OnGUI()
|
|
{
|
|
GUILayout.Space(5);
|
|
foreach (var item in menuItems)
|
|
{
|
|
if (GUILayout.Button(item, EditorStyles.toolbarButton))
|
|
{
|
|
GenerateScriptForConfig(selectedObject, item);
|
|
Close();
|
|
}
|
|
|
|
GUILayout.Space(10);
|
|
}
|
|
}
|
|
|
|
private void GenerateScriptForConfig(GameObject selectedObject, string itemName)
|
|
{
|
|
var uiScriptConfigs = UIGenerateConfiguration.Instance.UIScriptGenerateConfigs;
|
|
var config = uiScriptConfigs.FirstOrDefault(c => $"{c.ProjectName}" == itemName);
|
|
|
|
if (config != null)
|
|
{
|
|
UIScriptGeneratorHelper.GenerateAndAttachScript(selectedObject, config);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning("Configuration not found for item: " + itemName);
|
|
}
|
|
}
|
|
}
|
|
}
|