using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.SceneManagement; using UnityEngine.UI; public class uGUICanvas : MonoBehaviour { private RectTransform CanvasTransform { get { if (this.m_CanvasTransform == null) { this.m_CanvasTransform = this.m_Canvas.GetComponent(); } return this.m_CanvasTransform; } } private CanvasGroup canvasGroup { get { if (this.m_CanvasGroup == null) { this.m_CanvasGroup = base.GetComponent(); if (this.m_CanvasGroup == null) { this.m_CanvasGroup = base.gameObject.AddComponent(); } } return this.m_CanvasGroup; } } private int CurrentScreenWidth { get { return Screen.width; } } private int CurrentScreenHeight { get { return Screen.height; } } private UIRoot uiRoot { get { if (this.m_UIRoot == null) { string text = GameMain.Instance.GetNowSceneName(); if (string.IsNullOrEmpty(text)) { text = SceneManager.GetActiveScene().name; } Scene sceneByName = SceneManager.GetSceneByName(text); if (!sceneByName.IsValid()) { Debug.LogWarning(string.Format("シーン「{0}」は有効ではありません", text)); return this.m_UIRoot; } if (!sceneByName.isLoaded) { Debug.Log("シーンロード中"); return this.m_UIRoot; } GameObject[] rootGameObjects = sceneByName.GetRootGameObjects(); GameObject gameObject = null; foreach (GameObject gameObject2 in rootGameObjects) { if (!(gameObject2.name != "UI Root")) { gameObject = gameObject2; break; } } if (gameObject == null) { return this.m_UIRoot; } UIRoot component = gameObject.GetComponent(); if (component == null) { Debug.LogWarning(string.Format("オブジェクト「{0}」にUIRootコンポーネントがありません", gameObject.name)); return null; } this.m_UIRoot = component; } return this.m_UIRoot; } } private UIPanel uiPanel { get { if (this.m_UIPanel == null) { this.m_UIPanel = base.transform.GetComponentInParent(); } return this.m_UIPanel; } } private UICamera NGUI_UICamera { get { if (this.m_NGUI_UICamera == null && this.uiRoot) { this.m_NGUI_UICamera = this.uiRoot.GetComponentInChildren(); } return this.m_NGUI_UICamera; } } private Camera SystemUICamera { get { if (this.m_SystemUICamera == null) { this.m_SystemUICamera = this.m_SystemUI_UICamera.GetComponent(); } return this.m_SystemUICamera; } } public bool IsHover { get { return this.m_IsRaycastHit; } } public int sortOrder { get { return this.m_SortOrder; } set { if (this.m_SortOrder != value || this.m_Canvas.sortingOrder != value) { this.m_Canvas.sortingOrder = value; this.m_SortOrder = value; } } } private void Start() { this.OnReset(); } private void Update() { if (!this.canvasGroup.ignoreParentGroups && this.uiPanel != null && this.canvasGroup.alpha != this.uiPanel.alpha) { this.canvasGroup.alpha = this.uiPanel.alpha; } if (GameMain.Instance.MainCamera.IsFadeOut() || this.m_SystemUI_UICamera.Hover) { if (uGUIUtility.GetEventSystem() == null) { Debug.LogFormat("[uGUICanvas]EventSystem が見つからなかったので、「{0}」の子オブジェクトにEventSystemを作成します。", new object[] { base.name }); GameObject gameObject = new GameObject(); gameObject.AddComponent(); gameObject.AddComponent(); gameObject.transform.SetParent(base.gameObject.transform, false); gameObject.name = "EventSystem"; } uGUIUtility.SetActiveEventSystem(false); } else { if (uGUIUtility.GetEventSystem() == null) { Debug.LogFormat("[uGUICanvas]EventSystem が見つからなかったので、「{0}」の子オブジェクトにEventSystemを作成します。", new object[] { base.name }); GameObject gameObject2 = new GameObject(); gameObject2.AddComponent(); gameObject2.AddComponent(); gameObject2.transform.SetParent(base.gameObject.transform, false); gameObject2.name = "EventSystem"; } uGUIUtility.SetActiveEventSystem(true); } bool flag = this.IsPointerHit(); if (this.NGUI_UICamera) { this.NGUI_UICamera.EnableProcess = (!this.m_SystemUI_UICamera.Hover && !flag); } if (!NInput.GetMouseButton(1) && !NInput.GetMouseButton(2)) { if (flag && GameMain.Instance.MainCamera.IsFallThrough) { GameMain.Instance.MainCamera.gameObject.SendMessage("OnHover", false, SendMessageOptions.DontRequireReceiver); } else if (!flag && this.m_IsRaycastHit) { GameMain.Instance.MainCamera.gameObject.SendMessage("OnHover", true, SendMessageOptions.DontRequireReceiver); } } this.m_IsRaycastHit = flag; } private bool IsPointerHit() { EventSystem eventSystem = uGUIUtility.GetEventSystem(); if (eventSystem == null) { return false; } Vector2 position = Input.mousePosition; PointerEventData pointerEventData = new PointerEventData(eventSystem); pointerEventData.position = position; foreach (GraphicRaycaster graphicRaycaster in base.GetComponentsInChildren(false)) { List list = new List(); graphicRaycaster.Raycast(pointerEventData, list); if (list.Count > 0) { return true; } } return false; } private void OnReset() { this.m_Canvas = base.GetComponent(); if (!this.m_Canvas) { Debug.LogErrorFormat("[uGUICanvas]「{0}」には Canvas コンポーネントが存在しません", new object[] { base.name }); } this.m_GraphicRaycaster = base.GetComponent(); if (!this.m_GraphicRaycaster) { Debug.LogErrorFormat("[uGUICanvas]「{0}」には GraphicRaycaster コンポーネントが存在しません", new object[] { base.name }); } this.m_SystemUI_UICamera = GameMain.Instance.SysShortcut.GetComponentInParent().GetComponentInChildren(); if (this.m_SystemUI_UICamera == null) { Debug.LogError("システムUIルートが見つかりませんでした"); } UIRoot componentInParent = this.m_Canvas.GetComponentInParent(); if (componentInParent == null) { NDebug.Warning(string.Format("{0}の親にUIRootがありません", base.name)); } else { UICamera componentInChildren = componentInParent.GetComponentInChildren(); if (componentInChildren == null) { NDebug.Warning(string.Format("{0}の子オブジェクトにUICameraがありません", componentInParent.name)); } else { this.m_Canvas.renderMode = RenderMode.WorldSpace; this.m_Canvas.worldCamera = componentInChildren.GetComponent(); } } this.m_Canvas.planeDistance = 5f; uGUIUtility.SetLayer(base.gameObject, LayerMask.NameToLayer("NGUI"), true); } private void OnApplicationQuit() { this.m_IsQuittingApplication = true; } private void OnDestroy() { if (this.m_IsQuittingApplication) { return; } if (this.m_NGUI_UICamera) { this.m_NGUI_UICamera.EnableProcess = true; } if (GameMain.Instance == null || GameMain.Instance.MainCamera == null || GameMain.Instance.MainCamera.gameObject == null) { return; } GameMain.Instance.MainCamera.gameObject.SendMessage("OnHover", true, SendMessageOptions.DontRequireReceiver); } private void OnDisable() { if (this.m_IsQuittingApplication) { return; } if (this.m_NGUI_UICamera) { this.m_NGUI_UICamera.EnableProcess = true; } if (GameMain.Instance == null || GameMain.Instance.MainCamera == null || GameMain.Instance.MainCamera.gameObject == null) { return; } GameMain.Instance.MainCamera.gameObject.SendMessage("OnHover", true, SendMessageOptions.DontRequireReceiver); } [SerializeField] private Canvas m_Canvas; private RectTransform m_CanvasTransform; private CanvasGroup m_CanvasGroup; private UIRoot m_UIRoot; private UIPanel m_UIPanel; private UICamera m_NGUI_UICamera; private UICamera m_SystemUI_UICamera; private Camera m_SystemUICamera; private GraphicRaycaster m_GraphicRaycaster; private bool m_IsRaycastHit; [SerializeField] [Tooltip("uGUIキャンバスの描画順")] private int m_SortOrder = -1; private bool m_IsQuittingApplication; }