unity Editor 下批量導出Perfab ,更改模型shader,添加刪除組件

using UnityEngine;
using System.Collections;
using UnityEditor;

public class PrefabChange : Editor {

[MenuItem("Tools/BatchPrefab All Children")]
 static void BatchPrefab(){
    Transform tParent = ((GameObject)Selection.activeObject).transform;
    Object tempPrefab;
    AddMeshCollider (tParent);
    foreach(Transform t in tParent){
        tempPrefab = EditorUtility.CreateEmptyPrefab("Assets/Prefab/" + t.name +".prefab");
        tempPrefab = EditorUtility.ReplacePrefab(t.gameObject, tempPrefab);
    }
    ChangeShader (tParent);
    MoveMeshCollider( tParent);
}
//改變材質的shader
static void ChangeShader(Transform tParent){
    foreach(Transform t in tParent){
        Material[] temp =t.GetComponent<Renderer>().sharedMaterials;
        foreach (var item in temp) {
            if (item.name.Contains("toutie")) {
                item.shader=Shader.Find("Legacy Shaders/Transparent/Specular");
            }
        }
        if (t.childCount>0) {
            ChangeShader(t);
        }
    }
}
//給模型添加Meshcollider
static void AddMeshCollider(Transform tParent){
    foreach (Transform t in tParent) {
        t.gameObject.AddComponent<MeshCollider>();
        if (t.childCount>0) {
            AddMeshCollider(t);
        }
    }
}
//給模型刪除Meshcollider
static void MoveMeshCollider(Transform tParent){
    foreach (Transform t in tParent) {
        if (t.GetComponent<MeshCollider>()) {
            DestroyImmediate(t.GetComponent<MeshCollider>());
        }
        if (t.childCount>0) {
            MoveMeshCollider(t);
        }
    }
}

}

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