- Home /
AssetDatabase: Replacing an asset, but leaving reference intact
I am creating a mesh via a custom import script. After generating the mesh I am using AssetDatabes.CreateAsset() to write that to disk as a .asset file. All of that works just fine.
However, when this process runs again, CreateAsset() seems to explicitly delete the previous asset prior to putting the newly created one there, thereby invalidating any reference to that asset that is on other components.
Is there anyway to REPLACE the asset instead of creating it anew. Essentially I'm looking for functionality similar to ReplacePrefab()
-ryan
Not sure if this is relevant to your situation: http://unity3d.com/support/documentation/ScriptReference/AssetDatabase.LoadAssetAtPath.html
Wondering about that exact question. I tried replacing the asset and replacing the original .meta file, but it didn't do any good.
Please post if you've found an answer, otherwise.... BU$$anonymous$$P
Answer by Kencho · Jul 20, 2013 at 04:28 AM
I've found this method that apparently may work to clone any UnityEngine.Object
asset:
AnimationClip outputAnimClip = AssetDatabase.LoadMainAssetAtPath (path) as AnimationClip;
if (outputAnimClip != null) {
EditorUtility.CopySerialized (animClip, outputAnimClip);
AssetDatabase.SaveAssets ();
}
else {
outputAnimClip = new AnimationClip ();
EditorUtility.CopySerialized (animClip, outputAnimClip);
AssetDatabase.CreateAsset (outputAnimClip, path);
}
That snippet clones animClip
into outputAnimClip
. If the target asset doesn't exist at path
, it will be created; otherwise, it's copied and replaced, but the links are maintained.
First, it gets a reference to the asset at path
. If the reference is not null
, the asset already exists and it simply replaces it by copying the contents of animClip
into outputAnimClip
(which references the existing asset), and saving all the assets. If the reference is null
, then the asset doesn't exist, creates a new animation clip, copies the contents, and creates an asset using that object outputAnimClip
and the given path
.
I guess this will work with any other subclass of UnityEngine.Object
, although they would require a different treatment (specially in their instantiation). Maybe this could be rewritten as a generic function requiring the parameter T to be a subclass of UnityEngine.Object
and have a parameterless constructor.
This question is fairly old, I know, but as there's yet no official implementation and no generic answer, yet some people may find it useful, I'm leaving this here. Hope it helps! :)
Worked for me! In the else clause, you can directly use:
AssetDatabase.CreateAsset (animClip, path);
and it works as well.
Based on answer above:
T CreateOrReplaceAsset<T> (T asset, string path) where T:Object{
T existingAsset = AssetDatabase.LoadAssetAtPath<T>(path);
if (existingAsset == null){
AssetDatabase.CreateAsset(asset, path);
existingAsset = asset;
}
else{
EditorUtility.CopySerialized(asset, existingAsset);
}
return existingAsset;
}
Answer by femi · Dec 29, 2010 at 09:27 PM
This seems to work:
Mesh dummy = (Mesh)AssetDatabase.LoadAssetAtPath(asset_path, typeof(Mesh)); if (!dummy) { dummy = new Mesh(); dummy.name = name; AssetDatabase.CreateAsset(dummy, asset_path); } else dummy.Clear();
// [... import dummy ...]
AssetDatabase.SaveAssets();
(attempting to load/modify mesh then clear it and import from the new version of the resource)
Your answer
Follow this Question
Related Questions
Importing custom assets? 0 Answers
Two different assets have different layers on the same layer number. How to fix 1 Answer
Cannot save a generated texture as part of a bundle 1 Answer
How do I import assets from the Asset Store while offline? 2 Answers
Unity Error removes assets - "Rebuilding Library because asset database could not be found!" 0 Answers