纳金网

标题: 编辑器扩展代码:批量替换物体材质 批量替换模型 [打印本页]

作者: 会飞的鱼    时间: 2012-3-15 14:41
标题: 编辑器扩展代码:批量替换物体材质 批量替换模型
还是蛮有用的功能实现,未经实际测试,两段代码分别如下:



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 {
   
   var meshName : Mesh;
   
   @MenuItem ("WAR/Replace Mesh Recursively...")
   
   static function ReplaceMeshRecursivelyItem() {
      ScriptableWizard.DisplayWizard("Add Component Recursively", ReplaceMeshRecursively, "REPLACE MESH", "");
   }
   
   //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)



   static function ValidateMenuItem() : boolean {
return Selection.activeTransform;
   }
}
转自:http://forum.unity3d.com/threads/95151-EDITOR-SCRIPTS-Replace-Material1-with-Material2-on-all-Repleace-Mesh-on-all




作者: 彬彬    时间: 2012-3-16 19:36

   

作者: 彬彬    时间: 2012-3-16 19:37

   





欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5