Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jun 29, 2011 at 01:52 AM by testure for the following reason:

Not a question

avatar image
0
Question by wkmccoy · Jun 29, 2011 at 01:17 AM · editor-scriptingmeshfilterreplace mesh

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;
    }
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image testure · Jun 29, 2011 at 01:52 AM 0
Share

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.

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

AddComponent giving NullReferenceException in Editor script 1 Answer

Replace MeshFilter mesh by a other mesh in Editor 1 Answer

Editing tools issues 1 Answer

Array of objectFiled in editorWindow 1 Answer

Hiding Private Functions From AnimationEvents 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges