123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- 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<StandaloneInputModule>();
- if (component != null)
- {
- UnityEngine.Object.Destroy(component);
- }
- component = uGUIUtility.eventSystem.GetComponent<StandaloneInputModule2>();
- if (component == null)
- {
- uGUIUtility.eventSystem.gameObject.AddComponent<StandaloneInputModule2>();
- }
- }
- 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<Selectable>();
- componentInChildren.interactable = interactable;
- if (changeChildren)
- {
- Selectable[] componentsInChildren = Obj.GetComponentsInChildren<Selectable>();
- 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;
- }
|