static void InitRoot() { GameObject go = new GameObject("UIRoot"); m_Instance = go.AddComponent<TTUIRoot>(); go.AddComponent<RectTransform>(); m_Instance.root = go.transform; Canvas can = go.AddComponent<Canvas>(); can.renderMode = RenderMode.ScreenSpaceCamera; can.pixelPerfect = true; GameObject camObj = new GameObject("Camera"); 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; CanvasScaler cs = go.AddComponent<CanvasScaler>(); cs.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize; cs.referenceResolution = new Vector2(1136f, 640f); cs.screenMatchMode = CanvasScaler.ScreenMatchMode.Expand; //set the raycaster //GraphicRaycaster gr = go.AddComponent<GraphicRaycaster>(); GameObject subRoot = CreateSubCanvasForRoot(go.transform,250); subRoot.name = "FixedRoot"; m_Instance.fixedRoot = subRoot.transform; subRoot = CreateSubCanvasForRoot(go.transform,0); subRoot.name = "NormalRoot"; m_Instance.normalRoot = subRoot.transform; subRoot = CreateSubCanvasForRoot(go.transform,500); subRoot.name = "PopupRoot"; m_Instance.popupRoot = subRoot.transform; }
static void InitRoot() { GameObject go = new GameObject("UIRoot"); go.layer = LayerMask.NameToLayer("UI"); m_Instance = go.AddComponent <TTUIRoot>(); 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(1024, 768); 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; }
static void InitRoot() { GameObject go = new GameObject("UIRoot"); go.layer = LayerMask.NameToLayer("UI"); m_Instance = go.AddComponent<TTUIRoot>(); go.AddComponent<RectTransform>(); m_Instance.root = go.transform; Canvas can = go.AddComponent<Canvas>(); can.renderMode = RenderMode.ScreenSpaceCamera; can.pixelPerfect = true; 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; m_Instance.uiCamera = cam; cam.cullingMask = 1<<5; cam.nearClipPlane = -50f; cam.farClipPlane = 50f; //add audio listener camObj.AddComponent<AudioListener>(); camObj.AddComponent<GUILayer>(); CanvasScaler cs = go.AddComponent<CanvasScaler>(); cs.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize; cs.referenceResolution = new Vector2(1136f, 640f); 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 = CreateSubCanvasForRoot(go.transform,250); subRoot.name = "FixedRoot"; m_Instance.fixedRoot = subRoot.transform; subRoot = CreateSubCanvasForRoot(go.transform,0); subRoot.name = "NormalRoot"; m_Instance.normalRoot = subRoot.transform; subRoot = CreateSubCanvasForRoot(go.transform,500); subRoot.name = "PopupRoot"; m_Instance.popupRoot = subRoot.transform; //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>(); if (!Application.isMobilePlatform || Application.isEditor) { eventObj.AddComponent<UnityEngine.EventSystems.StandaloneInputModule>(); } else { eventObj.AddComponent<UnityEngine.EventSystems.TouchInputModule>(); } }
static void InitRoot() { GameObject go = new GameObject("@UIRoot"); go.layer = LayerMask.NameToLayer("UI"); m_Instance = go.AddComponent <TTUIRoot>(); go.AddComponent <RectTransform>(); Canvas can = go.AddComponent <Canvas>(); can.renderMode = RenderMode.ScreenSpaceCamera; //can.renderMode = RenderMode.ScreenSpaceOverlay; can.pixelPerfect = false; can.planeDistance = 1; go.AddComponent <GraphicRaycaster>(); m_Instance.root = go.transform; GameObject camObj = new GameObject("@UICamera"); camObj.layer = LayerMask.NameToLayer("UI"); //测试出现的可能情况 //camObj.transform.parent = go.transform; go.transform.SetParent(camObj.transform, false); camObj.transform.localPosition = new Vector3(0, 100, -100f); Camera cam = camObj.AddComponent <Camera>(); cam.clearFlags = CameraClearFlags.Depth; cam.orthographic = true; cam.farClipPlane = 1000f; can.worldCamera = cam; cam.cullingMask = 1 << 5; cam.nearClipPlane = -2000f; cam.farClipPlane = 200f; cam.useOcclusionCulling = false; cam.depth = 50f; m_Instance.uiCamera = cam; //add audio listener //camObj.AddComponent<AudioListener>(); //camObj.AddComponent<GUILayer>(); CanvasScaler cs = go.AddComponent <CanvasScaler>(); cs.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize; Vector2 resolotion = new Vector2(1080f, 1920f);//new Vector2(Screen.width, Screen.height);// // iphonex 适配 //if (DeviceScreenRect.iPhoneX) //{ // //resolotion = new Vector2(1125f, 2436f); // //去除掉安全区域的px // //812 - 44 -44 ppi 2436 px // //---------------- = ------------- // // 812 ppi 2202 px // resolotion = new Vector2(1125f, 2202f); //} cs.referenceResolution = resolotion; cs.screenMatchMode = CanvasScaler.ScreenMatchMode.MatchWidthOrHeight; //2018.8.18 osmin 使用width适配,天然优势 // cs.matchWidthOrHeight = 0.5f; cs.matchWidthOrHeight = 0f; //set the raycaster //GraphicRaycaster gr = go.AddComponent<GraphicRaycaster>(); GameObject subRoot; // subRoot = CreateSubCanvasForRoot(go.transform, 0); subRoot = CreateSubCanvas(go.transform, 0); subRoot.name = "NormalRoot"; m_Instance.normalRoot = subRoot.transform as RectTransform; m_Instance.normalRoot.transform.localScale = Vector3.one; subRoot = CreateSubCanvas(go.transform, 2); subRoot.name = "FixedRoot"; m_Instance.fixedRoot = subRoot.transform as RectTransform; m_Instance.fixedRoot.transform.localScale = Vector3.one; subRoot = CreateSubCanvas(go.transform, 4); subRoot.name = "PopupRoot"; m_Instance.popupRoot = subRoot.transform as RectTransform; 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>(); // eventObj.AddComponent<UnityEngine.EventSystems.TouchInputModule>(); }
//当被销毁时,ui需要做什么 void OnDestroy() { m_Instance = null; TTUIPage.ClearNodes(); TTUIPage.ClearAllPage(); }