public static T OverwriteOrCreateAsset<T>(T asset, string path) where T : UnityEngine.Object { try { path = Misc.SanitizeFileName(path); // If it's an existing asset but the path doesn't match, make a copy of it for the new path: if (AssetDatabase.Contains(asset)) { var assetPath = Path.GetFullPath(AssetDatabase.GetAssetPath(asset)); var fullPath = Path.GetFullPath(path); if (assetPath != fullPath) { var newAsset = UnityEngine.Object.Instantiate(asset); newAsset.name = asset.name; asset = newAsset; } } // to keep meta, rewrite the existing one if already exists. T loadedAsset = AssetDatabase.LoadAssetAtPath<T>(path); if (loadedAsset != null) { EditorUtility.CopySerialized(asset, loadedAsset); return loadedAsset; } AssetDatabase.CreateAsset(asset, path); return asset; } catch (Exception e) { Debug.LogError($"[MeshSync] Error in saving asset at {path}. Msg: {e.ToString()}"); return null; } }
public static T OverwriteOrCreateAsset <T>(T asset, string path) where T : UnityEngine.Object { try { path = Misc.SanitizeFileName(path); // to keep meta, rewrite the existing one if already exists. T loadedAsset = AssetDatabase.LoadAssetAtPath <T>(path); if (loadedAsset != null) { EditorUtility.CopySerialized(asset, loadedAsset); return(loadedAsset); } AssetDatabase.CreateAsset(asset, path); return(asset); } catch (Exception e) { Debug.LogError($"[MeshSync] Error in saving asset at {path}. Msg: {e.ToString()}"); return(null); } }
// save asset to path. // if there is already a file, overwrite it but keep .meta intact. public static T SaveAsset <T>(T asset, string path) where T : UnityEngine.Object { try { path = Misc.SanitizeFileName(path); // to keep meta, rewrite the existing one if already exists. T loadedAsset = AssetDatabase.LoadAssetAtPath <T>(path); if (loadedAsset != null) { EditorUtility.CopySerialized(asset, loadedAsset); return(loadedAsset); } else { AssetDatabase.CreateAsset(asset, path); return(asset); } } catch (Exception) { return(null); } }