Customizable Navigation Bar

Wednesday, May 11, 2011

Wednesday's Editor Tools 2: QuickComponentDelete

Ok guys, sorry about the massive delay between posts. I've just had a few things to take care of now that I am finished with school and back home temporarily.

This next editor script is going to make it easy for you guys to quickly delete components from multiple objects and their children. This is useful for when you make a booboo while attaching components or you swap one of your scripts out for something else.

Basically what this script is going to do is open up a display wizard and take in a string of the component that you want to remove from the objects in your selection.

What we are going to do is create a new C# file called QuickComponentDelete, and make the class extend ScriptableWizard.


using UnityEngine;
using System.Collections;
using UnityEditor;

public class QuickComponentDelete : ScriptableWizard
{
       
        /*We need something to take in a store the string name of the component you want to remove, so let's create a public string*/

        public string typeString;

        /*Next up let's create a static function called openWizard, and give it the MenuItem attribute so that we can find it in the menus on the toolbar. This static function is what will be called when you click on the QuickComponentDelete option in the menu*/

        [MenuItem("GameObject/Delete Components in Children Wizard")]
        static void openWizard()
        {

        /*Basically all we want this function to do is open ip a display wizard. There are other wizard functions that will take care of the functionality that we want, we just need a way for the user to access it*/

                ScriptableWizard.DisplayWizard("Set the string of type", typeof(DeleteComponentsInChildren), "Set");
        }

        /*The next function that we are going to create is called OnWizardUpdate(). This is an event that is called when the display wizard is opened or updated in some way shape or form. All that we are going to do is set the string variable helpString to something that will tell the user what to do.*/


        void OnWizardUpdate()
        {
                helpString = "Set the string of the Component type you want to delete:";
        }

        /*The next function we create is called OnWizardCreate(). The functionality added here is what gets called when the user clicks the create button. The create button will be called something else depending on what you name it. We have it called "Set" in this script, as you can see in the ScriptableWizard.DisplayWizard() function call*/

        void OnWizardCreate()
        {

                /*What we are going to do first is grab all of the object transforms in the current selection*/

                Transform[] allTransforms = Selection.GetTransforms(SelectionMode.Deep);
                /*Afterwards we are simply just going to grab the components of the string type that was passed and Destroy them on each object*/

                foreach(Transform currentTran in allTransforms)
                {
                        if((currentTran.GetComponent(typeString) != null))
                        {
                                Object.DestroyImmediate(currentTran.GetComponent(typeString));
                        }
                }
        }
}


That's it for this script. You'll notice that a lot of these editor scripts are very short and simple, but they save you quite a bit of time in the long run.

That's it for this week's tutorial, follow me on twitter, and keep coding!