Beispiel #1
0
    static public GameObject AddChild(GameObject parent, GameObject prefab, bool checkPanel, string name = "")
    {
        GameObject go = GameObject.Instantiate(prefab) as GameObject;

        if (go != null && parent != null)
        {
            Transform t = go.transform;
            if (string.IsNullOrEmpty(name) == false)
            {
                t.name = name;
            }
            t.parent        = parent.transform;
            t.localRotation = Quaternion.identity;
            t.localScale    = Vector3.one;
            t.localPosition = Vector3.zero;
            if (checkPanel)
            {
                UIPanel panel = go.GetComponent <UIPanel>();
                if (panel != null)
                {
                    if (panel.DepthFlag == UIPanel.PanelDepthFlag.Top)
                    {
                        UIPanel.AddToTop(panel);
                    }
                    else if (panel.DepthFlag == UIPanel.PanelDepthFlag.Bottom)
                    {
                        UIPanel.AddToBottom(panel);
                    }
                    else if (panel.DepthFlag == UIPanel.PanelDepthFlag.Normal)
                    {
                        UIPanel.AddToNormal(panel);
                    }
                }
#if UNITY_EDITOR || UNITY_STANDALONE_WIN
                Transform t_p = parent.transform;

                UIPanel t_panel = null;
                while (t_p != null)
                {
                    t_panel = t_p.GetComponent <UIPanel>();
                    if (t_panel != null && t_panel.DepthFlag != UIPanel.PanelDepthFlag.None)
                    {
                        break;
                    }
                    t_p = t_p.parent;
                }

                if (t_panel != null)
                {
                    UIPanel[] panels = go.GetComponentsInChildren <UIPanel>(true);
                    for (int i = 0; i < panels.Length; i++)
                    {
                        if (panels[i].DepthFlag != UIPanel.PanelDepthFlag.None)
                        {
                            Debug.LogError("child panel couldn't contain non none property.");
                            break;
                        }
                    }
                }
#endif
            }
        }
        return(go);
    }