/// <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)
{
foreach (UIRoot r in UIRoot.list)
{
if (r.gameObject.layer == layer)
{
root = r;
break;
}
}
}
// Try to find an existing panel
if (root == null)
{
for (int i = 0, imax = UIPanel.list.Count; i < imax; ++i)
{
UIPanel p = UIPanel.list[i];
GameObject go = p.gameObject;
if (go.hideFlags == HideFlags.None && go.layer == layer)
{
trans.parent = p.transform;
trans.localScale = Vector3.one;
return p;
}
}
}
// If we are working with a different UI type, we need to treat it as a brand-new one instead
if (root != null)
{
UICamera cam = root.GetComponentInChildren<UICamera>();
#if UNITY_4_3 || UNITY_4_5 || UNITY_4_6
if (cam != null && cam.camera.isOrthoGraphic == advanced3D)
#else
if (cam != null && cam.GetComponent<Camera>().orthographic == advanced3D)
#endif
{
trans = null;
root = null;
}
}
// 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.Constrained;
}
else
{
go.name = "UI Root";
root.scalingStyle = UIRoot.Scaling.Flexible;
}
}
// 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 camera can't see the UI
c.cullingMask = (c.cullingMask & (~mask));
}
// Create a camera 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;
}