Esempio n. 1
0
    void _bakeMeshesInPlace(MB2_MeshBakerCommon mom)
    {
        Mesh mesh;

        //MB2_MeshBakerCommon mom = (MB2_MeshBakerCommon) target;
        if (!MB_Utility.doCombinedValidate(mom, MB_ObjsToCombineTypes.prefabOnly))
        {
            return;
        }
        mom.DestroyMesh();

        List <GameObject> objsToMesh = mom.objsToMesh;

        if (mom.useObjsToMeshFromTexBaker && mom.GetComponent <MB2_TextureBaker>() != null)
        {
            objsToMesh = mom.GetComponent <MB2_TextureBaker>().objsToMesh;
        }

        GameObject[]  objs               = new GameObject[1];
        List <string> usedNames          = new List <string>();
        MB_RenderType originalRenderType = mom.renderType;

        for (int i = 0; i < objsToMesh.Count; i++)
        {
            if (objsToMesh[i] == null)
            {
                Debug.LogError("The " + i + "th object on the list of objects to combine is 'None'. Use Command-Delete on Mac OS X; Delete or Shift-Delete on Windows to remove this one element.");
                return;
            }
            objs[0] = objsToMesh[i];
            Renderer r = MB_Utility.GetRenderer(objsToMesh[i]);
            if (r is SkinnedMeshRenderer)
            {
                mom.renderType = MB_RenderType.skinnedMeshRenderer;
            }
            else
            {
                mom.renderType = MB_RenderType.meshRenderer;
            }
            mesh = mom.AddDeleteGameObjects(objs, null, false);
            if (mesh != null)
            {
                //mom.ApplyAll();
                mom.Apply();
                Mesh mf = MB_Utility.GetMesh(objs[0]);
                if (mf != null)
                {
                    string baseName, folderPath, newFilename;
                    string pth = AssetDatabase.GetAssetPath(mf);
                    if (pth != null && pth.Length != 0)
                    {
                        baseName   = System.IO.Path.GetFileNameWithoutExtension(pth) + "_" + objs[0].name + "_MB";
                        folderPath = System.IO.Path.GetDirectoryName(pth);
                    }
                    else                                           //try to get the name from prefab
                    {
                        pth = AssetDatabase.GetAssetPath(objs[0]); //get prefab name
                        if (pth != null && pth.Length != 0)
                        {
                            baseName   = System.IO.Path.GetFileNameWithoutExtension(pth) + "_" + objs[0].name + "_MB";
                            folderPath = System.IO.Path.GetDirectoryName(pth);
                        }
                        else                             //save in root
                        {
                            baseName   = objs[0].name + "mesh_MB";
                            folderPath = "Assets";
                        }
                    }
                    //make name unique
                    newFilename = System.IO.Path.Combine(folderPath, baseName + ".asset");
                    int j = 0;
                    while (usedNames.Contains(newFilename))
                    {
                        newFilename = System.IO.Path.Combine(folderPath, baseName + j + ".asset");
                        j++;
                    }
                    usedNames.Add(newFilename);
                    updateProgressBar("Created mesh saving mesh on " + objs[0].name + " to asset " + newFilename, .6f);
                    if (newFilename != null && newFilename.Length != 0)
                    {
                        Debug.Log("Creating mesh for " + objs[0].name + " with adjusted UVs at: " + newFilename);
                        AssetDatabase.CreateAsset(mesh, newFilename);
                    }
                    else
                    {
                        Debug.LogWarning("Could not save mesh for " + objs[0].name);
                    }
                }
            }
            mom.DestroyMesh();
        }
        mom.renderType = originalRenderType;
        return;
    }
