com.alicizax.unity.framework/Editor/UI/GenerateWindow/UIGenerateEditorWindow.cs

113 lines
4.2 KiB
C#
Raw Normal View History

2025-09-05 19:46:30 +08:00
using System.Linq;
using UnityEditor;
using UnityEngine;
2025-11-07 20:47:57 +08:00
namespace AlicizaX.UI.Editor
{
public class UIGenerateEditorWindow : EditorWindow
{
private GameObject selectedObject;
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;
2025-11-10 16:01:53 +08:00
var window = GetWindow<UIGenerateEditorWindow>(true, "UI 生成绑定", true);
2025-11-07 20:47:57 +08:00
window.selectedObject = selectedObject;
window.menuItems = uiScriptConfigs.Select(config => $"{config.ProjectName}").ToArray();
2025-11-10 16:01:53 +08:00
var windowWidth = 300f;
var windowHeight = Mathf.Max(1, window.menuItems.Length) * 35f + 10f;
2025-11-07 20:47:57 +08:00
Vector3 objectWorldPosition = selectedObject.transform.position;
2025-11-10 16:01:53 +08:00
Vector2 screenPoint;
var sceneView = SceneView.lastActiveSceneView;
if (sceneView != null)
{
Vector2 guiPointInSceneView = HandleUtility.WorldToGUIPoint(objectWorldPosition);
screenPoint = new Vector2(sceneView.position.x + guiPointInSceneView.x,
sceneView.position.y + guiPointInSceneView.y);
}
else if (EditorWindow.mouseOverWindow != null)
{
Vector2 guiPoint = HandleUtility.WorldToGUIPoint(objectWorldPosition);
var host = EditorWindow.mouseOverWindow;
screenPoint = new Vector2(host.position.x + guiPoint.x, host.position.y + guiPoint.y);
}
else
{
var mainDisplay = Display.displays.Length > 0 ? Display.displays[0] : null;
float centerX = (mainDisplay != null) ? (mainDisplay.systemWidth / 2f) : (Screen.width / 2f);
float centerY = (mainDisplay != null) ? (mainDisplay.systemHeight / 2f) : (Screen.height / 2f);
screenPoint = new Vector2(centerX, centerY);
}
Vector2 windowPosition = new Vector2(screenPoint.x, screenPoint.y - windowHeight - 5f);
float screenW = Mathf.Max(100, Display.main.systemWidth);
float screenH = Mathf.Max(100, Display.main.systemHeight);
if (windowPosition.x + windowWidth > screenW) windowPosition.x = screenW - windowWidth - 5f;
if (windowPosition.x < 5f) windowPosition.x = 5f;
if (windowPosition.y < 5f) windowPosition.y = 5f;
if (windowPosition.y + windowHeight > screenH) windowPosition.y = screenH - windowHeight - 5f;
2025-11-07 20:47:57 +08:00
window.minSize = new Vector2(windowWidth, windowHeight);
window.maxSize = new Vector2(windowWidth, windowHeight);
window.position = new Rect(windowPosition, new Vector2(windowWidth, windowHeight));
2025-11-10 16:01:53 +08:00
window.ShowPopup();
2025-11-07 20:47:57 +08:00
}
private void OnGUI()
{
GUILayout.Space(5);
2025-11-10 16:01:53 +08:00
if (menuItems == null || menuItems.Length == 0)
{
EditorGUILayout.LabelField("没有可用配置");
return;
}
2025-11-07 20:47:57 +08:00
foreach (var item in menuItems)
{
2025-11-10 16:01:53 +08:00
if (GUILayout.Button(item, EditorStyles.toolbarButton, GUILayout.Height(28)))
2025-11-07 20:47:57 +08:00
{
GenerateScriptForConfig(selectedObject, item);
Close();
}
2025-11-10 16:01:53 +08:00
GUILayout.Space(6);
2025-11-07 20:47:57 +08:00
}
2025-09-05 19:46:30 +08:00
}
2025-11-07 20:47:57 +08:00
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);
}
2025-09-05 19:46:30 +08:00
}
}
}