#if UNITY_EDITOR using UnityEditor; using UnityEngine; using UnityEngine.UI; namespace MegaKoop.EditorTools { public static class UGUIMenuBuilder { [MenuItem("Tools/MegaKoop/Generate UGUI Main Menu", priority = 0)] public static void GenerateUGUIMainMenu() { // Create or use dedicated UI_Canvas (do NOT bind to a random existing Canvas) Canvas canvas = null; var canvasGO = GameObject.Find("UI_Canvas"); if (canvasGO == null) { canvasGO = new GameObject("UI_Canvas", typeof(Canvas), typeof(CanvasScaler), typeof(GraphicRaycaster)); Undo.RegisterCreatedObjectUndo(canvasGO, "Create Canvas"); } canvas = canvasGO.GetComponent(); if (canvas == null) canvas = canvasGO.AddComponent(); canvas.renderMode = RenderMode.ScreenSpaceOverlay; canvas.sortingOrder = 100; var scaler = canvasGO.GetComponent(); if (scaler == null) scaler = canvasGO.AddComponent(); scaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize; scaler.referenceResolution = new Vector2(1920, 1080); scaler.matchWidthOrHeight = 0.5f; var raycaster = canvasGO.GetComponent(); if (raycaster == null) raycaster = canvasGO.AddComponent(); // Ensure/upgrade EventSystem based on available modules var existingES = Object.FindObjectOfType(); var inputSystemModuleType = System.Type.GetType("UnityEngine.InputSystem.UI.InputSystemUIInputModule, Unity.InputSystem", false); bool wantIS = inputSystemModuleType != null; bool wantLegacy = !wantIS; GameObject esGO = null; if (existingES == null) { var es = new GameObject("EventSystem", typeof(UnityEngine.EventSystems.EventSystem)); if (wantIS) es.AddComponent(inputSystemModuleType); if (wantLegacy) es.AddComponent(); Undo.RegisterCreatedObjectUndo(es, "Create EventSystem"); esGO = es; } else { esGO = existingES.gameObject; var sim = existingES.GetComponent(); var ism = inputSystemModuleType != null ? existingES.GetComponent(inputSystemModuleType) : null; if (wantIS && ism == null) Undo.AddComponent(existingES.gameObject, inputSystemModuleType); if (wantLegacy && sim == null) Undo.AddComponent(existingES.gameObject); } // Assign actionsAsset only if the picked InputActionAsset contains a "UI" action map bool assignedUIActions = false; if (wantIS && inputSystemModuleType != null && esGO != null) { var module = esGO.GetComponent(inputSystemModuleType); if (module != null) { var prop = inputSystemModuleType.GetProperty("actionsAsset"); if (prop != null) { var current = prop.GetValue(module, null) as UnityEngine.Object; if (current == null) { var iaaType = System.Type.GetType("UnityEngine.InputSystem.InputActionAsset, Unity.InputSystem", false); string ChooseUIAsset() { var guids = AssetDatabase.FindAssets("t:InputActionAsset"); foreach (var g in guids) { var path = AssetDatabase.GUIDToAssetPath(g); var asset = AssetDatabase.LoadAssetAtPath(path); if (asset != null && iaaType != null && iaaType.IsInstanceOfType(asset)) { var map = iaaType.GetMethod("FindActionMap", new[] { typeof(string), typeof(bool) })?.Invoke(asset, new object[] { "UI", true }); if (map != null) return path; } } return null; } var best = ChooseUIAsset(); if (!string.IsNullOrEmpty(best)) { var asset = AssetDatabase.LoadAssetAtPath(best); prop.SetValue(module, asset, null); var comp = module as Component; if (comp != null) EditorUtility.SetDirty(comp); assignedUIActions = true; } // else: leave null to let module use its default actions } else { assignedUIActions = true; } } } } if (esGO != null) { var hasSIM = esGO.GetComponent() != null; var hasIS = inputSystemModuleType != null && esGO.GetComponent(inputSystemModuleType) != null; if (hasIS && !assignedUIActions && !hasSIM) { Undo.AddComponent(esGO); } Debug.Log($"[UGUIMenuBuilder] EventSystem configured. InputSystem={(hasIS ? "ON" : "OFF")} (actions={(assignedUIActions ? "OK" : "none")}), Standalone={(hasSIM ? "ON" : "OFF")}"); } // Clean old generated panels if present to avoid duplicates void DestroyChildIfExists(Transform parent, string childName) { var child = parent.Find(childName); if (child != null) { Undo.DestroyObjectImmediate(child.gameObject); } } DestroyChildIfExists(canvas.transform, "Panel_MainMenu"); DestroyChildIfExists(canvas.transform, "Panel_Settings"); DestroyChildIfExists(canvas.transform, "Panel_Lobby"); var mainPanel = new MainMenuPanelBuilder(canvas.transform).Build(); var settingsPanel = new SettingsPanelBuilder(canvas.transform).Build(); var lobbyPanel = new LobbyPanelBuilder(canvas.transform).Build(); // Controllers var mainCtrl = canvas.gameObject.GetComponent(); if (mainCtrl == null) mainCtrl = canvas.gameObject.AddComponent(); var lobbyCtrl = canvas.gameObject.GetComponent(); if (lobbyCtrl == null) lobbyCtrl = canvas.gameObject.AddComponent(); // Inject panel references for reliability mainCtrl.SetPanels(mainPanel, settingsPanel, lobbyPanel); lobbyCtrl.SetLobbyRoot(lobbyPanel); Selection.activeGameObject = canvas.gameObject; Debug.Log("[UGUIMenuBuilder] UGUI Main Menu generated successfully. You can press Play and test."); } } } #endif