static void InitRoot() { GameObject go = new GameObject("UIRoot"); go.layer = LayerMask.NameToLayer("UI"); m_Instance = go.AddComponent <WTUIRoot>(); go.AddComponent <RectTransform>(); Canvas can = go.AddComponent <Canvas>(); can.renderMode = RenderMode.ScreenSpaceCamera; can.pixelPerfect = true; go.AddComponent <GraphicRaycaster>(); m_Instance.root = go.transform; GameObject camObj = new GameObject("UICamera"); camObj.layer = LayerMask.NameToLayer("UI"); camObj.transform.parent = go.transform; camObj.transform.localPosition = new Vector3(0, 0, -100f); Camera cam = camObj.AddComponent <Camera>(); cam.clearFlags = CameraClearFlags.Depth; cam.orthographic = true; cam.farClipPlane = 200f; can.worldCamera = cam; cam.cullingMask = 1 << 5; cam.nearClipPlane = -50f; cam.farClipPlane = 50f; m_Instance.uiCamera = cam; //add audio listener //camObj.AddComponent<AudioListener>(); camObj.AddComponent <GUILayer>(); CanvasScaler cs = go.AddComponent <CanvasScaler>(); cs.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize; cs.referenceResolution = new Vector2(1334f, 750f); cs.screenMatchMode = CanvasScaler.ScreenMatchMode.Expand; ////add auto scale camera fix size. //TTCameraScaler tcs = go.AddComponent<TTCameraScaler>(); //tcs.scaler = cs; //set the raycaster //GraphicRaycaster gr = go.AddComponent<GraphicRaycaster>(); GameObject subRoot; subRoot = CreateSubCanvasForRoot(go.transform, 0); subRoot.name = "NormalRoot"; m_Instance.normalRoot = subRoot.transform; m_Instance.normalRoot.transform.localScale = Vector3.one; subRoot = CreateSubCanvasForRoot(go.transform, 250); subRoot.name = "FixedRoot"; m_Instance.fixedRoot = subRoot.transform; m_Instance.fixedRoot.transform.localScale = Vector3.one; subRoot = CreateSubCanvasForRoot(go.transform, 500); subRoot.name = "PopupRoot"; m_Instance.popupRoot = subRoot.transform; m_Instance.popupRoot.transform.localScale = Vector3.one; //add Event System GameObject esObj = GameObject.Find("EventSystem"); if (esObj != null) { GameObject.DestroyImmediate(esObj); } GameObject eventObj = new GameObject("EventSystem"); eventObj.layer = LayerMask.NameToLayer("UI"); eventObj.transform.SetParent(go.transform); eventObj.AddComponent <EventSystem>(); eventObj.AddComponent <UnityEngine.EventSystems.StandaloneInputModule>(); }
void OnDestroy() { m_Instance = null; }