Hi, I try to create an editor script that do the following: backup asset prefab > modify it > restore the original back. My intention is to create a build script that can strip a part of project (e.g. remove some components) to create server version and client version of our game. Below is the code I use but no luck.
[MenuItem("Build/Test")]
static void Test()
{
foreach (string guid in AssetDatabase.FindAssets("t:GameObject"))
{
string prefabPath = AssetDatabase.GUIDToAssetPath(guid);
GameObject go = AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath);
if (go != null && go.name == "TestPrefab")
{
copyFile(prefabPath, "tmp.prefab");
copyFile(prefabPath+".meta", "tmp.prefab.meta");
//Make change to prefab
DestroyImmediate(go.GetComponent<UnityEngine.AI.NavMeshAgent>(), true);
//Do something...
copyFile("tmp.prefab", prefabPath);
copyFile("tmp.prefab.meta", prefabPath+".meta");
AssetDatabase.Refresh();
return;
}
}
static void copyFile(string src, string dst)
{
if (dst.LastIndexOf('/') >= 0)
{
Directory.CreateDirectory(dst.Substring(0, dst.LastIndexOf('/')));
}
File.Copy(src, dst, true);
}
I successfully backup the prefab with above code but I cannot restore it. I can manually restore it by replacing the changed prefab with the backup (when Unity is closed) though. So, I’d like to know how to do this backup and restore properly.
EDIT: I also try AssetDatabase.CopyAsset. But also no luck
Thanks.