uGUICanvas.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. bool flag = this.IsPointerHit();
  194. if (this.NGUI_UICamera)
  195. {
  196. this.NGUI_UICamera.EnableProcess = (!this.m_SystemUI_UICamera.Hover && !flag);
  197. }
  198. if (!NInput.GetMouseButton(1) && !NInput.GetMouseButton(2))
  199. {
  200. if (flag && GameMain.Instance.MainCamera.IsFallThrough)
  201. {
  202. GameMain.Instance.MainCamera.gameObject.SendMessage("OnHover", false, SendMessageOptions.DontRequireReceiver);
  203. }
  204. else if (!flag && this.m_IsRaycastHit)
  205. {
  206. GameMain.Instance.MainCamera.gameObject.SendMessage("OnHover", true, SendMessageOptions.DontRequireReceiver);
  207. }
  208. }
  209. this.m_IsRaycastHit = flag;
  210. }
  211. private bool IsPointerHit()
  212. {
  213. EventSystem eventSystem = uGUIUtility.GetEventSystem();
  214. if (eventSystem == null)
  215. {
  216. return false;
  217. }
  218. Vector2 position = Input.mousePosition;
  219. PointerEventData pointerEventData = new PointerEventData(eventSystem);
  220. pointerEventData.position = position;
  221. foreach (GraphicRaycaster graphicRaycaster in base.GetComponentsInChildren<GraphicRaycaster>(false))
  222. {
  223. List<RaycastResult> list = new List<RaycastResult>();
  224. graphicRaycaster.Raycast(pointerEventData, list);
  225. if (list.Count > 0)
  226. {
  227. return true;
  228. }
  229. }
  230. return false;
  231. }
  232. private void OnReset()
  233. {
  234. this.m_Canvas = base.GetComponent<Canvas>();
  235. if (!this.m_Canvas)
  236. {
  237. Debug.LogErrorFormat("[uGUICanvas]「{0}」には Canvas コンポーネントが存在しません", new object[]
  238. {
  239. base.name
  240. });
  241. }
  242. this.m_GraphicRaycaster = base.GetComponent<GraphicRaycaster>();
  243. if (!this.m_GraphicRaycaster)
  244. {
  245. Debug.LogErrorFormat("[uGUICanvas]「{0}」には GraphicRaycaster コンポーネントが存在しません", new object[]
  246. {
  247. base.name
  248. });
  249. }
  250. this.m_SystemUI_UICamera = GameMain.Instance.SysShortcut.GetComponentInParent<UIRoot>().GetComponentInChildren<UICamera>();
  251. if (this.m_SystemUI_UICamera == null)
  252. {
  253. Debug.LogError("システムUIルートが見つかりませんでした");
  254. }
  255. UIRoot componentInParent = this.m_Canvas.GetComponentInParent<UIRoot>();
  256. if (componentInParent == null)
  257. {
  258. NDebug.Warning(string.Format("{0}の親にUIRootがありません", base.name));
  259. }
  260. else
  261. {
  262. UICamera componentInChildren = componentInParent.GetComponentInChildren<UICamera>();
  263. if (componentInChildren == null)
  264. {
  265. NDebug.Warning(string.Format("{0}の子オブジェクトにUICameraがありません", componentInParent.name));
  266. }
  267. else
  268. {
  269. this.m_Canvas.renderMode = RenderMode.WorldSpace;
  270. this.m_Canvas.worldCamera = componentInChildren.GetComponent<Camera>();
  271. }
  272. }
  273. this.m_Canvas.planeDistance = 5f;
  274. uGUIUtility.SetLayer(base.gameObject, LayerMask.NameToLayer("NGUI"), true);
  275. }
  276. private void OnApplicationQuit()
  277. {
  278. this.m_IsQuittingApplication = true;
  279. }
  280. private void OnDestroy()
  281. {
  282. if (this.m_IsQuittingApplication)
  283. {
  284. return;
  285. }
  286. if (this.m_NGUI_UICamera)
  287. {
  288. this.m_NGUI_UICamera.EnableProcess = true;
  289. }
  290. if (GameMain.Instance == null || GameMain.Instance.MainCamera == null || GameMain.Instance.MainCamera.gameObject == null)
  291. {
  292. return;
  293. }
  294. GameMain.Instance.MainCamera.gameObject.SendMessage("OnHover", true, SendMessageOptions.DontRequireReceiver);
  295. }
  296. private void OnDisable()
  297. {
  298. if (this.m_IsQuittingApplication)
  299. {
  300. return;
  301. }
  302. if (this.m_NGUI_UICamera)
  303. {
  304. this.m_NGUI_UICamera.EnableProcess = true;
  305. }
  306. if (GameMain.Instance == null || GameMain.Instance.MainCamera == null || GameMain.Instance.MainCamera.gameObject == null)
  307. {
  308. return;
  309. }
  310. GameMain.Instance.MainCamera.gameObject.SendMessage("OnHover", true, SendMessageOptions.DontRequireReceiver);
  311. }
  312. [SerializeField]
  313. private Canvas m_Canvas;
  314. private RectTransform m_CanvasTransform;
  315. private CanvasGroup m_CanvasGroup;
  316. private UIRoot m_UIRoot;
  317. private UIPanel m_UIPanel;
  318. private UICamera m_NGUI_UICamera;
  319. private UICamera m_SystemUI_UICamera;
  320. private Camera m_SystemUICamera;
  321. private GraphicRaycaster m_GraphicRaycaster;
  322. private bool m_IsRaycastHit;
  323. [SerializeField]
  324. [Tooltip("uGUIキャンバスの描画順")]
  325. private int m_SortOrder = -1;
  326. private bool m_IsQuittingApplication;
  327. }