- Home /
Not a question
EDITOR Script to replace a mesh for many of the same type
I thought I would share this, as its something I had questions on, but luckily there are cool and smart people who keep posting tidbits of gold! So I thought I would give back. Brief- replaces all Mesh(s) in a selected gameobject with another (recursively)
I have train tracks in my project (work for the RR) and it drives me crazy to have to build them, so I am hoping EasyRoads next ver. will have the track placement issues adjusted.) So I wanted to swap out concrete ties for another version (lower polygon) I can do this at runtime, but I wanted to do it in the editor.
So here is a script I wrote, and it actually works!! (after a few issues) Would love to add to it, or correct it, so if you have any thoughts, please share. ----< JavaScript -------------
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;
}
}
You should post this over on unifycommunity's scripts wiki, and let everybody know about it in the unity forums.
http://www.unifycommunity.com/wiki/index.php?title=Scripts
Since this isn't a question, I'm going to close it.