uGUICanvas.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. using UnityEngine.SceneManagement;
  6. using UnityEngine.UI;
  7. public class uGUICanvas : MonoBehaviour
  8. {
  9. private RectTransform CanvasTransform
  10. {
  11. get
  12. {
  13. if (this.m_CanvasTransform == null)
  14. {
  15. this.m_CanvasTransform = this.m_Canvas.GetComponent<RectTransform>();
  16. }
  17. return this.m_CanvasTransform;
  18. }
  19. }
  20. private CanvasGroup canvasGroup
  21. {
  22. get
  23. {
  24. if (this.m_CanvasGroup == null)
  25. {
  26. this.m_CanvasGroup = base.GetComponent<CanvasGroup>();
  27. if (this.m_CanvasGroup == null)
  28. {
  29. this.m_CanvasGroup = base.gameObject.AddComponent<CanvasGroup>();
  30. }
  31. }
  32. return this.m_CanvasGroup;
  33. }
  34. }
  35. private int CurrentScreenWidth
  36. {
  37. get
  38. {
  39. return Screen.width;
  40. }
  41. }
  42. private int CurrentScreenHeight
  43. {
  44. get
  45. {
  46. return Screen.height;
  47. }
  48. }
  49. private UIRoot uiRoot
  50. {
  51. get
  52. {
  53. if (this.m_UIRoot == null)
  54. {
  55. string text = GameMain.Instance.GetNowSceneName();
  56. if (string.IsNullOrEmpty(text))
  57. {
  58. text = SceneManager.GetActiveScene().name;
  59. }
  60. Scene sceneByName = SceneManager.GetSceneByName(text);
  61. if (!sceneByName.IsValid())
  62. {
  63. Debug.LogWarning(string.Format("シーン「{0}」は有効ではありません", text));
  64. return this.m_UIRoot;
  65. }
  66. if (!sceneByName.isLoaded)
  67. {
  68. Debug.Log("シーンロード中");
  69. return this.m_UIRoot;
  70. }
  71. GameObject[] rootGameObjects = sceneByName.GetRootGameObjects();
  72. GameObject gameObject = null;
  73. foreach (GameObject gameObject2 in rootGameObjects)
  74. {
  75. if (!(gameObject2.name != "UI Root"))
  76. {
  77. gameObject = gameObject2;
  78. break;
  79. }
  80. }
  81. if (gameObject == null)
  82. {
  83. return this.m_UIRoot;
  84. }
  85. UIRoot component = gameObject.GetComponent<UIRoot>();
  86. if (component == null)
  87. {
  88. Debug.LogWarning(string.Format("オブジェクト「{0}」にUIRootコンポーネントがありません", gameObject.name));
  89. return null;
  90. }
  91. this.m_UIRoot = component;
  92. }
  93. return this.m_UIRoot;
  94. }
  95. }
  96. private UIPanel uiPanel
  97. {
  98. get
  99. {
  100. if (this.m_UIPanel == null)
  101. {
  102. this.m_UIPanel = base.transform.GetComponentInParent<UIPanel>();
  103. }
  104. return this.m_UIPanel;
  105. }
  106. }
  107. private UICamera NGUI_UICamera
  108. {
  109. get
  110. {
  111. if (this.m_NGUI_UICamera == null && this.uiRoot)
  112. {
  113. this.m_NGUI_UICamera = this.uiRoot.GetComponentInChildren<UICamera>();
  114. }
  115. return this.m_NGUI_UICamera;
  116. }
  117. }
  118. private Camera SystemUICamera
  119. {
  120. get
  121. {
  122. if (this.m_SystemUICamera == null)
  123. {
  124. this.m_SystemUICamera = this.m_SystemUI_UICamera.GetComponent<Camera>();
  125. }
  126. return this.m_SystemUICamera;
  127. }
  128. }
  129. public bool IsHover
  130. {
  131. get
  132. {
  133. return this.m_IsRaycastHit;
  134. }
  135. }
  136. public int sortOrder
  137. {
  138. get
  139. {
  140. return this.m_SortOrder;
  141. }
  142. set
  143. {
  144. if (this.m_SortOrder != value || this.m_Canvas.sortingOrder != value)
  145. {
  146. this.m_Canvas.sortingOrder = value;
  147. this.m_SortOrder = value;
  148. }
  149. }
  150. }
  151. private void Start()
  152. {
  153. this.OnReset();
  154. }
  155. private void Update()
  156. {
  157. if (!this.canvasGroup.ignoreParentGroups && this.uiPanel != null && this.canvasGroup.alpha != this.uiPanel.alpha)
  158. {
  159. this.canvasGroup.alpha = this.uiPanel.alpha;
  160. }
  161. if (GameMain.Instance.MainCamera.IsFadeOut() || this.m_SystemUI_UICamera.Hover)
  162. {
  163. if (uGUIUtility.GetEventSystem() == null)
  164. {
  165. Debug.LogFormat("[uGUICanvas]EventSystem が見つからなかったので、「{0}」の子オブジェクトにEventSystemを作成します。", new object[]
  166. {
  167. base.name
  168. });
  169. GameObject gameObject = new GameObject();
  170. gameObject.AddComponent<EventSystem>();
  171. gameObject.AddComponent<StandaloneInputModule2>();
  172. gameObject.transform.SetParent(base.gameObject.transform, false);
  173. gameObject.name = "EventSystem";
  174. }
  175. uGUIUtility.SetActiveEventSystem(false);
  176. }
  177. else
  178. {
  179. if (uGUIUtility.GetEventSystem() == null)
  180. {
  181. Debug.LogFormat("[uGUICanvas]EventSystem が見つからなかったので、「{0}」の子オブジェクトにEventSystemを作成します。", new object[]
  182. {
  183. base.name
  184. });
  185. GameObject gameObject2 = new GameObject();
  186. gameObject2.AddComponent<EventSystem>();
  187. gameObject2.AddComponent<StandaloneInputModule2>();
  188. gameObject2.transform.SetParent(base.gameObject.transform, false);
  189. gameObject2.name = "EventSystem";
  190. }
  191. uGUIUtility.SetActiveEventSystem(true);
  192. }
  193. this.m_IsRaycastHit = this.IsPointerHit();
  194. if (this.NGUI_UICamera)
  195. {
  196. this.NGUI_UICamera.EnableProcess = (!this.m_SystemUI_UICamera.Hover && !this.m_IsRaycastHit);
  197. }
  198. }
  199. private bool IsPointerHit()
  200. {
  201. EventSystem eventSystem = uGUIUtility.GetEventSystem();
  202. if (eventSystem == null)
  203. {
  204. return false;
  205. }
  206. Vector2 position = Input.mousePosition;
  207. PointerEventData pointerEventData = new PointerEventData(eventSystem);
  208. pointerEventData.position = position;
  209. List<RaycastResult> list = new List<RaycastResult>();
  210. this.m_GraphicRaycaster.Raycast(pointerEventData, list);
  211. return list.Count > 0;
  212. }
  213. private void OnReset()
  214. {
  215. this.m_Canvas = base.GetComponent<Canvas>();
  216. if (!this.m_Canvas)
  217. {
  218. Debug.LogErrorFormat("[uGUICanvas]「{0}」には Canvas コンポーネントが存在しません", new object[]
  219. {
  220. base.name
  221. });
  222. }
  223. this.m_GraphicRaycaster = base.GetComponent<GraphicRaycaster>();
  224. if (!this.m_GraphicRaycaster)
  225. {
  226. Debug.LogErrorFormat("[uGUICanvas]「{0}」には GraphicRaycaster コンポーネントが存在しません", new object[]
  227. {
  228. base.name
  229. });
  230. }
  231. this.m_SystemUI_UICamera = GameMain.Instance.SysShortcut.GetComponentInParent<UIRoot>().GetComponentInChildren<UICamera>();
  232. if (this.m_SystemUI_UICamera == null)
  233. {
  234. Debug.LogError("システムUIルートが見つかりませんでした");
  235. }
  236. UIRoot componentInParent = this.m_Canvas.GetComponentInParent<UIRoot>();
  237. if (componentInParent == null)
  238. {
  239. NDebug.Warning(string.Format("{0}の親にUIRootがありません", base.name));
  240. }
  241. else
  242. {
  243. UICamera componentInChildren = componentInParent.GetComponentInChildren<UICamera>();
  244. if (componentInChildren == null)
  245. {
  246. NDebug.Warning(string.Format("{0}の子オブジェクトにUICameraがありません", componentInParent.name));
  247. }
  248. else
  249. {
  250. this.m_Canvas.renderMode = RenderMode.WorldSpace;
  251. this.m_Canvas.worldCamera = componentInChildren.GetComponent<Camera>();
  252. }
  253. }
  254. this.m_Canvas.planeDistance = 5f;
  255. uGUIUtility.SetLayer(base.gameObject, LayerMask.NameToLayer("NGUI"), true);
  256. }
  257. private void OnApplicationQuit()
  258. {
  259. this.m_IsQuittingApplication = true;
  260. }
  261. private void OnDestroy()
  262. {
  263. if (this.m_IsQuittingApplication)
  264. {
  265. return;
  266. }
  267. if (this.m_NGUI_UICamera)
  268. {
  269. this.m_NGUI_UICamera.EnableProcess = true;
  270. }
  271. }
  272. private void OnDisable()
  273. {
  274. if (this.m_IsQuittingApplication)
  275. {
  276. return;
  277. }
  278. if (this.m_NGUI_UICamera)
  279. {
  280. this.m_NGUI_UICamera.EnableProcess = true;
  281. }
  282. }
  283. [SerializeField]
  284. private Canvas m_Canvas;
  285. private RectTransform m_CanvasTransform;
  286. private CanvasGroup m_CanvasGroup;
  287. private UIRoot m_UIRoot;
  288. private UIPanel m_UIPanel;
  289. private UICamera m_NGUI_UICamera;
  290. private UICamera m_SystemUI_UICamera;
  291. private Camera m_SystemUICamera;
  292. private GraphicRaycaster m_GraphicRaycaster;
  293. private bool m_IsRaycastHit;
  294. [SerializeField]
  295. [Tooltip("uGUIキャンバスの描画順")]
  296. private int m_SortOrder = -1;
  297. private bool m_IsQuittingApplication;
  298. }