- Home /
Editing and saving assets
I have an editor script that currently loads assets in certain directory, modifies them and then saves the changes. I use the following code:
curPrefabs = AssetDatabase.LoadAllAssetsAtPath(pfPath);
foreach(UnityEngine.Object obj in curPrefabs)
{
if(obj != null)
if(obj.GetType() == typeof(GameObject))
{
target = (GameObject)Instantiate(obj);
...............modify target..............................
target.name = obj.name;
UnityEngine.Object newPref = EditorUtility.CreateEmptyPrefab("Assets" +
path + obj.name + ".prefab");
EditorUtility.ReplacePrefab(target, newPref);
GameObject.DestroyImmediate(target);
}
AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
}
1st. If I try to use LoadAssetAtPath(pfPath, typeof(GameObject)) or LoadMainAssetAtPAth(pfPath) I always receive null, even when LoadAllAssetsAtPath return GameObject (among other types).
2nd. If I try to replace an existing prefab with EditorUtility.ReplacePrefab(target, obj); instead of creating a new one like in the script above, Unity seems to save the file (I know this because I can see the changes made when I run the script two times in a row), but it never updates in the editor and is overwritten with unmodified version when I close Unity.
3rd. If I use this code to replace some of the materials on a prefab, the link to materials is partially lost. I.e. if I make changes to materials they are only applied to GameObjects after I close and reopen the project. I've traced this to a separate issue: http://answers.unity3d.com/questions/122861/retaining-material-connections.html?viewedQuestions=122213&viewedQuestions=12934
What causes all these issues? What am I doing wrong?