Exemplo n.º 1
0
        public static void BakeMesh(GameObject go, string path, bool useIt)
        {
            if (go == null || !path.StartsWith("Assets"))
            {
                return;
            }
            //save mesh
            //MeshFilter mf = go.GetComponentInChildren<MeshFilter>();
            MeshFilter mf = go.GetComponent <MeshFilter>();

            if (mf == null)
            {
                return;
            }

            mf.ToObjFileEx(path);
            //AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
            AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate | ImportAssetOptions.ForceSynchronousImport);

            ModelImporter mI = AssetImporter.GetAtPath(path) as ModelImporter;

            mI.importMaterials     = false;
            mI.importAnimation     = false;
            mI.generateSecondaryUV = true;
            mI.optimizeMesh        = false;
            mI.optimizeGameObjects = false;

            mI.animationType  = ModelImporterAnimationType.None;
            mI.importNormals  = ModelImporterNormals.Import;
            mI.importTangents = ModelImporterTangents.CalculateLegacyWithSplitTangents;

            AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate | ImportAssetOptions.ForceSynchronousImport);

            if (useIt)
            {
                Mesh mesh = AssetDatabase.LoadAssetAtPath(path, typeof(Mesh)) as Mesh;
                if (mesh != null)
                {
                    //if (mesh.uv2.Length == mf.sharedMesh.uv2.Length) {
                    mesh.uv2 = mf.sharedMesh.uv2;
                    //}
                    mf.sharedMesh = mesh;
                }
            }
        }
Exemplo n.º 2
0
        public static void BakeMesh(GameObject go, bool useIt)
        {
            if (go == null)
            {
                return;
            }
            if (!AssetDatabase.Contains(go))
            {
                go = PrefabUtility.GetPrefabParent(go) as GameObject;
                if (go == null)
                {
                    Debug.LogWarning(go.name + "is not an prefab!");
                    return;
                }
            }
            //save mesh
            MeshFilter mf = go.GetComponent <MeshFilter>();

            if (mf == null)
            {
                Debug.LogWarning(string.Format("{0} has no MeshFilter!", go.name));
                return;
            }

            string path = AssetDatabase.GetAssetPath(go);

            path = path.Replace(".prefab", "Mesh.obj");
            mf.ToObjFileEx(path);
            AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
            if (useIt)
            {
                Mesh mesh    = AssetDatabase.LoadAssetAtPath(path, typeof(Mesh)) as Mesh;
                Mesh oldMesh = mf.sharedMesh;
                if (mesh != null)
                {
                    mf.sharedMesh = mesh;
                }
                if (oldMesh != null)
                {
                    UnityEngine.Object.DestroyImmediate(oldMesh, true);
                }
                AssetDatabase.SaveAssets();
            }
        }