123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327 |
- 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<RectTransform>();
- }
- return this.m_CanvasTransform;
- }
- }
- private CanvasGroup canvasGroup
- {
- get
- {
- if (this.m_CanvasGroup == null)
- {
- this.m_CanvasGroup = base.GetComponent<CanvasGroup>();
- if (this.m_CanvasGroup == null)
- {
- this.m_CanvasGroup = base.gameObject.AddComponent<CanvasGroup>();
- }
- }
- 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<UIRoot>();
- 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<UIPanel>();
- }
- return this.m_UIPanel;
- }
- }
- private UICamera NGUI_UICamera
- {
- get
- {
- if (this.m_NGUI_UICamera == null && this.uiRoot)
- {
- this.m_NGUI_UICamera = this.uiRoot.GetComponentInChildren<UICamera>();
- }
- return this.m_NGUI_UICamera;
- }
- }
- private Camera SystemUICamera
- {
- get
- {
- if (this.m_SystemUICamera == null)
- {
- this.m_SystemUICamera = this.m_SystemUI_UICamera.GetComponent<Camera>();
- }
- 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<EventSystem>();
- gameObject.AddComponent<StandaloneInputModule2>();
- 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<EventSystem>();
- gameObject2.AddComponent<StandaloneInputModule2>();
- gameObject2.transform.SetParent(base.gameObject.transform, false);
- gameObject2.name = "EventSystem";
- }
- uGUIUtility.SetActiveEventSystem(true);
- }
- this.m_IsRaycastHit = this.IsPointerHit();
- if (this.NGUI_UICamera)
- {
- this.NGUI_UICamera.EnableProcess = (!this.m_SystemUI_UICamera.Hover && !this.m_IsRaycastHit);
- }
- }
- private bool IsPointerHit()
- {
- EventSystem eventSystem = uGUIUtility.GetEventSystem();
- if (eventSystem == null)
- {
- return false;
- }
- Vector2 position = Input.mousePosition;
- PointerEventData pointerEventData = new PointerEventData(eventSystem);
- pointerEventData.position = position;
- List<RaycastResult> list = new List<RaycastResult>();
- this.m_GraphicRaycaster.Raycast(pointerEventData, list);
- return list.Count > 0;
- }
- private void OnReset()
- {
- this.m_Canvas = base.GetComponent<Canvas>();
- if (!this.m_Canvas)
- {
- Debug.LogErrorFormat("[uGUICanvas]「{0}」には Canvas コンポーネントが存在しません", new object[]
- {
- base.name
- });
- }
- this.m_GraphicRaycaster = base.GetComponent<GraphicRaycaster>();
- if (!this.m_GraphicRaycaster)
- {
- Debug.LogErrorFormat("[uGUICanvas]「{0}」には GraphicRaycaster コンポーネントが存在しません", new object[]
- {
- base.name
- });
- }
- this.m_SystemUI_UICamera = GameMain.Instance.SysShortcut.GetComponentInParent<UIRoot>().GetComponentInChildren<UICamera>();
- if (this.m_SystemUI_UICamera == null)
- {
- Debug.LogError("システムUIルートが見つかりませんでした");
- }
- UIRoot componentInParent = this.m_Canvas.GetComponentInParent<UIRoot>();
- if (componentInParent == null)
- {
- NDebug.Warning(string.Format("{0}の親にUIRootがありません", base.name));
- }
- else
- {
- UICamera componentInChildren = componentInParent.GetComponentInChildren<UICamera>();
- if (componentInChildren == null)
- {
- NDebug.Warning(string.Format("{0}の子オブジェクトにUICameraがありません", componentInParent.name));
- }
- else
- {
- this.m_Canvas.renderMode = RenderMode.WorldSpace;
- this.m_Canvas.worldCamera = componentInChildren.GetComponent<Camera>();
- }
- }
- 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;
- }
- }
- private void OnDisable()
- {
- if (this.m_IsQuittingApplication)
- {
- return;
- }
- if (this.m_NGUI_UICamera)
- {
- this.m_NGUI_UICamera.EnableProcess = true;
- }
- }
- [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;
- }
|