Esempio n. 1
0
    static public void ImportLayout(GameObject layout_obj, string sPrefabPath)
    {
        GameObject prefab_obj;

#if UNITY_4_3 || UNITY_4_5 || UNITY_4_6
        prefab_obj = Resources.LoadAssetAtPath(sPrefabPath, typeof(GameObject)) as GameObject;
#else
        prefab_obj = AssetDatabase.LoadAssetAtPath(sPrefabPath, typeof(GameObject)) as GameObject;
#endif
        GameObject new_prefab_obj = Object.Instantiate(prefab_obj) as GameObject;
        Vector3    pos            = new_prefab_obj.transform.localPosition;
        Vector3    rotate         = new_prefab_obj.transform.localEulerAngles;
        Vector3    scale          = new_prefab_obj.transform.localScale;
        bool       need_reset_pos = LayoutTool.NeedResetPos(new_prefab_obj);

        if (!LayoutTool.ProcessBeforeImport(new_prefab_obj))
        {
            Object.DestroyImmediate(new_prefab_obj);
            Object.DestroyImmediate(layout_obj);
            return;
        }

        Camera      camera      = LayoutTool.CreateCamera();
        UIElement[] all_element = layout_obj.GetComponentsInChildren <UIElement>(true);

        if (need_reset_pos)
        {
            new_prefab_obj.transform.localPosition = Vector3.zero;
        }

        new_prefab_obj.transform.localEulerAngles = Vector3.zero;
        new_prefab_obj.transform.localScale       = Vector3.one;
        new_prefab_obj.name = prefab_obj.name;
        LayoutTool.SetCamera(new_prefab_obj, camera);

        for (int i = 0; i < all_element.Length; ++i)
        {
            if (all_element[i].FullPathName == "UIRootTempPanel")
            {
                continue;
            }

            bool     bHasUIWidget = true;
            UIWidget widget       = all_element[i].GetComponent <UIWidget>();
            if (widget == null)
            {
                bHasUIWidget = false;
            }

            List <GameObject> ui_list = null;
            if (bHasUIWidget)
            {
                ui_list = EditorTool.FindGameObjectByName(new_prefab_obj, widget.name);
            }
            else
            {
                ui_list = EditorTool.FindGameObjectByName(new_prefab_obj, all_element[i].Name);
            }

            if (ui_list.Count == 0)
            {
                if (bHasUIWidget)
                {
                    Debug.LogError("找不到节点\"" + widget.name + "\"");
                }
                else
                {
                    Debug.LogError("找不到节点\"" + all_element[i].Name + "\"");
                }
            }
            else if (ui_list.Count > 1)
            {
                GameObject child         = null;
                string     sFullPathName = all_element[i].FullPathName;
                if (sFullPathName.Length > 0)
                {
                    int nIndex = sFullPathName.IndexOf('/');
                    if (nIndex >= 0)
                    {
                        sFullPathName = sFullPathName.Substring(nIndex + 1);
                    }
                    child = EditorTool.FindGameObjectByPath(new_prefab_obj, sFullPathName);
                }

                if (child != null)
                {
                    if (bHasUIWidget)
                    {
                        if (!LayoutTool.LoadWidgetInfo(child, widget))
                        {
                            Debug.LogError("节点\"" + widget.name + "\"类型改变,无法导入数据");
                        }
                    }
                    else
                    {
                        LayoutTool.LoadNonWidgetTransformInfo(child, all_element[i]);
                    }
                }
                else
                {
                    if (bHasUIWidget)
                    {
                        Debug.LogError("节点名称\"" + widget.name + "\"不唯一");
                    }
                    else
                    {
                        Debug.LogError("节点名称\"" + all_element[i].Name + "\"不唯一");
                    }
                }
            }
            else
            {
                if (bHasUIWidget)
                {
                    if (!LayoutTool.LoadWidgetInfo(ui_list[0], widget))
                    {
                        Debug.LogError("节点\"" + widget.name + "\"类型改变,无法导入数据");
                    }
                }
                else
                {
                    LayoutTool.LoadNonWidgetTransformInfo(ui_list[0], all_element[i]);
                }
            }
        }

        if (need_reset_pos)
        {
            new_prefab_obj.transform.localPosition = pos;
        }

        new_prefab_obj.transform.localEulerAngles = rotate;
        new_prefab_obj.transform.localScale       = scale;
        PrefabUtility.ReplacePrefab(new_prefab_obj, prefab_obj);
        AssetDatabase.SaveAssets();
        LayoutTool.ReleaseCamera(camera);
        Object.DestroyImmediate(new_prefab_obj);
    }