protected static Transform GetParent(string bigType, string smallType) { var uri = GetUri(bigType, smallType); Transform theParent; if (!Parents.TryGetValue(uri, out theParent)) { var bigTypeObjName = string.Format("__{0}__", bigType); var bigTypeObj = GameObject.Find(bigTypeObjName) ?? new GameObject(bigTypeObjName); GameObject.DontDestroyOnLoad(bigTypeObj); theParent = new GameObject(smallType).transform; KTool.SetChild(theParent, bigTypeObj.transform); Parents[uri] = theParent; } return(theParent); }
/// <summary> /// 设置某个物件,在指定调试组下 /// </summary> /// <param name="bigType"></param> /// <param name="smallType"></param> /// <param name="obj"></param> public static void SetParent(string bigType, string smallType, GameObject obj) { var uri = GetUri(bigType, smallType); Transform theParent = GetParent(bigType, smallType); int typeCount; if (!Counts.TryGetValue(uri, out typeCount)) { Counts[uri] = 0; } typeCount = ++Counts[uri]; try { KTool.SetChild(obj, theParent.gameObject); } catch (Exception e) { Logger.LogError(string.Format("[SetParent]{0}->{1}->{2}", bigType, smallType, e.Message)); } theParent.gameObject.name = GetNameWithCount(smallType, typeCount); }
//void CreateUGUI() //{ // var canvasObj = new GameObject("UICanvas"); // UICanvas = canvasObj.AddComponent<Canvas>(); // UICanvas.renderMode = RenderMode.ScreenSpaceOverlay; // //UICanvas.worldCamera = UiCamera.cachedCamera; // canvasObj.AddComponent<GraphicRaycaster>(); // var scaler = canvasObj.AddComponent<CanvasScaler>(); // scaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize; // 屏幕固定大小 // scaler.referenceResolution = new Vector2(1080, 1920); // var evtSysObj = new GameObject("EventSystem"); // KTool.SetChild(evtSysObj, canvasObj); // evtSysObj.AddComponent<EventSystem>(); //} private void CreateUIRoot() { GameObject uiRootobj = GameObject.Find("NGUIRoot") ?? new GameObject("NGUIRoot"); UiRoot = uiRootobj.GetComponent <UIRoot>() ?? uiRootobj.AddComponent <UIRoot>(); Debuger.Assert(UiRoot); UiRoot.scalingStyle = UIRoot.Scaling.FixedSizeOnMobiles; // 尝试将NGUI转化成跟2dToolkit镜头一致显示 UiRoot.manualHeight = 1080; //GameDef.ScreenPixelY;//(int)(GameDef.ScreenPixelY / (GameDef.ScreenPixelY / 2f / GameDef.DefaultPixelPerMeters)); // fit width! //UiRoot.manualWidth = 1920;//GameDef.ScreenPixelX; // 屏幕中间位置 //UiRoot.transform.localPosition = new Vector3(GameDef.ScreenPixelX / 2f / GameDef.DefaultPixelPerMeters, // GameDef.ScreenPixelY / 2f / GameDef.DefaultPixelPerMeters, -50); //var scale = 1 / GameDef.DefaultPixelPerMeters; //// 覆盖NGUI的Uiroot自动缩放 //UiRoot.transform.localScale = new Vector3(scale, scale, scale); //UiRoot.enabled = false; GameObject panelRootObj = new GameObject("PanelRoot"); KTool.SetChild(panelRootObj.transform, uiRootobj.transform); Transform panelTrans = panelRootObj.transform; PanelRoot = panelRootObj.AddComponent <UIPanel>(); Debuger.Assert(PanelRoot); PanelRoot.generateNormals = true; var uiCamTrans = uiRootobj.transform.Find("UICamera"); GameObject uiCamObj = uiCamTrans != null ? uiCamTrans.gameObject : new GameObject("UICamera"); KTool.SetChild(uiCamObj.transform, UiRoot.transform); UiCamera = uiCamObj.GetComponent <UICamera>() ?? uiCamObj.AddComponent <UICamera>(); UiCamera.cachedCamera.cullingMask = 1 << (int)UnityLayerDef.UI; UiCamera.cachedCamera.clearFlags = CameraClearFlags.Depth; UiCamera.cachedCamera.orthographic = true; UiCamera.cachedCamera.orthographicSize = GameDef.ScreenPixelY / GameDef.DefaultPixelPerMeters / 2f; // 9.6,一屏19.2米,跟GameCamera一致 UiCamera.cachedCamera.nearClipPlane = -500; UiCamera.cachedCamera.farClipPlane = 500; foreach (UIAnchor.Side side in Enum.GetValues(typeof(UIAnchor.Side))) { GameObject anchorObj = new GameObject(side.ToString()); KTool.SetChild(anchorObj.transform, panelTrans); AnchorSide[side.ToString()] = anchorObj.transform; } GameObject nullAnchor = new GameObject("Null"); KTool.SetChild(nullAnchor.transform, panelTrans); AnchorSide["Null"] = nullAnchor.transform; AnchorSide[""] = AnchorSide[UIAnchor.Side.Center.ToString()]; // default NGUITools.SetLayer(uiRootobj, (int)UnityLayerDef.UI); PressWidget = new GameObject("PressWidget").AddComponent <UIWidget>(); NGUITools.SetLayer(PressWidget.gameObject, (int)UnityLayerDef.UI); KTool.SetChild(PressWidget.gameObject, panelRootObj); PressWidget.SetDimensions(2000, 2000); var col = PressWidget.gameObject.AddComponent <BoxCollider>(); col.size = new Vector3(2000, 2000); PressWidget.autoResizeBoxCollider = true; PressWidget.gameObject.SetActive(false); //UICamera.onDragStart = (go) => //{ // if (go != null) // 点击任意NGUI控件,出现阻挡 // PressWidget.gameObject.SetActive(true); //}; //UICamera.onPress = (go, state) => //{ // if (!state) // 点击任意NGUI控件,出现阻挡 // PressWidget.gameObject.SetActive(false); // //if (go != null) // //{ // // if (go.GetComponent<UIButton>() == null) // // { // // if (go.GetComponent<UIEventListener>() != null && go.GetComponent<UISprite>() != null) // // { // // if (Debug.isDebugBuild) // // { // // Debug.LogWarning("自动加UIButton和ButtonScale - " + go.name, go); // // } // // // 当不包含ButtonScale动画 // // // 并且拥有EventListener和UISprite! // // // 统一加上ButtonScale! // // var bScale = go.GetComponent<UIButtonScale>() ?? go.AddComponent<UIButtonScale>(); // // bScale.pressed = bScale.hover; // // bScale.hover = Vector3.one; // // var bColor = go.GetComponent<UIButton>() ?? go.AddComponent<UIButton>(); // // bColor.hover = Color.white; // // bColor.pressed = Color.white*.8f; // 小小灰 // // } // // } // //} //}; }