public static bool IsAnyUnsavedMeshInHierarchy(GameObject go) { bool isAnyUnsavedMesh = false; GameObject[] arrGameObjects = CREditorUtils.GetAllChildObjectsWithGeometry(go, true); int nGameObjects = arrGameObjects.Length; for (int i = 0; i < nGameObjects; i++) { UnityEngine.GameObject gameObject = arrGameObjects[i]; UnityEngine.Mesh mesh = gameObject.GetMesh(); if (mesh != null && !AssetDatabase.Contains(mesh.GetInstanceID())) { isAnyUnsavedMesh = true; } } return(isAnyUnsavedMesh); }
public static void SaveAnyUnsavedMeshInHierarchy(GameObject go, bool saveAsCopy) { string path = EditorUtility.SaveFilePanelInProject("Save unsaved meshes into Assets...", go.name + ".prefab", "prefab", "Please, enter a name where the unsaved meshes will be saved to"); if (path.Length != 0) { string meshesPrefabPath = AssetDatabase.GenerateUniqueAssetPath(path); Object meshesPrefab = PrefabUtility.CreateEmptyPrefab(meshesPrefabPath); GameObject[] arrGameObjects = CREditorUtils.GetAllChildObjectsWithGeometry(go, true); int nGameObjects = arrGameObjects.Length; for (int i = 0; i < nGameObjects; i++) { UnityEngine.GameObject gameObject = arrGameObjects[i]; UnityEngine.Mesh mesh = gameObject.GetMesh(); if (mesh != null && !AssetDatabase.Contains(mesh.GetInstanceID())) { if (saveAsCopy) { Mesh newMesh = UnityEngine.Object.Instantiate(mesh); newMesh.name = mesh.name; CREditorUtils.SetMesh(gameObject, newMesh); AssetDatabase.AddObjectToAsset(newMesh, meshesPrefab); } else { AssetDatabase.AddObjectToAsset(mesh, meshesPrefab); } EditorUtility.DisplayProgressBar("Saving scene meshes to prefab...", "Saving mesh " + i + 1 + " of " + nGameObjects, (float)i + 1 / (float)nGameObjects); } } EditorUtility.ClearProgressBar(); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } }