/// <summary> /// Create a new UI. /// </summary> static public UIPanel CreateUI(Transform trans, bool advanced3D, int layer) { // Find the existing UI Root UIRoot root = (trans != null) ? NGUITools.FindInParents <UIRoot>(trans.gameObject) : null; if (root == null && UIRoot.list.Count > 0) { root = UIRoot.list[0]; } // If no root found, create one if (root == null) { GameObject go = NGUITools.AddChild(null, false); root = go.AddComponent <UIRoot>(); // Automatically find the layers if none were specified if (layer == -1) { layer = LayerMask.NameToLayer("UI"); } if (layer == -1) { layer = LayerMask.NameToLayer("2D UI"); } go.layer = layer; if (advanced3D) { go.name = "UI Root (3D)"; root.scalingStyle = UIRoot.Scaling.FixedSize; } else { go.name = "UI Root"; root.scalingStyle = UIRoot.Scaling.PixelPerfect; } } // Find the first panel UIPanel panel = root.GetComponentInChildren <UIPanel>(); if (panel == null) { // Find other active cameras in the scene Camera[] cameras = NGUITools.FindActive <Camera>(); float depth = -1f; bool colorCleared = false; int mask = (1 << root.gameObject.layer); for (int i = 0; i < cameras.Length; ++i) { Camera c = cameras[i]; // If the color is being cleared, we won't need to if (c.clearFlags == CameraClearFlags.Color || c.clearFlags == CameraClearFlags.Skybox) { colorCleared = true; } // Choose the maximum depth depth = Mathf.Max(depth, c.depth); // Make sure this WcCamera can't see the UI c.cullingMask = (c.cullingMask & (~mask)); } // Create a WcCamera that will draw the UI Camera cam = NGUITools.AddChild <Camera>(root.gameObject, false); cam.gameObject.AddComponent <UICamera>(); cam.clearFlags = colorCleared ? CameraClearFlags.Depth : CameraClearFlags.Color; cam.backgroundColor = Color.grey; cam.cullingMask = mask; cam.depth = depth + 1f; if (advanced3D) { cam.nearClipPlane = 0.1f; cam.farClipPlane = 4f; cam.transform.localPosition = new Vector3(0f, 0f, -700f); } else { cam.orthographic = true; cam.orthographicSize = 1; cam.nearClipPlane = -10; cam.farClipPlane = 10; } // Make sure there is an audio listener present AudioListener[] listeners = NGUITools.FindActive <AudioListener>(); if (listeners == null || listeners.Length == 0) { cam.gameObject.AddComponent <AudioListener>(); } // Add a panel to the root panel = root.gameObject.AddComponent <UIPanel>(); #if UNITY_EDITOR UnityEditor.Selection.activeGameObject = panel.gameObject; #endif } if (trans != null) { // Find the root object while (trans.parent != null) { trans = trans.parent; } if (NGUITools.IsChild(trans, panel.transform)) { // Odd hierarchy -- can't reparent panel = trans.gameObject.AddComponent <UIPanel>(); } else { // Reparent this root object to be a child of the panel trans.parent = panel.transform; trans.localScale = Vector3.one; trans.localPosition = Vector3.zero; SetChildLayer(panel.cachedTransform, panel.cachedGameObject.layer); } } return(panel); }
/// <summary> /// Helper function that returns the selected root object. /// </summary> static public GameObject SelectedRoot(bool createIfMissing) { GameObject go = Selection.activeGameObject; // Only use active objects if (go != null && !NGUITools.GetActive(go)) { go = null; } // Try to find a panel UIPanel p = (go != null) ? NGUITools.FindInParents <UIPanel>(go) : null; // No selection? Try to find the root automatically if (p == null) { UIPanel[] panels = NGUITools.FindActive <UIPanel>(); if (panels.Length > 0) { go = panels[0].gameObject; } } // Now find the first uniformly scaled object if (go != null) { Transform t = go.transform; // Find the first uniformly scaled object while (!Mathf.Approximately(t.localScale.x, t.localScale.y) || !Mathf.Approximately(t.localScale.x, t.localScale.z)) { t = t.parent; if (t == null) { return((p != null) ? p.gameObject : null); } else { go = t.gameObject; } } } if (createIfMissing && go == null) { // No object specified -- find the first panel if (go == null) { UIPanel panel = GameObject.FindObjectOfType(typeof(UIPanel)) as UIPanel; if (panel != null) { go = panel.gameObject; } } // No UI present -- create a new one if (go == null) { go = UICreateNewUIWizard.CreateNewUI(); } } return(go); }
/// <summary> /// Mark all widgets associated with this atlas as having changed. /// </summary> public void MarkAsChanged() { #if UNITY_EDITOR NGUITools.SetDirty(gameObject); #endif //根据实际的使用情况,当前图集被修改了,不需要重新修改图集mReplacement. 2015.12.16 -- by gzg //if (mReplacement != null) mReplacement.MarkAsChanged(); if (mOwnerForm != null && !mOwnerForm.activeSelf) { return; } UISprite[] list = mOwnerForm == null?NGUITools.FindActive <UISprite>() : mOwnerForm.GetComponentsInChildren <UISprite>(); for (int i = 0, imax = list.Length; i < imax; ++i) { UISprite sp = list[i]; if (CheckIfRelated(this, sp.atlas)) { UIAtlas atl = sp.atlas; sp.atlas = null; sp.atlas = atl; #if UNITY_EDITOR NGUITools.SetDirty(sp); #endif } } UIFont[] fonts = mOwnerForm == null?Resources.FindObjectsOfTypeAll(typeof(UIFont)) as UIFont[] : new UIFont[0]; for (int i = 0, imax = fonts.Length; i < imax; ++i) { UIFont font = fonts[i]; if (CheckIfRelated(this, font.atlas)) { UIAtlas atl = font.atlas; font.atlas = null; font.atlas = atl; #if UNITY_EDITOR NGUITools.SetDirty(font); #endif } } UILabel[] labels = mOwnerForm == null?NGUITools.FindActive <UILabel>() : mOwnerForm.GetComponentsInChildren <UILabel>(); for (int i = 0, imax = labels.Length; i < imax; ++i) { UILabel lbl = labels[i]; if (lbl.bitmapFont != null && CheckIfRelated(this, lbl.bitmapFont.atlas)) { UIFont font = lbl.bitmapFont; lbl.bitmapFont = null; lbl.bitmapFont = font; #if UNITY_EDITOR NGUITools.SetDirty(lbl); #endif } } }
/// <summary> /// Mark all widgets associated with this atlas as having changed. /// </summary> public void MarkAsChanged() { #if UNITY_EDITOR NGUITools.SetDirty(this); #endif var rep = replacement; if (rep != null) { rep.MarkAsChanged(); } var list = NGUITools.FindActive <UISprite>(); for (int i = 0, imax = list.Length; i < imax; ++i) { var sp = list[i]; if (NGUITools.CheckIfRelated(this, sp.atlas)) { var atl = sp.atlas; sp.atlas = null; sp.atlas = atl; #if UNITY_EDITOR NGUITools.SetDirty(sp); #endif } } var f0 = Resources.FindObjectsOfTypeAll <NGUIFont>(); for (int i = 0, imax = f0.Length; i < imax; ++i) { var font = f0[i]; if (font.atlas == null) { continue; } if (NGUITools.CheckIfRelated(this, font.atlas)) { var atl = font.atlas; font.atlas = null; font.atlas = atl; #if UNITY_EDITOR NGUITools.SetDirty(font); #endif } } var f1 = Resources.FindObjectsOfTypeAll <UIFont>(); for (int i = 0, imax = f1.Length; i < imax; ++i) { var font = f1[i]; if (NGUITools.CheckIfRelated(this, font.atlas)) { var atl = font.atlas; font.atlas = null; font.atlas = atl; #if UNITY_EDITOR NGUITools.SetDirty(font); #endif } } var labels = NGUITools.FindActive <UILabel>(); for (int i = 0, imax = labels.Length; i < imax; ++i) { var lbl = labels[i]; if (lbl.atlas == null) { continue; } if (NGUITools.CheckIfRelated(this, lbl.atlas)) { var atl = lbl.atlas; var font = lbl.font; lbl.font = null; lbl.font = font; #if UNITY_EDITOR NGUITools.SetDirty(lbl); #endif } } }
/// <summary> /// Mark all widgets associated with this atlas as having changed. /// </summary> public void MarkAsChanged() { #if UNITY_EDITOR NGUITools.SetDirty(gameObject); #endif if (mReplacement != null) { mReplacement.MarkAsChanged(); } UISprite[] list = NGUITools.FindActive <UISprite>(); for (int i = 0, imax = list.Length; i < imax; ++i) { UISprite sp = list[i]; if (CheckIfRelated(this, sp.atlas)) { UIAtlas atl = sp.atlas; sp.atlas = null; sp.atlas = atl; #if UNITY_EDITOR NGUITools.SetDirty(sp); #endif } } UIFont[] fonts = Resources.FindObjectsOfTypeAll(typeof(UIFont)) as UIFont[]; for (int i = 0, imax = fonts.Length; i < imax; ++i) { UIFont font = fonts[i]; if (CheckIfRelated(this, font.atlas)) { UIAtlas atl = font.atlas; font.atlas = null; font.atlas = atl; #if UNITY_EDITOR NGUITools.SetDirty(font); #endif } } UILabel[] labels = NGUITools.FindActive <UILabel>(); for (int i = 0, imax = labels.Length; i < imax; ++i) { UILabel lbl = labels[i]; if (lbl.bitmapFont != null && CheckIfRelated(this, lbl.bitmapFont.atlas)) { UIFont font = lbl.bitmapFont; lbl.bitmapFont = null; lbl.bitmapFont = font; #if UNITY_EDITOR NGUITools.SetDirty(lbl); #endif } } }