static public GameObject DuplicateObjectForPrefab(GameObject from)
    {
        GameObject newobj = null;

        if (from)
        {
            newobj = (GameObject)GameObject.Instantiate(from);

            if (newobj)
            {
                MegaModifyObject[] mobjs = newobj.GetComponentsInChildren <MegaModifyObject>();
                //MeshFilter[] mfil = newobj.GetComponentsInChildren<MeshFilter>();

                for (int i = 0; i < mobjs.Length; i++)
                {
                    Mesh m = MegaModifyObject.FindMesh(mobjs[i].gameObject);
                    if (m)
                    {
                        Mesh newmesh = DupMesh(m, mobjs[i]);
                        MegaModifyObject.SetMeshNew(mobjs[i].gameObject, newmesh);
                    }
                }

                //for ( int i = 0; i < mfil.Length; i++ )
                //mfil[i].sharedMesh = DupMesh(mfil[i].sharedMesh, mobjs[i]);

                //SkinnedMeshRenderer[] skin = newobj.GetComponentsInChildren<SkinnedMeshRenderer>();

                //for ( int i = 0; i < skin.Length; i++ )
                //skin[i].sharedMesh = DupMesh(skin[i].sharedMesh, mobjs[i]);

                for (int i = 0; i < mobjs.Length; i++)
                {
                    mobjs[i].MeshUpdated();
                }

                MegaModifier[] frommods = from.GetComponentsInChildren <MegaModifier>();
                MegaModifier[] tomods   = newobj.GetComponentsInChildren <MegaModifier>();

                for (int i = 0; i < frommods.Length; i++)
                {
                    tomods[i].PostCopy(frommods[i]);
                }

                MegaWrap[] wraps = newobj.GetComponentsInChildren <MegaWrap>();

                for (int i = 0; i < wraps.Length; i++)
                {
                    wraps[i].SetMesh();
                }

                newobj.name = from.name;                        // + " - Copy";
            }
        }

        return(newobj);
    }
Пример #2
0
    static void CreatePrefab()
    {
        if (Selection.activeGameObject != null)
        {
            if (!Directory.Exists("Assets/MegaPrefabs"))
            {
                AssetDatabase.CreateFolder("Assets", "MegaPrefabs");
            }

            GameObject obj = Selection.activeGameObject;

            // Make a copy?
            GameObject newobj = MegaCopyObject.DuplicateObjectForPrefab(obj);
            newobj.name = obj.name;

            // Get all modifyObjects in children
            MegaModifyObject[] mods = newobj.GetComponentsInChildren <MegaModifyObject>();

            int id = 0;

            for (int i = 0; i < mods.Length; i++)
            {
                // Need method to get the base mesh
                GameObject pobj = mods[i].gameObject;

                // Get the mesh and make an asset for it
                //Mesh mesh = MegaUtils.GetSharedMesh(pobj);
                GameObject inobj = null;
                Mesh       mesh  = MegaModifyObject.FindMesh(pobj, out inobj);


                if (mesh)
                {
                    if (!AssetDatabase.Contains(mesh))
                    {
                        string mname = mesh.name;
                        int    ix    = mname.IndexOf("Instance");
                        if (ix != -1)
                        {
                            mname = mname.Remove(ix);
                        }

                        string meshpath = "Assets/MegaPrefabs/" + mname + ".prefab";
                        id++;
                        AssetDatabase.CreateAsset(mesh, meshpath);
                        AssetDatabase.SaveAssets();
                        AssetDatabase.Refresh();
                    }
                }
            }

            MegaWrap[] wraps = newobj.GetComponentsInChildren <MegaWrap>();

            for (int i = 0; i < wraps.Length; i++)
            {
                // Need method to get the base mesh
                GameObject pobj = wraps[i].gameObject;

                // Get the mesh and make an asset for it
                //Mesh mesh = MegaUtils.GetSharedMesh(pobj);
                GameObject inobj = null;
                Mesh       mesh  = MegaModifyObject.FindMesh(pobj, out inobj);

                if (mesh)
                {
                    if (!AssetDatabase.Contains(mesh))
                    {
                        string mname = mesh.name;

                        int ix = mname.IndexOf("Instance");
                        if (ix != -1)
                        {
                            mname = mname.Remove(ix);
                        }

                        string meshpath = "Assets/MegaPrefabs/" + mname + ".prefab";
                        id++;
                        AssetDatabase.CreateAsset(mesh, meshpath);
                        AssetDatabase.SaveAssets();
                        AssetDatabase.Refresh();
                    }
                }
            }

            Object prefab = PrefabUtility.CreateEmptyPrefab("Assets/MegaPrefabs/" + newobj.name + "_Prefab.prefab");
            //EditorUtility.ReplacePrefab(newobj, prefab, ReplacePrefabOptions.ConnectToPrefab);
            PrefabUtility.ReplacePrefab(newobj, prefab, ReplacePrefabOptions.ConnectToPrefab);
            DestroyImmediate(newobj, true);
        }
    }