public void SaveAsAsset(UnityPath prefabPath) { ShowMeshes(); //var prefabPath = PrefabPath; if (prefabPath.IsFileExists) { // clear SubAssets foreach (var x in prefabPath.GetSubAssets().Where(x => !(x is GameObject) && !(x is Component))) { GameObject.DestroyImmediate(x, true); } } // // save sub assets // var paths = new List <UnityPath>() { prefabPath }; foreach (var o in ObjectsForSubAsset()) { if (o == null) { continue; } var assetPath = GetAssetPath(prefabPath, o); if (!assetPath.IsNull) { assetPath.Parent.EnsureFolder(); assetPath.CreateAsset(o); paths.Add(assetPath); } else { // save as subasset prefabPath.AddObjectToAsset(o); } } // Create or upate Main Asset if (prefabPath.IsFileExists) { Debug.LogFormat("replace prefab: {0}", prefabPath); var prefab = prefabPath.LoadAsset <GameObject>(); PrefabUtility.ReplacePrefab(Root, prefab, ReplacePrefabOptions.ReplaceNameBased); } else { Debug.LogFormat("create prefab: {0}", prefabPath); PrefabUtility.CreatePrefab(prefabPath.Value, Root); } foreach (var x in paths) { x.ImportAsset(); } }
public void SaveAsAsset(UnityPath prefabPath) { ShowMeshes(); //var prefabPath = PrefabPath; if (prefabPath.IsFileExists) { // clear SubAssets foreach (var x in prefabPath.GetSubAssets().Where(x => !(x is GameObject) && !(x is Component))) { GameObject.DestroyImmediate(x, true); } } // // save sub assets // var paths = new List <UnityPath>() { prefabPath }; foreach (var o in ObjectsForSubAsset()) { if (o == null) { continue; } var assetPath = GetAssetPath(prefabPath, o); if (!assetPath.IsNull) { if (assetPath.IsFileExists) { if (AvoidOverwriteAndLoad(assetPath, o)) { // 上書きせずに既存のアセットからロードして置き換えた continue; } } // アセットとして書き込む assetPath.Parent.EnsureFolder(); assetPath.CreateAsset(o); paths.Add(assetPath); } else { // save as subasset prefabPath.AddObjectToAsset(o); } } // Create or update Main Asset if (prefabPath.IsFileExists) { Debug.LogFormat("replace prefab: {0}", prefabPath); var prefab = prefabPath.LoadAsset <GameObject>(); #if UNITY_2018_3_OR_NEWER PrefabUtility.SaveAsPrefabAssetAndConnect(Root, prefabPath.Value, InteractionMode.AutomatedAction); #else PrefabUtility.ReplacePrefab(Root, prefab, ReplacePrefabOptions.ReplaceNameBased); #endif } else { Debug.LogFormat("create prefab: {0}", prefabPath); #if UNITY_2018_3_OR_NEWER PrefabUtility.SaveAsPrefabAssetAndConnect(Root, prefabPath.Value, InteractionMode.AutomatedAction); #else PrefabUtility.CreatePrefab(prefabPath.Value, Root); #endif } foreach (var x in paths) { x.ImportAsset(); } }
public void SaveAsAsset(UnityPath prefabPath) { ShowMeshes(); //var prefabPath = PrefabPath; if (prefabPath.IsFileExists) { // clear SubAssets foreach (var x in prefabPath.GetSubAssets().Where(x => !(x is GameObject) && !(x is Component))) { GameObject.DestroyImmediate(x, true); } } // Add SubAsset var materialDir = prefabPath.GetAssetFolder(".Materials"); materialDir.EnsureFolder(); var textureDir = prefabPath.GetAssetFolder(".Textures"); textureDir.EnsureFolder(); var meshDir = prefabPath.GetAssetFolder(".Meshes"); if (!MeshAsSubAsset) { meshDir.EnsureFolder(); } var paths = new List <UnityPath>() { prefabPath }; foreach (var o in ObjectsForSubAsset()) { if (o is Material) { var materialPath = materialDir.Child(o.name.EscapeFilePath() + ".asset"); materialPath.CreateAsset(o); paths.Add(materialPath); } else if (o is Texture2D) { var texturePath = textureDir.Child(o.name.EscapeFilePath() + ".asset"); texturePath.CreateAsset(o); paths.Add(texturePath); } else if (o is Mesh && !MeshAsSubAsset) { var meshPath = meshDir.Child(o.name.EscapeFilePath() + ".asset"); meshPath.CreateAsset(o); paths.Add(meshPath); } else { // save as subasset prefabPath.AddObjectToAsset(o); } } // Create or upate Main Asset if (prefabPath.IsFileExists) { Debug.LogFormat("replace prefab: {0}", prefabPath); var prefab = prefabPath.LoadAsset <GameObject>(); PrefabUtility.ReplacePrefab(Root, prefab, ReplacePrefabOptions.ReplaceNameBased); } else { Debug.LogFormat("create prefab: {0}", prefabPath); PrefabUtility.CreatePrefab(prefabPath.Value, Root); } foreach (var x in paths) { x.ImportAsset(); } }