NGUIWindow.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using System;
  2. using UnityEngine;
  3. namespace Kasizuki
  4. {
  5. public class NGUIWindow : MonoBehaviour
  6. {
  7. protected virtual void Awake()
  8. {
  9. if (this.m_CachedObjectDic == null)
  10. {
  11. this.m_CachedObjectDic = new ObjectCacheDic();
  12. }
  13. }
  14. public void Open(float time = 0.25f, Action callback = null)
  15. {
  16. if (!base.gameObject.activeSelf)
  17. {
  18. base.gameObject.SetActive(true);
  19. }
  20. if (this.OnOpen != null)
  21. {
  22. this.OnOpen();
  23. }
  24. this.SetRaycastBlockerActive(true);
  25. this.tweenAlpha.SetOnFinished(delegate
  26. {
  27. if (callback != null)
  28. {
  29. callback();
  30. }
  31. this.SetRaycastBlockerActive(false);
  32. });
  33. TweenAlpha.Begin(base.gameObject, time, 1f);
  34. }
  35. public void Close(float time = 0.25f, Action callback = null)
  36. {
  37. if (!base.gameObject.activeInHierarchy)
  38. {
  39. return;
  40. }
  41. callback = (Action)Delegate.Combine(callback, new Action(delegate
  42. {
  43. this.gameObject.SetActive(false);
  44. if (this.OnClose != null)
  45. {
  46. this.OnClose();
  47. }
  48. }));
  49. this.SetRaycastBlockerActive(true);
  50. this.tweenAlpha.SetOnFinished(delegate
  51. {
  52. if (callback != null)
  53. {
  54. callback();
  55. }
  56. this.SetRaycastBlockerActive(false);
  57. });
  58. TweenAlpha.Begin(base.gameObject, time, 0f);
  59. }
  60. public Action OnOpen { get; set; }
  61. public Action OnClose { get; set; }
  62. private GameObject raycastBlocker
  63. {
  64. get
  65. {
  66. if (this.m_RaycastBlocker == null)
  67. {
  68. this.m_RaycastBlocker = this.CreateBlocker();
  69. }
  70. return this.m_RaycastBlocker;
  71. }
  72. }
  73. private GameObject CreateBlocker()
  74. {
  75. GameObject gameObject = null;
  76. UIRect component = base.gameObject.GetComponent<UIRect>();
  77. if (component != null)
  78. {
  79. gameObject = new GameObject("Ray Blocker");
  80. gameObject.transform.SetParent(base.transform, false);
  81. gameObject.layer = base.gameObject.layer;
  82. gameObject.AddComponent<BoxCollider>();
  83. UIWidget uiwidget = gameObject.AddComponent<UIWidget>();
  84. uiwidget.depth = 9999;
  85. uiwidget.updateAnchors = UIRect.AnchorUpdate.OnStart;
  86. uiwidget.SetRect(-2500f, -2500f, 5000f, 5000f);
  87. uiwidget.autoResizeBoxCollider = true;
  88. uiwidget.ResizeCollider();
  89. }
  90. else
  91. {
  92. Debug.Log("[NGUI Window] " + base.gameObject.name + "には UI Rect を継承したクラスがありません");
  93. }
  94. return gameObject;
  95. }
  96. private void SetRaycastBlockerActive(bool isActive)
  97. {
  98. if (this.raycastBlocker == null)
  99. {
  100. return;
  101. }
  102. if (this.raycastBlocker.activeSelf != isActive)
  103. {
  104. this.raycastBlocker.SetActive(isActive);
  105. }
  106. }
  107. public T AddCache<T>(string name, T Obj) where T : UnityEngine.Object
  108. {
  109. if (this.m_CachedObjectDic == null)
  110. {
  111. this.m_CachedObjectDic = new ObjectCacheDic();
  112. }
  113. this.m_CachedObjectDic.AddCache<T>(name, Obj);
  114. return Obj;
  115. }
  116. public T GetCache<T>(string name) where T : UnityEngine.Object
  117. {
  118. return this.m_CachedObjectDic.GetCache<T>(name);
  119. }
  120. public void RemoveCache(string name)
  121. {
  122. this.m_CachedObjectDic.RemoveCache(name);
  123. }
  124. public GameObject GetChildObject(string name)
  125. {
  126. return UTY.GetChildObject(base.gameObject, name, false);
  127. }
  128. public T GetChildObject<T>(string name) where T : UnityEngine.Object
  129. {
  130. if (this.m_CachedObjectDic == null)
  131. {
  132. this.m_CachedObjectDic = new ObjectCacheDic();
  133. }
  134. return this.m_CachedObjectDic.GetChildObject<T>(base.gameObject, name);
  135. }
  136. public T CacheChildObject<T>(string path, string name) where T : UnityEngine.Object
  137. {
  138. return this.AddCache<T>(name, this.GetChildObject<T>(path));
  139. }
  140. public ObjectCacheDic cachedObjectDic
  141. {
  142. get
  143. {
  144. return this.m_CachedObjectDic;
  145. }
  146. }
  147. private TweenAlpha tweenAlpha
  148. {
  149. get
  150. {
  151. if (this.m_TweenAlpha == null)
  152. {
  153. this.m_TweenAlpha = base.GetComponent<TweenAlpha>();
  154. if (this.m_TweenAlpha == null)
  155. {
  156. this.m_TweenAlpha = base.gameObject.AddComponent<TweenAlpha>();
  157. if (this.m_TweenAlpha == null)
  158. {
  159. Debug.LogErrorFormat("「{0}」には「{1}」をアタッチ出来ません", new object[]
  160. {
  161. base.gameObject.name,
  162. typeof(TweenAlpha).Name
  163. });
  164. }
  165. }
  166. }
  167. return this.m_TweenAlpha;
  168. }
  169. }
  170. private GameObject m_RaycastBlocker;
  171. private TweenAlpha m_TweenAlpha;
  172. private ObjectCacheDic m_CachedObjectDic;
  173. }
  174. }