Nekopletní fix
This commit is contained in:
@@ -23,17 +23,92 @@ namespace MegaKoop.EditorTools
|
||||
canvas = canvasGO.GetComponent<Canvas>();
|
||||
if (canvas == null) canvas = canvasGO.AddComponent<Canvas>();
|
||||
canvas.renderMode = RenderMode.ScreenSpaceOverlay;
|
||||
canvas.sortingOrder = 100;
|
||||
var scaler = canvasGO.GetComponent<CanvasScaler>();
|
||||
if (scaler == null) scaler = canvasGO.AddComponent<CanvasScaler>();
|
||||
scaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
|
||||
scaler.referenceResolution = new Vector2(1920, 1080);
|
||||
scaler.matchWidthOrHeight = 0.5f;
|
||||
var raycaster = canvasGO.GetComponent<GraphicRaycaster>();
|
||||
if (raycaster == null) raycaster = canvasGO.AddComponent<GraphicRaycaster>();
|
||||
|
||||
// Ensure EventSystem exists
|
||||
if (Object.FindObjectOfType<UnityEngine.EventSystems.EventSystem>() == null)
|
||||
// Ensure/upgrade EventSystem based on available modules
|
||||
var existingES = Object.FindObjectOfType<UnityEngine.EventSystems.EventSystem>();
|
||||
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), typeof(UnityEngine.EventSystems.StandaloneInputModule));
|
||||
var es = new GameObject("EventSystem", typeof(UnityEngine.EventSystems.EventSystem));
|
||||
if (wantIS) es.AddComponent(inputSystemModuleType);
|
||||
if (wantLegacy) es.AddComponent<UnityEngine.EventSystems.StandaloneInputModule>();
|
||||
Undo.RegisterCreatedObjectUndo(es, "Create EventSystem");
|
||||
esGO = es;
|
||||
}
|
||||
else
|
||||
{
|
||||
esGO = existingES.gameObject;
|
||||
var sim = existingES.GetComponent<UnityEngine.EventSystems.StandaloneInputModule>();
|
||||
var ism = inputSystemModuleType != null ? existingES.GetComponent(inputSystemModuleType) : null;
|
||||
if (wantIS && ism == null) Undo.AddComponent(existingES.gameObject, inputSystemModuleType);
|
||||
if (wantLegacy && sim == null) Undo.AddComponent<UnityEngine.EventSystems.StandaloneInputModule>(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<UnityEngine.Object>(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<UnityEngine.Object>(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<UnityEngine.EventSystems.StandaloneInputModule>() != null;
|
||||
var hasIS = inputSystemModuleType != null && esGO.GetComponent(inputSystemModuleType) != null;
|
||||
if (hasIS && !assignedUIActions && !hasSIM)
|
||||
{
|
||||
Undo.AddComponent<UnityEngine.EventSystems.StandaloneInputModule>(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
|
||||
@@ -73,9 +148,15 @@ namespace MegaKoop.EditorTools
|
||||
|
||||
var gfxGroup = CreateVerticalGroup(settingsContainer.transform, "Graphics_Group", 10, TextAnchor.UpperLeft, new RectOffset(10,10,10,10));
|
||||
CreateText(gfxGroup.transform, "Text_Graphics", "Graphics", 24, TextAnchor.MiddleLeft, Color.white, FontStyles.Bold);
|
||||
CreateDropdown(gfxGroup.transform, "Dropdown_Quality", new string[]{"Low","Medium","High","Ultra"});
|
||||
CreateUnityDropdown(gfxGroup.transform, "Dropdown_Quality", new string[]{"Low","Medium","High","Ultra"});
|
||||
CreateToggle(gfxGroup.transform, "Toggle_Fullscreen", "Fullscreen", true);
|
||||
CreateDropdown(gfxGroup.transform, "Dropdown_Resolution", new string[]{"1280x720","1920x1080","2560x1440","3840x2160"});
|
||||
CreateUnityDropdown(gfxGroup.transform, "Dropdown_Resolution", new string[]{"1280x720","1920x1080","2560x1440","3840x2160"});
|
||||
|
||||
var audioGroup = CreateVerticalGroup(settingsContainer.transform, "Audio_Group", 10, TextAnchor.UpperLeft, new RectOffset(10,10,10,10));
|
||||
CreateText(audioGroup.transform, "Text_Audio", "Audio", 24, TextAnchor.MiddleLeft, Color.white, FontStyles.Bold);
|
||||
CreateLabeledSlider(audioGroup.transform, "Master Volume", "Slider_MasterVolume", 0, 100, 100);
|
||||
CreateLabeledSlider(audioGroup.transform, "Music Volume", "Slider_MusicVolume", 0, 100, 80);
|
||||
CreateLabeledSlider(audioGroup.transform, "SFX Volume", "Slider_SFXVolume", 0, 100, 100);
|
||||
|
||||
var settingsButtons = CreateHorizontalGroup(settingsContainer.transform, "Settings_Buttons", 10, TextAnchor.MiddleCenter, new RectOffset(0,0,0,0));
|
||||
CreateMenuButton(settingsButtons.transform, "Button_ApplySettings", "APPLY");
|
||||
@@ -206,6 +287,22 @@ namespace MegaKoop.EditorTools
|
||||
return go;
|
||||
}
|
||||
|
||||
private static GameObject CreateUnityDropdown(Transform parent, string name, string[] options)
|
||||
{
|
||||
var resources = new DefaultControls.Resources();
|
||||
var go = DefaultControls.CreateDropdown(resources);
|
||||
go.name = name;
|
||||
go.transform.SetParent(parent, false);
|
||||
var rt = (RectTransform)go.transform; rt.sizeDelta = new Vector2(600, 50);
|
||||
|
||||
var font = Resources.GetBuiltinResource<Font>("Arial.ttf");
|
||||
foreach (var t in go.GetComponentsInChildren<Text>(true)) t.font = font;
|
||||
var dd = go.GetComponent<Dropdown>();
|
||||
dd.options = (options != null ? options.Select(o => new Dropdown.OptionData(o)).ToList() : new System.Collections.Generic.List<Dropdown.OptionData>());
|
||||
dd.RefreshShownValue();
|
||||
return go;
|
||||
}
|
||||
|
||||
private static GameObject CreateVerticalGroup(Transform parent, string name, float spacing, TextAnchor align, RectOffset padding)
|
||||
{
|
||||
var go = new GameObject(name, typeof(RectTransform), typeof(VerticalLayoutGroup), typeof(ContentSizeFitter));
|
||||
@@ -316,12 +413,18 @@ namespace MegaKoop.EditorTools
|
||||
template.SetActive(false);
|
||||
var templateImg = template.GetComponent<Image>(); templateImg.color = new Color(0.1f,0.1f,0.1f,0.95f);
|
||||
|
||||
var tplCanvas = template.GetComponent<Canvas>();
|
||||
if (tplCanvas == null) tplCanvas = template.AddComponent<Canvas>();
|
||||
tplCanvas.overrideSorting = true;
|
||||
tplCanvas.sortingOrder = 5000;
|
||||
if (!template.GetComponent<GraphicRaycaster>()) template.AddComponent<GraphicRaycaster>();
|
||||
|
||||
// Viewport
|
||||
var viewport = new GameObject("Viewport", typeof(RectTransform), typeof(Mask), typeof(Image));
|
||||
viewport.transform.SetParent(template.transform, false);
|
||||
var viewportRT = (RectTransform)viewport.transform; viewportRT.anchorMin = new Vector2(0,0); viewportRT.anchorMax = new Vector2(1,1); viewportRT.offsetMin = Vector2.zero; viewportRT.offsetMax = Vector2.zero;
|
||||
var vImg = viewport.GetComponent<Image>(); vImg.color = new Color(0,0,0,0.2f);
|
||||
viewport.GetComponent<Mask>().showMaskGraphic = false;
|
||||
var vImg = viewport.GetComponent<Image>(); vImg.color = new Color(0,0,0,0.2f); vImg.raycastTarget = false;
|
||||
var mask = viewport.GetComponent<Mask>(); mask.showMaskGraphic = false;
|
||||
|
||||
// Scroll content
|
||||
var content = new GameObject("Content", typeof(RectTransform), typeof(VerticalLayoutGroup));
|
||||
@@ -356,6 +459,7 @@ namespace MegaKoop.EditorTools
|
||||
dd.captionText = caption.GetComponent<TextMeshProUGUI>();
|
||||
dd.itemText = itemLabelGO.GetComponent<TextMeshProUGUI>();
|
||||
dd.options = options.Select(o => new TMP_Dropdown.OptionData(o)).ToList();
|
||||
dd.alphaFadeSpeed = 0.15f;
|
||||
dd.RefreshShownValue();
|
||||
|
||||
// ScrollRect wiring
|
||||
@@ -366,6 +470,53 @@ namespace MegaKoop.EditorTools
|
||||
return go;
|
||||
}
|
||||
|
||||
private static GameObject CreateSlider(Transform parent, string name, float min, float max, float value)
|
||||
{
|
||||
var go = new GameObject(name, typeof(RectTransform), typeof(Slider));
|
||||
go.transform.SetParent(parent, false);
|
||||
var slider = go.GetComponent<Slider>();
|
||||
slider.minValue = min; slider.maxValue = max; slider.value = value; slider.direction = Slider.Direction.LeftToRight; slider.wholeNumbers = true;
|
||||
var rt = (RectTransform)go.transform; rt.sizeDelta = new Vector2(600, 30);
|
||||
|
||||
var background = new GameObject("Background", typeof(RectTransform), typeof(Image));
|
||||
background.transform.SetParent(go.transform, false);
|
||||
var bgRT = (RectTransform)background.transform; bgRT.anchorMin = new Vector2(0,0.25f); bgRT.anchorMax = new Vector2(1,0.75f); bgRT.offsetMin = Vector2.zero; bgRT.offsetMax = Vector2.zero;
|
||||
var bgImg = background.GetComponent<Image>(); bgImg.color = new Color(0.1f,0.1f,0.1f,1f);
|
||||
|
||||
var fillArea = new GameObject("Fill Area", typeof(RectTransform));
|
||||
fillArea.transform.SetParent(go.transform, false);
|
||||
var faRT = (RectTransform)fillArea.transform; faRT.anchorMin = new Vector2(0,0.25f); faRT.anchorMax = new Vector2(1,0.75f); faRT.offsetMin = new Vector2(10,0); faRT.offsetMax = new Vector2(-10,0);
|
||||
|
||||
var fill = new GameObject("Fill", typeof(RectTransform), typeof(Image));
|
||||
fill.transform.SetParent(fillArea.transform, false);
|
||||
var fillImg = fill.GetComponent<Image>(); fillImg.color = new Color(0.4f,0.9f,0.4f,1f);
|
||||
var fillRT = (RectTransform)fill.transform; fillRT.anchorMin = new Vector2(0,0); fillRT.anchorMax = new Vector2(1,1); fillRT.offsetMin = Vector2.zero; fillRT.offsetMax = Vector2.zero;
|
||||
|
||||
var handleArea = new GameObject("Handle Slide Area", typeof(RectTransform));
|
||||
handleArea.transform.SetParent(go.transform, false);
|
||||
var haRT = (RectTransform)handleArea.transform; haRT.anchorMin = new Vector2(0,0); haRT.anchorMax = new Vector2(1,1); haRT.offsetMin = Vector2.zero; haRT.offsetMax = Vector2.zero;
|
||||
|
||||
var handle = new GameObject("Handle", typeof(RectTransform), typeof(Image));
|
||||
handle.transform.SetParent(handleArea.transform, false);
|
||||
var handleImg = handle.GetComponent<Image>(); handleImg.color = new Color(0.8f,1f,0.8f,1f);
|
||||
var hRT = (RectTransform)handle.transform; hRT.sizeDelta = new Vector2(16, 24); hRT.anchorMin = new Vector2(0.5f,0.5f); hRT.anchorMax = new Vector2(0.5f,0.5f); hRT.anchoredPosition = Vector2.zero;
|
||||
|
||||
slider.fillRect = fill.GetComponent<RectTransform>();
|
||||
slider.handleRect = handle.GetComponent<RectTransform>();
|
||||
slider.targetGraphic = handleImg;
|
||||
slider.direction = Slider.Direction.LeftToRight;
|
||||
|
||||
return go;
|
||||
}
|
||||
|
||||
private static GameObject CreateLabeledSlider(Transform parent, string label, string name, float min, float max, float value)
|
||||
{
|
||||
var group = CreateVerticalGroup(parent, name + "_Group", 4, TextAnchor.UpperLeft, new RectOffset(0,0,0,0));
|
||||
CreateText(group.transform, name + "_Label", label, 18, TextAnchor.MiddleLeft, Color.white, FontStyles.Normal);
|
||||
CreateSlider(group.transform, name, min, max, value);
|
||||
return group;
|
||||
}
|
||||
|
||||
private static GameObject CreateToggle(Transform parent, string name, string labelText, bool initial)
|
||||
{
|
||||
var go = new GameObject(name, typeof(RectTransform), typeof(Toggle));
|
||||
|
||||
Reference in New Issue
Block a user