From be71b2387c8edf443717ff22c69ef64a0096ef59 Mon Sep 17 00:00:00 2001 From: in0finite Date: Sun, 16 Jan 2022 17:14:28 +0100 Subject: [PATCH] assign asset when created --- Assets/Scripts/Editor/AssetExporter.cs | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/Assets/Scripts/Editor/AssetExporter.cs b/Assets/Scripts/Editor/AssetExporter.cs index f0bc8807..999e7e3a 100644 --- a/Assets/Scripts/Editor/AssetExporter.cs +++ b/Assets/Scripts/Editor/AssetExporter.cs @@ -205,7 +205,7 @@ namespace SanAndreasUnity.Editor { MeshFilter meshFilter = meshFilters[i]; string indexPath = meshFilters.Length == 1 ? "" : "-" + i; - CreateAssetIfNotExists(meshFilter.sharedMesh, $"{ModelsPath}/{assetName}{indexPath}.asset"); + meshFilter.sharedMesh = (Mesh)CreateAssetIfNotExists(meshFilter.sharedMesh, $"{ModelsPath}/{assetName}{indexPath}.asset"); } var meshRenderers = go.GetComponentsInChildren(); @@ -220,7 +220,7 @@ namespace SanAndreasUnity.Editor for (int i = 0; i < meshColliders.Length; i++) { string indexPath = meshColliders.Length == 1 ? "" : "-" + i; - CreateAssetIfNotExists(meshColliders[i].sharedMesh, $"{CollisionModelsPath}/{assetName}{indexPath}.asset"); + meshColliders[i].sharedMesh = (Mesh)CreateAssetIfNotExists(meshColliders[i].sharedMesh, $"{CollisionModelsPath}/{assetName}{indexPath}.asset"); } //PrefabUtility.SaveAsPrefabAsset(go, $"{PrefabsPath}/{assetName}.prefab"); @@ -231,27 +231,30 @@ namespace SanAndreasUnity.Editor string indexPath = index.HasValue ? "-" + index.Value : ""; string assetName = rootGo.name + indexPath; - var mats = meshRenderer.sharedMaterials; + var mats = meshRenderer.sharedMaterials.ToArray(); for (int i = 0; i < mats.Length; i++) { var tex = mats[i].mainTexture; if (tex != null) - CreateAssetIfNotExists(tex, $"{TexturesPath}/{assetName}-{i}.asset"); - CreateAssetIfNotExists(mats[i], $"{MaterialsPath}/{assetName}-{i}.mat"); + mats[i].mainTexture = (Texture)CreateAssetIfNotExists(tex, $"{TexturesPath}/{assetName}-{i}.asset"); + mats[i] = (Material)CreateAssetIfNotExists(mats[i], $"{MaterialsPath}/{assetName}-{i}.mat"); } + + meshRenderer.sharedMaterials = mats; } - private static bool CreateAssetIfNotExists(Object asset, string path) + private static Object CreateAssetIfNotExists(Object asset, string path) { if (AssetDatabase.Contains(asset)) - return false; + return asset; if (File.Exists(Path.Combine(Application.dataPath + "/../", path))) - return false; + return AssetDatabase.LoadMainAssetAtPath(path); AssetDatabase.CreateAsset(asset, path); - return true; + + return AssetDatabase.LoadMainAssetAtPath(path); } } }