查看: 1893|回复: 2
打印 上一主题 下一主题

编辑器扩展代码:批量替换物体材质 批量替换模型

[复制链接]

5552

主题

2

听众

8万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
11

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2012-3-15 14:41:34 |只看该作者 |倒序浏览
还是蛮有用的功能实现,未经实际测试,两段代码分别如下:



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



分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

797

主题

1

听众

1万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
5568
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2012-3-16 19:36:23 |只看该作者

   
回复

使用道具 举报

797

主题

1

听众

1万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
5568
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

板凳
发表于 2012-3-16 19:37:21 |只看该作者

   
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2025-1-11 18:37 , Processed in 0.078276 second(s), 29 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部