RepleaceMeterialRecursively:
class ReplaceMaterialRecursively extends ScriptableWizard {
var ToMaterialName : Material;
var FromMaterialName: Material;
@MenuItem ("WAR/Replace Material Recursively...")
static function ReplaceMaterialRecursivelyItem() {
ScriptableWizard.DisplayWizard("Replace Material Recursively", ReplaceMaterialRecursively, "REPLACE Material", "");
}
//Main function
function OnWizardCreate() {
var total : int = 0;
for (var currentTransform : Transform in Selection.transforms) {
total += RecurseAndAdd(currentTransform, ToMaterialName, FromMaterialName);
}
if (total == 0)
Debug.Log("No Materials Replaced.");
else
Debug.Log(total + " Material replaced with "" + ToMaterialName + "" ");
}
function RecurseAndAdd(parent : Transform, MaterialName : Material, FromMaterialName:Material) : int {
//keep count
var total : int = 0;
//add components to children
Debug.Log("in loop ");
// print("var"+var);
for (var child : Transform in parent) {
total += RecurseAndAdd(child, ToMaterialName, FromMaterialName);
}
//add component to parent
//parent.GetComponent(MaterialFilter);
if (parent.renderer != null){
for (var material in parent.renderer.sharedMaterials) {
Debug.Log("Material=" + material);
Debug.Log("FromMaterialName=" + FromMaterialName);
if (material== FromMaterialName) {
Debug.Log("FromMaterial Found!=");
parent.renderer.sharedMaterial = ToMaterialName;
// parent.gameObject.GetComponent(Material).Material = MaterialName;
// parent.gameObject.GetComponent(Material).Material = MaterialName;
total++;
}
}
}
return total;
}
//Set the help string
function OnWizardUpdate () {
//helpString = "Specify the exact name of the replacment Material:";
helpString = "Select Game Obects to Affect";
// isValid = (MaterialName != null);
helpString = "Select a Material to assign to the game object you have selected";
if(ToMaterialName == null || FromMaterialName == null) {
errorString = "Select a Material to assign to the game object you have selected!";
isValid = false;
} else {
errorString = "";
isValid = true;
}
}
// The menu item will be disabled if no transform is selected.
@MenuItem ("WAR/Replace Material Recursively...", true)
static function ValidateMenuItem() : boolean {
return Selection.activeTransform;
}
}
模型替换的代码如下:
class ReplaceMeshRecursively extends ScriptableWizard {
//Main function
function OnWizardCreate() {
var total : int = 0;
for (var currentTransform : Transform in Selection.transforms) {
total += RecurseAndAdd(currentTransform, meshName);
}
if (total == 0)
Debug.Log("No Meshs Replaced.");
else
Debug.Log(total + " Mesh replaced with "" + meshName + "" ");
}
function RecurseAndAdd(parent : Transform, meshName : Mesh) : int {
//keep count
var total : int = 0;
//add components to children
for (var child : Transform in parent) {
total += RecurseAndAdd(child, meshName);
}
//add component to parent
//parent.GetComponent(MeshFilter);
if (parent.GetComponent(MeshFilter)) {
parent.gameObject.GetComponent(MeshFilter).mesh = meshName;
total++;
}
return total;
}
//Set the help string
function OnWizardUpdate () {
helpString = "Specify the exact name of the replacment Mesh:";
}
// The menu item will be disabled if no transform is selected.
@MenuItem ("WAR/Replace Mesh Recursively...", true)