uGUIUtility.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. using UnityEngine.UI;
  6. public static class uGUIUtility
  7. {
  8. public static void SetLayer(GameObject gameObject, int layerNo, bool needChildren = false)
  9. {
  10. if (!gameObject)
  11. {
  12. return;
  13. }
  14. gameObject.layer = layerNo;
  15. if (!needChildren)
  16. {
  17. return;
  18. }
  19. IEnumerator enumerator = gameObject.transform.GetEnumerator();
  20. try
  21. {
  22. while (enumerator.MoveNext())
  23. {
  24. object obj = enumerator.Current;
  25. Transform transform = (Transform)obj;
  26. uGUIUtility.SetLayer(transform.gameObject, layerNo, true);
  27. }
  28. }
  29. finally
  30. {
  31. IDisposable disposable;
  32. if ((disposable = (enumerator as IDisposable)) != null)
  33. {
  34. disposable.Dispose();
  35. }
  36. }
  37. }
  38. public static EventSystem GetEventSystem()
  39. {
  40. if (!uGUIUtility.eventSystem)
  41. {
  42. uGUIUtility.eventSystem = EventSystem.current;
  43. if (!uGUIUtility.eventSystem)
  44. {
  45. Debug.LogWarning("[uGUIUtility]EventSystem が存在しないか無効になっています");
  46. return null;
  47. }
  48. BaseInputModule component = uGUIUtility.eventSystem.GetComponent<StandaloneInputModule>();
  49. if (component != null)
  50. {
  51. UnityEngine.Object.Destroy(component);
  52. }
  53. component = uGUIUtility.eventSystem.GetComponent<StandaloneInputModule2>();
  54. if (component == null)
  55. {
  56. uGUIUtility.eventSystem.gameObject.AddComponent<StandaloneInputModule2>();
  57. }
  58. }
  59. return uGUIUtility.eventSystem;
  60. }
  61. public static void SetActiveEventSystem(bool b)
  62. {
  63. EventSystem eventSystem = uGUIUtility.GetEventSystem();
  64. if (eventSystem == null)
  65. {
  66. return;
  67. }
  68. if (eventSystem.enabled != b)
  69. {
  70. eventSystem.enabled = b;
  71. }
  72. }
  73. public static void SetSelectedGameObject(GameObject Obj)
  74. {
  75. EventSystem eventSystem = uGUIUtility.GetEventSystem();
  76. if (eventSystem == null)
  77. {
  78. return;
  79. }
  80. eventSystem.SetSelectedGameObject(Obj);
  81. }
  82. public static void SetSelectableInterctable(GameObject Obj, bool interactable, bool changeChildren = false)
  83. {
  84. Selectable componentInChildren = Obj.GetComponentInChildren<Selectable>();
  85. componentInChildren.interactable = interactable;
  86. if (changeChildren)
  87. {
  88. Selectable[] componentsInChildren = Obj.GetComponentsInChildren<Selectable>();
  89. for (int i = 0; i < componentsInChildren.Length; i++)
  90. {
  91. componentsInChildren[i].interactable = interactable;
  92. }
  93. }
  94. }
  95. public static Sprite GetSpriteInAtlas(UIAtlas atlas, string resourceName)
  96. {
  97. if (atlas == null)
  98. {
  99. Debug.LogError("NGUIのアトラスにnullが指定されました");
  100. return null;
  101. }
  102. Texture2D texture2D = (Texture2D)atlas.texture;
  103. UISpriteData sprite = atlas.GetSprite(resourceName);
  104. if (sprite == null)
  105. {
  106. Debug.LogWarningFormat("NGUIのアトラス「{0}」から「{1}」が見つかりませんでした", new object[]
  107. {
  108. atlas.name,
  109. resourceName
  110. });
  111. return null;
  112. }
  113. Rect rect = new Rect((float)sprite.x, (float)(texture2D.height - sprite.y - sprite.height), (float)sprite.width, (float)sprite.height);
  114. Sprite sprite2;
  115. if (!sprite.hasBorder)
  116. {
  117. sprite2 = Sprite.Create(texture2D, rect, Vector2.one * 0.5f, 100f, 0U, SpriteMeshType.FullRect);
  118. }
  119. else
  120. {
  121. 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));
  122. }
  123. sprite2.name = resourceName;
  124. return sprite2;
  125. }
  126. public static Sprite CreateSprite(Texture2D tex)
  127. {
  128. return Sprite.Create(tex, new Rect(0f, 0f, (float)tex.width, (float)tex.height), Vector2.one * 0.5f, 100f, 0U, SpriteMeshType.FullRect);
  129. }
  130. private static EventSystem eventSystem;
  131. }