using System; using System.Collections; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; public static class uGUIUtility { public static void SetLayer(GameObject gameObject, int layerNo, bool needChildren = false) { if (!gameObject) { return; } gameObject.layer = layerNo; if (!needChildren) { return; } IEnumerator enumerator = gameObject.transform.GetEnumerator(); try { while (enumerator.MoveNext()) { object obj = enumerator.Current; Transform transform = (Transform)obj; uGUIUtility.SetLayer(transform.gameObject, layerNo, true); } } finally { IDisposable disposable; if ((disposable = (enumerator as IDisposable)) != null) { disposable.Dispose(); } } } public static EventSystem GetEventSystem() { if (!uGUIUtility.eventSystem) { uGUIUtility.eventSystem = EventSystem.current; if (!uGUIUtility.eventSystem) { Debug.LogWarning("[uGUIUtility]EventSystem が存在しないか無効になっています"); return null; } BaseInputModule component = uGUIUtility.eventSystem.GetComponent(); if (component != null) { UnityEngine.Object.Destroy(component); } component = uGUIUtility.eventSystem.GetComponent(); if (component == null) { uGUIUtility.eventSystem.gameObject.AddComponent(); } } return uGUIUtility.eventSystem; } public static void SetActiveEventSystem(bool b) { EventSystem eventSystem = uGUIUtility.GetEventSystem(); if (eventSystem == null) { return; } if (eventSystem.enabled != b) { eventSystem.enabled = b; } } public static void SetSelectedGameObject(GameObject Obj) { EventSystem eventSystem = uGUIUtility.GetEventSystem(); if (eventSystem == null) { return; } eventSystem.SetSelectedGameObject(Obj); } public static void SetSelectableInterctable(GameObject Obj, bool interactable, bool changeChildren = false) { Selectable componentInChildren = Obj.GetComponentInChildren(); componentInChildren.interactable = interactable; if (changeChildren) { Selectable[] componentsInChildren = Obj.GetComponentsInChildren(); for (int i = 0; i < componentsInChildren.Length; i++) { componentsInChildren[i].interactable = interactable; } } } public static Sprite GetSpriteInAtlas(UIAtlas atlas, string resourceName) { if (atlas == null) { Debug.LogError("NGUIのアトラスにnullが指定されました"); return null; } Texture2D texture2D = (Texture2D)atlas.texture; UISpriteData sprite = atlas.GetSprite(resourceName); if (sprite == null) { Debug.LogWarningFormat("NGUIのアトラス「{0}」から「{1}」が見つかりませんでした", new object[] { atlas.name, resourceName }); return null; } Rect rect = new Rect((float)sprite.x, (float)(texture2D.height - sprite.y - sprite.height), (float)sprite.width, (float)sprite.height); Sprite sprite2; if (!sprite.hasBorder) { sprite2 = Sprite.Create(texture2D, rect, Vector2.one * 0.5f, 100f, 0u, SpriteMeshType.FullRect); } else { sprite2 = Sprite.Create(texture2D, rect, Vector2.one * 0.5f, 100f, 0u, SpriteMeshType.FullRect, new Vector4((float)sprite.borderLeft, (float)sprite.borderBottom, (float)sprite.borderRight, (float)sprite.borderTop)); } sprite2.name = resourceName; return sprite2; } public static Sprite CreateSprite(Texture2D tex) { return Sprite.Create(tex, new Rect(0f, 0f, (float)tex.width, (float)tex.height), Vector2.one * 0.5f, 100f, 0u, SpriteMeshType.FullRect); } private static EventSystem eventSystem; }