Esempio n. 2
0
    /// <summary>
    /// We will modify the source object so duplicate them
    /// </summary>
    /// <param name="tempGameObjectInstances"></param>
    public static void _DuplicateSrcObjectInstancesAndUnpack(MB_RenderType renderType, GameObject[] objsToCombine, List <Transform> tempGameObjectInstances)
    {
        Debug.Assert(renderType == MB_RenderType.skinnedMeshRenderer, "RenderType must be Skinned Mesh Renderer");
        // first pass, collect the prefab-instance roots for each of the src objects.
        Transform[] sceneInstanceParents = new Transform[objsToCombine.Length];
        for (int i = 0; i < objsToCombine.Length; i++)
        {
            // Get the prefab root
            GameObject pr = null;
            {
                MB_PrefabType pt = MBVersionEditor.PrefabUtility_GetPrefabType(objsToCombine[i]);
                if (pt == MB_PrefabType.scenePefabInstance || pt == MB_PrefabType.isInstanceAndNotAPartOfAnyPrefab)
                {
                    pr = MBVersionEditor.PrefabUtility_GetPrefabInstanceRoot(objsToCombine[i]);
                }

                if (pr == null)
                {
                    pr = _FindCommonAncestorForBonesAnimatorAndSmr(objsToCombine[i]);
                }
            }

            sceneInstanceParents[i] = pr.transform;
        }

        // second pass, some of the parents could be children of other parents. ensure that we are
        // using the uppermost ancestor for all.
        for (int i = 0; i < objsToCombine.Length; i++)
        {
            sceneInstanceParents[i] = _FindUppermostParent(objsToCombine[i], sceneInstanceParents);
        }

        // Now build a map of sceneInstanceParents to the renderers contained beneath.
        Dictionary <Transform, List <Transform> > srcPrefabInstances2Renderers = new Dictionary <Transform, List <Transform> >();

        for (int i = 0; i < objsToCombine.Length; i++)
        {
            List <Transform> renderersUsed;
            if (!srcPrefabInstances2Renderers.TryGetValue(sceneInstanceParents[i], out renderersUsed))
            {
                renderersUsed = new List <Transform>();
                srcPrefabInstances2Renderers.Add(sceneInstanceParents[i], renderersUsed);
            }

            renderersUsed.Add(objsToCombine[i].transform);
        }

        // Duplicate the prefab-instance-root scene objects
        List <Transform> srcRoots  = new List <Transform>(srcPrefabInstances2Renderers.Keys);
        List <Transform> targRoots = new List <Transform>();

        for (int i = 0; i < srcRoots.Count; i++)
        {
            Transform  src = srcRoots[i];
            GameObject n   = GameObject.Instantiate <GameObject>(src.gameObject);
            n.transform.rotation   = src.rotation;
            n.transform.position   = src.position;
            n.transform.localScale = src.localScale;
            targRoots.Add(n.transform);
            tempGameObjectInstances.Add(targRoots[i]);
            _CheckSrcRootScale(renderType, src);
        }

        // Find the correct duplicated objsToCombine in the new instances that maps to objs in "objsToCombine".
        List <GameObject> newObjsToCombine = new List <GameObject>();

        for (int i = 0; i < srcRoots.Count; i++)
        {
            List <Transform> renderers = srcPrefabInstances2Renderers[srcRoots[i]];
            for (int j = 0; j < renderers.Count; j++)
            {
                Transform t = MB_BatchPrefabBakerEditorFunctions.FindCorrespondingTransform(srcRoots[i], renderers[j], targRoots[i]);
                Debug.Assert(!newObjsToCombine.Contains(t.gameObject));
                newObjsToCombine.Add(t.gameObject);
            }
        }
        Debug.Assert(newObjsToCombine.Count == objsToCombine.Length);

        for (int i = 0; i < newObjsToCombine.Count; i++)
        {
            //GameObject go = newObjsToCombine[i];
            //SerializedObject so = null;
            //MB_PrefabType pt = MBVersionEditor.GetPrefabType(go);
            //if (pt == MB_PrefabType.sceneInstance)
            //{
            //    MBVersionEditor.UnpackPrefabInstance(go, ref so);
            //}

            objsToCombine[i] = newObjsToCombine[i];
        }
    }