com.alicizax.unity.ui.exten.../Editor/Inspector/UXUIEditor.cs

86 lines
3.7 KiB
C#
Raw Normal View History

2025-07-28 19:42:29 +08:00
using System;
using System.Reflection;
using TMPro.EditorUtilities;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
2025-07-28 20:52:34 +08:00
internal class UXUIEditor : Editor
2025-07-28 19:42:29 +08:00
{
static object InvokeMethod(Type type, string methodName, object[] parameters = null)
{
if (parameters == null)
{
return type.GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static).Invoke(null, null);
}
Type[] types = new Type[parameters.Length];
for (int i = 0; i < parameters.Length; i++)
{
types[i] = parameters[i].GetType();
}
return type.GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static, null, types, null).Invoke(null, parameters);
}
[MenuItem("GameObject/UI/UXImage")]
public static void CreateUXImage(MenuCommand menuCommand)
{
Type MenuOptionsType = typeof(UnityEditor.UI.ImageEditor).Assembly.GetType("UnityEditor.UI.MenuOptions");
InvokeMethod(MenuOptionsType, "AddImage", new object[] { menuCommand });
GameObject obj = Selection.activeGameObject;
obj.name = "UXImage";
DestroyImmediate(obj.GetComponent<Image>());
var image = obj.AddComponent<UXImage>();
image.material = AssetDatabase.LoadAssetAtPath<Material>(UXGUIConfig.UIDefaultMatPath);
}
[MenuItem("GameObject/UI/UXTextMeshPro")]
private static void CreateUXTextMeshPro(MenuCommand menuCommand)
{
Type MenuOptionsType = typeof(UnityEditor.UI.ImageEditor).Assembly.GetType("UnityEditor.UI.MenuOptions");
InvokeMethod(MenuOptionsType, "AddText", new object[] { menuCommand });
GameObject obj = Selection.activeGameObject;
obj.name = "UXTextMeshPro";
DestroyImmediate(obj.GetComponent<Text>());
obj.AddComponent<UXTextMeshPro>();
}
[MenuItem("GameObject/UI/UXButton")]
public static void CreateUXButton(MenuCommand menuCommand)
{
Type MenuOptionsType = typeof(TMPro.EditorUtilities.TMPro_CreateObjectMenu).Assembly.GetType("TMPro.EditorUtilities.TMPro_CreateObjectMenu");
InvokeMethod(MenuOptionsType, "AddButton", new object[] { menuCommand });
GameObject obj = Selection.activeGameObject;
obj.name = "UXButton";
DestroyImmediate(obj.GetComponent<Button>());
obj.AddComponent<UXButton>();
}
[MenuItem("GameObject/UI/UXScrollView")]
private static void CreateUxRecyclerView()
{
GameObject selectionObject = Selection.activeGameObject;
if (selectionObject == null) return;
const string prefabPath = "Packages/com.alicizax.unity.ui.extension/Editor/RecyclerView/Res/ScrollView.prefab";
GameObject prefab = AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath);
GameObject instance = (GameObject)PrefabUtility.InstantiatePrefab(prefab, selectionObject.transform);
PrefabUtility.UnpackPrefabInstance(instance, PrefabUnpackMode.Completely, InteractionMode.UserAction);
Selection.activeGameObject = instance;
}
2025-08-01 19:32:36 +08:00
[MenuItem("GameObject/UI/UXTemplateWindow")]
private static void CreateTemplateWindow()
{
GameObject selectionObject = Selection.activeGameObject;
if (selectionObject == null) return;
const string prefabPath = "Packages/com.alicizax.unity.ui.extension/Editor/Res/Template/UITemplateWindow.prefab";
GameObject prefab = AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath);
GameObject instance = (GameObject)PrefabUtility.InstantiatePrefab(prefab, selectionObject.transform);
PrefabUtility.UnpackPrefabInstance(instance, PrefabUnpackMode.Completely, InteractionMode.UserAction);
Selection.activeGameObject = instance;
}
2025-07-28 19:42:29 +08:00
}