Unity批處理修改prefab

使用script 批處理prefab文件 並保存。

EditorUtility.DisplayProgressBar("Modify Prefab", "Please wait...", 0);

string[] ids = AssetDatabase.FindAssets("t:Prefab", new string[] { "Assets/Resources/Prefabs"});
for (int i = 0; i < ids.Length; i++) {

    string path = AssetDatabase.GUIDToAssetPath(ids[i]);
    GameObject prefab = AssetDatabase.LoadAssetAtPath(path, typeof(GameObject)) as GameObject;
    GameObject instance = PrefabUtility.InstantiatePrefab(prefab) as GameObject;

    // change instance
    Transform[] trans = instance.GetComponentsInChildren<Transform>();
    int layer = LayerMask.NameToLayer("Player");
    for (int j = 0; j < trans.Length; j++) {
        trans[j].gameObject.layer = layer;
    }

    // other change instance ... ...

    // save
    PrefabUtility.ReplacePrefab(instance, prefab, ReplacePrefabOptions.ConnectToPrefab);

    DestroyImmediate(instance);

    EditorUtility.DisplayProgressBar("Modify Prefab", "Please wait...", i/(float)ids.Length);
}

AssetDatabase.SaveAssets();

EditorUtility.ClearProgressBar();

ref:http://answers.unity3d.com/questions/996188/modify-prefabs-in-editor-script.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章