{"id":78290,"date":"2018-04-18T14:55:41","date_gmt":"2018-04-18T14:55:41","guid":{"rendered":"https:\/\/www.red-gate.com\/simple-talk\/?p=78290"},"modified":"2021-07-29T19:44:14","modified_gmt":"2021-07-29T19:44:14","slug":"extending-unity-editor-c","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/development\/dotnet-development\/extending-unity-editor-c\/","title":{"rendered":"Extending the Unity Editor with C#"},"content":{"rendered":"<h4>The series so far:<\/h4>\n<ol>\n<li><a href=\"https:\/\/www.red-gate.com\/simple-talk\/dotnet\/c-programming\/introduction-game-development-unity-c\/\">Introduction to Game Development with Unity and C#<\/a><\/li>\n<li><a href=\"https:\/\/www.red-gate.com\/simple-talk\/dotnet\/c-programming\/using-c-functions-animations\/\">Using C# Functions in Your Animations<\/a><\/li>\n<li><a href=\"https:\/\/www.red-gate.com\/simple-talk\/dotnet\/c-programming\/procedural-generation-unity-c\/\">Procedural Generation with Unity and C#<\/a><\/li>\n<li><a href=\"https:\/\/www.red-gate.com\/simple-talk\/dotnet\/c-programming\/pathfinding-unity-c\/\">Pathfinding with Unity and C#<\/a><\/li>\n<li><a href=\"https:\/\/www.red-gate.com\/simple-talk\/dotnet\/c-programming\/extending-unity-editor-c\/\">Extending the Unity Editor with C#<\/a><\/li>\n<li><a href=\"https:\/\/www.red-gate.com\/simple-talk\/dotnet\/c-programming\/using-unity-ui-and-c-to-create-a-tic-tac-toe-game\/\">Using Unity UI and C# to Create a Tic-Tac-Toe Game<\/a><\/li>\n<\/ol>\n\n<p>Pretend for a moment that you&#8217;re working on your game, and you need to have monster characters placed throughout your scene. Now suppose that in another scene you need the same monster objects from the first scene, but they need different properties from the monsters in the previous scene. For example, they may need to be a different size than the previous monsters. Typically, you only have two methods of changing an object&#8217;s properties. One way to change an object is through scripting, but there&#8217;s a problem with that method; all objects that use the same script will apply those same properties. From one scene to the next, they&#8217;ll all be the same.<\/p>\n<p>Another method is to alter the values of variables in the <em>Inspector <\/em>window in Unity, but this would require going to each individual object and changing whatever values you would like changed. This can be very tedious and time consuming if you have a lot of objects to change at once. Surely there&#8217;s another way to do the same task. Fortunately, there is a third option available thanks to Unity extensions, and of course the process of creating your own extensions is made relatively simple. In this project you&#8217;ll create a simple Unity extension that will gather all objects of a certain tag and set both a random color to each individual object as well as allow you, the user, to change the size of the objects all at once in just a couple clicks.<\/p>\n<h2>Setting Up<\/h2>\n<p>To begin, launch Unity and click the <em>New <\/em>button to start a new project as shown in Figure 1.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"123\" class=\"wp-image-78364\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/04\/word-image-19.jpeg\" \/> Figure 1: Starting the new project<\/p>\n<p>Next, name the project <em>Extending Unity<\/em> and be sure to set it as a <em>2D<\/em> project. While this same concept can be used in 3D projects just as easily, this particular project will be using 2D objects to accomplish its goals. Figure 2 shows what your project setup window should look like before creating the <em>Extending Unity <\/em>project by clicking the <em>Create project<\/em> button.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"1002\" height=\"582\" class=\"wp-image-78365\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/04\/word-image-20.jpeg\" \/> Figure 2: Project creation screen<\/p>\n<p>Once the project is created, the first task to complete is to create objects to be edited using an extension you&#8217;ll make yourself. Using the <em>Create <\/em>menu in the <em>Hierarchy <\/em>window, select <em>2D Object-&gt;Sprite <\/em>to create a new object called <em>New Sprite<\/em>. Figure 3 shows where to find this option.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"368\" height=\"387\" class=\"wp-image-78366\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/04\/word-image-21.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 3: Creating a new Sprite object using the Hierarchy&#8217;s Create menu<\/p>\n<p>Of course, it&#8217;ll be hard to see the changes your extension makes to an object if you can&#8217;t see it. Assign a sprite to this object by selecting the button to the far right of the <em>Sprite <\/em>field in the <em>Inspector<\/em> window like in Figure 4.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"576\" height=\"64\" class=\"wp-image-78367\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/04\/word-image-22.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 4: Selecting a new sprite image<\/p>\n<p>Select <em>UISprite <\/em>as the sprite you will use for this project&#8217;s objects like in Figure 5.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"769\" height=\"539\" class=\"wp-image-78368\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/04\/word-image-23.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 5: The sprite this object will use<\/p>\n<p>In order for your extension to change <em>New Sprite <\/em>and all duplicated <em>New Sprite <\/em>object properties simultaneously, you will need to create and assign a tag to the <em>New Sprite<\/em> object. Tags help identify an object for scripting purposes. For example, using tags helps Unity decide if the player ran into an enemy or a wall and will perform whatever actions you assign it from there. To create a new tag, select the <em>New Sprite <\/em>object and open the <em>Tag <\/em>drop down menu found in the <em>Inspector<\/em> window. Then select <em>Add Tag <\/em>at the bottom of the menu. Figure 6 shows where to find these options.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"302\" height=\"314\" class=\"wp-image-78369\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/04\/word-image-24.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 6: Opening the Tags menu<\/p>\n<p>Once <em>Add Tag <\/em>is selected, your <em>Inspector <\/em>window should now look like the one in Figure 7.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"575\" height=\"228\" class=\"wp-image-78370\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/04\/word-image-25.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 7: The Unity Tags and Layers window<\/p>\n<p>To create a new tag, click the <em>+ <\/em>symbol in the <em>Tags <\/em>menu. Name your new tag <em>Sprite <\/em>and click the <em>Save <\/em>button to finish creating your new tag. Figure 8 shows where to add tags.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"575\" height=\"228\" class=\"wp-image-78371\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/04\/word-image-26.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 8: Creating a new tag<\/p>\n<p>With the new tag created, select the <em>New Sprite <\/em>object and set the <em>Sprite <\/em>tag to <em>New Sprite <\/em>by opening the <em>Tag <\/em>menu again and this time selecting <em>Sprite <\/em>like in Figure 9.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"575\" height=\"314\" class=\"wp-image-78372\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/04\/word-image-27.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 9: Apply the Sprite tag to the New Sprite object<\/p>\n<p>Finally, create eight duplicates of the <em>New Sprite <\/em>object to bring you to a total of nine <em>New Sprite <\/em>objects. To create a duplicate, select the <em>New Sprite <\/em>object and press Ctrl + D. Then change the locations of the <em>New Sprite<\/em> objects so they&#8217;re not all in the same place. Figure 10 shows an example of how the objects can be arranged within the <em>Game<\/em> window, but you may arrange them in any way you wish. To move the Sprites, modify the X and Y <em>Position<\/em> values in the Transform window. You can see them in the <em>Game<\/em> or <em>Scene<\/em> window.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"210\" height=\"196\" class=\"wp-image-78373\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/04\/word-image-28.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 10: Example arrangement of the New Sprite objects<\/p>\n<p>Now it&#8217;s time to create the extension, but there&#8217;s one thing you must do in order for the extension to work. You have to create a new folder called <em>Editor <\/em>for Unity to store any extensions you make. The <em>Editor <\/em>folder is where Unity searches for any sort of extensions, whether you make them yourself or if you acquire them from the <em>Asset Store. <\/em>Create a new folder by right clicking in the <em>Assets <\/em>window and choosing <em>Create-&gt;Folder <\/em>to create the new folder. Figure 11 shows where to create the <em>Editor <\/em>folder. Remember, the folder name <strong><em>must <\/em><\/strong>be <em>Editor <\/em>for the extension to work.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"766\" height=\"671\" class=\"wp-image-78374\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/04\/word-image-29.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 11: Creating a new folder<\/p>\n<p>Within the newly created <em>Editor <\/em>folder in the <em>Assets <\/em>window, create a new script titled <em>NewWindow<\/em> by selecting <em>Create-&gt;C# Script<\/em>. Like in Figure 12, be sure this script is within the <em>Editor <\/em>window.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"747\" height=\"660\" class=\"wp-image-78375\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/04\/word-image-30.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 12: Adding a new script to the Editor folder<\/p>\n<p>Once the <em>NewWindow <\/em>script is created, double click it to open Visual Studio and begin creating your Unity extension.<\/p>\n<h2>Adding the Code<\/h2>\n<p>Right away you&#8217;ll need to add a line underneath the line of code that says <strong>using UnityEngine;<\/strong>. To make this project work, enter the following code:<\/p>\n<pre class=\"lang:c# theme:vs2012\">using UnityEditor;<\/pre>\n<p>Next you should go to the class declaration and change <strong>MonoBehaviour<\/strong> to <strong>EditorWindow<\/strong> in order for our new window to appear within the Unity engine. With this change done your code should begin to look something like the code in Figure 13.<\/p>\n<p class=\"caption\" style=\"text-align: left;\"><img loading=\"lazy\" decoding=\"async\" width=\"341\" height=\"341\" class=\"wp-image-78376\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/04\/word-image-31.jpeg\" \/><\/p>\n<p class=\"caption\" style=\"text-align: left;\">Figure 13: The code that lets Unity know this is an extension<\/p>\n<p>Now it&#8217;s time to move on to the code that will make this window perform the tasks it needs to do. To begin, delete or comment out the <em>Start <\/em>and <em>Update <\/em>functions as they will not be needed for the duration of this project. Once this is done, underneath the class definition add this code:<\/p>\n<pre class=\"lang:c# theme:vs2012\">  private GameObject[] sprites;\r\n  private bool changeColors = true;\r\n  private float spriteScale = 1f;\r\n  private string buttonString = \"Get New Colors!\";<\/pre>\n<p>The first line creates an empty array that will be used to store those <em>New Sprite <\/em>objects you created earlier. Later, you will use <strong>for<\/strong> loops to cycle through each <em>New Sprite <\/em>object to change their size and give them random colors. After this array is declared, you then create a boolean that will control if you want to give new colors to the different <em>New Sprite <\/em>objects followed by a float variable that will be used to change each object&#8217;s size. Finally, a string variable was created to be used with a button you will create shortly. Though its default value is &#8220;Get New Colors!&#8221; it can be changed to something else depending on the circumstances. At this point your <em>NewWindow <\/em>script should begin to look like Figure 14.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"450\" height=\"112\" class=\"wp-image-78377\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/04\/word-image-32.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 14: All variables declared<\/p>\n<p>This is where the fun begins! First you must define where the user will find and open this window within Unity. Underneath the variable declarations, add the following line of code:<\/p>\n<pre class=\"lang:c# theme:vs2012\">[MenuItem(\"Window\/New Window\")]<\/pre>\n<p>You have now told Unity where to place the option to open your custom window, which will simply be called <em>New Window<\/em>. Later on, after completing this script and returning to Unity, you will navigate to <em>Window-&gt;New Window <\/em>to open your custom window. Of course, clicking that won&#8217;t actually show anything right now, which is where this next part comes in. A static void function will be needed to display the window to the user. Right under the previous line of code add the following function:<\/p>\n<pre class=\"lang:c# theme:vs2012\">  public static void ShowWindow()\r\n  {\r\n     EditorWindow.GetWindow(typeof(NewWindow));\r\n  }<\/pre>\n<p>All that&#8217;s done here is to simply find an instance of the <em>New Window<\/em> window. If one does not exist, Unity will create one. Figure 15 shows the code needed to display a new window simply title <em>New Window<\/em>.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"418\" height=\"133\" class=\"wp-image-78378\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/04\/word-image-33.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 15: Coding the ability to select and display New Window in Unity<\/p>\n<p>It&#8217;s now time to create the mechanics of this custom window. After all, a blank window isn&#8217;t really going to help with development in any way. Start by creating the following function:<\/p>\n<pre class=\"lang:c# theme:vs2012\">  private void OnGUI()\r\n  {\r\n  }<\/pre>\n<p>In short, this function will be the one that handles all input from you, the user, and applies whatever changes you ask to the <em>New Sprite <\/em>objects. Begin by placing a label at the top of the extension window, which is done by adding this code to the <em>OnGUI <\/em>function:<\/p>\n<pre class=\"lang:c# theme:vs2012\">GUILayout.Label(\"Sprite Settings\", EditorStyles.boldLabel);<\/pre>\n<p>Essentially, all you&#8217;ve done is place text at the top of the window that says \u2018Sprite Settings\u2019 and made it bold. The next line is a lot more important, as this line will collect all the <em>New Sprite <\/em>objects created earlier and store them in the array you created at the beginning. This is where that <em>Sprite <\/em>tag you created comes into play. You assigned the <em>New Sprite <\/em>objects with the <em>Sprite <\/em>tag in order to help the <em>NewWindow <\/em>script identify which objects to store into the <strong>sprites<\/strong> array and ultimately help the <em>NewWindow <\/em>extension you&#8217;re creating know which objects to change. With that in mind, add the following line to the <em>OnGUI<\/em> function:<\/p>\n<pre class=\"lang:c# theme:vs2012\">sprites = GameObject.FindGameObjectsWithTag(\"Sprite\");<\/pre>\n<p>Now <em>NewWindow <\/em>will be able to find all objects tagged with <em>Sprite<\/em> and add them to the <em>sprites <\/em>array for later editing. Next, go ahead and change the <em>buttonString <\/em>text depending on the value of the <em>changeColors <\/em>boolean variable:<\/p>\n<pre class=\"lang:c# theme:vs2012\">  if (changeColors)\r\n       buttonString = \"Get New Colors!\";\r\n  else\r\n       buttonString = \"Change Sprites to White!\";<\/pre>\n<p>Like in video games, it&#8217;s nice to have your Unity extension react to certain things you do, in this case changing the value of a boolean. The button that will later be created that assigns new colors to the <em>New Sprite <\/em>objects should have different text depending on if they&#8217;re all going to get different colors or if you want to set the color of all the objects to white. At this point, your <em>OnGUI <\/em>function should look like the one seen in Figure 16.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"543\" height=\"197\" class=\"wp-image-78379\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/04\/word-image-34.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 16: The beginnings of the OnGUI function<\/p>\n<p>The next step is to create a toggle and a slider. You&#8217;ll be adjusting the value of the <strong>changeColors<\/strong> boolean by clicking on the toggle, which will then allow you to click another button and give each <em>New Sprite <\/em>object random colors or to simply set all their colors to white. After that, the <strong>spriteScale<\/strong> value will be altered based on a slider, which will in turn affect the size of the <em>New Sprite <\/em>objects. For the slider, the minimum value allowed will be one, while the maximum size of the <em>New Sprite <\/em>objects will be ten. To accomplish this you must add this code:<\/p>\n<pre class=\"lang:c# theme:vs2012\">  changeColors = EditorGUILayout.Toggle(\"Randomly Change Sprite Colors?\", changeColors);\r\n  spriteScale = EditorGUILayout.Slider(\"Sprite Scale\", spriteScale, 1, 10);<\/pre>\n<p>With the toggle and slider in place, create the code that will change the size of each object in your <strong>sprites<\/strong> array to whatever the value of the <strong>spriteScale<\/strong> variable is:<\/p>\n<pre class=\"lang:c# theme:vs2012\">  foreach (GameObject sprite in sprites)\r\n       sprite.transform.localScale = new Vector3(spriteScale, spriteScale, 0);<\/pre>\n<p>This <strong>foreach<\/strong> loop may seem a little weird, so I\u2019ll break it down. Basically, the <em>NewWindow <\/em>script will cycle through each object in the <strong>sprites<\/strong> array. For every object (sprite), a new <strong>Vector3<\/strong> variable will be created and set as the value of <strong>sprite<\/strong>&#8216;s local scale, replacing the <em>X <\/em>and <em>Y <\/em>values in the <strong>Vector3<\/strong> variable to the value of <strong>spriteScale<\/strong>. Remember that the <strong>spriteScale<\/strong> value is being changed by the slider created earlier. You&#8217;re not necessarily getting the value of the slider itself. Now that this step is complete, the <strong>OnGUI<\/strong> function should look similar to the function in Figure 17.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"720\" height=\"292\" class=\"wp-image-78380\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/04\/word-image-35.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 17: Creating a loop that changes the scale of all objects in the sprites array<\/p>\n<p>There is now one final step in the <strong>OnGUI<\/strong> function creation process that must be done before this function, and by extension this script, is complete. First, create an <strong>if<\/strong> statement that checks to see if the user pressed a button, which will be drawn in the same statement. The code for this looks like the following:<\/p>\n<pre class=\"lang:c# theme:vs2012\">  if (GUI.Button(new Rect(position.width \/ 2, position.height \/ 2, 180, 100), buttonString))\r\n  {\r\n  }<\/pre>\n<p>The button that the user can press is specified by using the code <strong>new Rect<\/strong> and then specifying the location and size of the button. After that, you give the code a string to place within the button. In this case, you&#8217;ll be using the <strong>buttonString<\/strong> variable created at the start. After that&#8217;s done, it&#8217;s time to specify to the script what to do when the button is pressed. To accomplish this, enter the following code inside the <strong>if<\/strong> block:<\/p>\n<pre class=\"lang:c# theme:vs2012 \">  foreach(GameObject sprite in sprites)\r\n  {\r\n  \tif (changeColors)\r\n  \t{\r\n  \t\tfloat red, green, blue;\r\n  \t\tred = Random.Range(0.0f, 1.0f);\r\n  \t\tgreen = Random.Range(0.0f, 1.0f);\r\n  \t\tblue = Random.Range(0.0f, 1.0f);\r\n  \t\tsprite.GetComponent&lt;SpriteRenderer&gt;().color = new Color(red, green, blue, 1);\r\n  \t}\r\n  \telse\r\n  \t{\r\n  \t\tsprite.GetComponent&lt;SpriteRenderer&gt;().color = new Color(1, 1, 1, 1);\r\n  \t}\r\n  }<\/pre>\n<p>Similar to the previous <strong>foreach<\/strong> loop where you adjusted the size of the objects in the <strong>sprites<\/strong> array, <strong>NewWindow<\/strong> will cycle through each <em>New Sprite <\/em>object and change its color. If the <strong>changeColors<\/strong> boolean is true, it will create three float variables that will all get a random number between zero and one assigned to them. From there, the script gets the <em>sprite <\/em>object&#8217;s <strong>SpriteRenderer <\/strong>component and then gets the <strong>color<\/strong> variable within that component. It sets it to a new color with the values or <strong>red<\/strong>, <strong>green<\/strong>, and <strong>blue <\/strong>and sets the alpha (the transparency) to 1. However, if the <strong>changeColors<\/strong> boolean is false, it will simply set all <strong>sprite<\/strong> objects to the color white, which is <strong>1, 1, 1<\/strong> in code.<\/p>\n<p>It&#8217;s important to specify in the <strong>Random.Range<\/strong> part of the code that the numbers available to randomly pick include decimal numbers, which is why you need to specify the <strong>.0f<\/strong> part of the number. Should you forget to do this, Unity will simply pick between zero and one, which will not work for assigning random colors as zero and one are the minimum and maximum values that Unity will read when it comes to colors. The <strong>if<\/strong> statement and <strong>foreach<\/strong> loop should look like the one seen in Figure 18.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"752\" height=\"350\" class=\"wp-image-78381\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/04\/word-image-36.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 18: Completing the OnGUI function<\/p>\n<p>With the code completed, you&#8217;ve not only created the <strong>OnGUI<\/strong> function but also the <em>NewWindow <\/em>script. Now is the time to save your code and return to Unity to test out your new extension.<\/p>\n<h2>Testing the Extension<\/h2>\n<p>Remember when you specified in the code where to open the <em>New Window <\/em>window? You coded it so that it would appear under <em>Window <\/em>in the menu and have the name <em>New Window<\/em>. Figure 19 shows where to find your extension.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"450\" height=\"500\" class=\"wp-image-78382\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/04\/word-image-37.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 19: Opening the New Window extension<\/p>\n<p>Once opened, test out your new extension. Change the <em>Sprite Scale <\/em>slider and watch as the <em>New Sprite <\/em>objects change as you adjust the slider. Toggle the <em>Randomly Change Sprite <\/em>checkbox to true and click the <em>Get New Colors <\/em>button to change all <em>New Sprite <\/em>colors. Set them back to the color white by setting the toggle to false and pressing the button again. Figure 20 shows an example <em>Game <\/em>screen of the final project.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1210\" height=\"462\" class=\"wp-image-78383\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/04\/word-image-166.png\" \/><\/p>\n<p class=\"caption\">Figure 20: The New Window extension in action<\/p>\n<h2>Reusing the Extension<\/h2>\n<p>If you wish, you can also save the extension for later use in a different project in an object called a \u2018package\u2019. To do this, go to your <em>Assets <\/em>window and right click the <em>Editor <\/em>folder. Then, select <em>Export Package <\/em>like in Figure 21.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"544\" height=\"446\" class=\"wp-image-78384\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/04\/word-image-167.png\" \/><\/p>\n<p class=\"caption\">Figure 21: Exporting the package<\/p>\n<p>A new window will appear showing what assets will be included inside the package you&#8217;re about to make. Be sure to have everything selected for exporting. Here, the only thing that will be exported is the folder <em>Editor <\/em>as well as the <em>NewWindow <\/em>script you created. When you&#8217;re ready, click the <em>Export <\/em>button near the bottom of the window as shown in Figure 22.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"403\" height=\"278\" class=\"wp-image-78385\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/04\/word-image-168.png\" \/><\/p>\n<p class=\"caption\">Figure 22: Exporting the extension<\/p>\n<p>Save your Unity package to any location you desire on your computer and give it a name. In this example the package has been named <em>extension package <\/em>but it can have whatever name you wish. Once you&#8217;ve saved it, create a new project in Unity. Then, with the project currently open, find the package on your computer (Figure 23) and double-click it.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"757\" height=\"166\" class=\"wp-image-78386\" style=\"background-color: transparent; color: #333333; font-family: Georgia,&amp;quot; times new roman&amp;quot;,&amp;quot;bitstream charter&amp;quot;,times,serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 400; height: 166px; letter-spacing: normal; max-width: 991.21px; orphans: 2; outline-color: #72777c; outline-style: solid; outline-width: 1px; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/04\/word-image-38.jpeg\" \/><b><\/b><i><\/i><u><\/u><\/p>\n<p><span style=\"display: inline !important; float: none; background-color: transparent; color: #333333; font-family: Georgia,'Times New Roman','Bitstream Charter',Times,serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; line-height: 24px; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px; word-wrap: break-word;\">Figure 23: Selecting the package file to be imported into a new project<\/span><b><\/b><i><\/i><u><\/u><\/p>\n<p>You&#8217;ll see a new window open appear in your new Unity project. In this window you can see the <em>Editor <\/em>folder and <em>NewWindow <\/em>script you created in the <em>Extending Unity<\/em> project. It is here that you can also decide to omit certain scripts from the import but considering there&#8217;s only one script in this extension you don&#8217;t need to make such changes. Like in Figure 24, click the <em>Import <\/em>button near the bottom of the window to bring these assets into your new project.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"420\" height=\"326\" class=\"wp-image-78387\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/04\/word-image-169.png\" \/><\/p>\n<p class=\"caption\">Figure 24: Importing the extension<\/p>\n<p>Once Unity has finished importing the package you&#8217;ll see in your <em>Assets <\/em>window the very extension you made in the <em>Extending Unity <\/em>project! In this case, to actually use the functionality, you will need to add some Sprites to the new project.<\/p>\n<h2>Conclusion<\/h2>\n<p>It&#8217;s almost guaranteed that at some point when using any program, whether it be Unity, Photoshop, or Visual Studio, that extensions will be desired, perhaps even needed. In the case of Unity, you can easily create your own extensions that will be tailored specifically to your needs. There&#8217;s certainly no harm in browsing Unity&#8217;s <em>Asset Store <\/em>for extensions. In fact, it is often worth checking to see if there&#8217;s anything that can help with whatever you&#8217;re developing. However, odds are they will not be the perfect fit for your project. Even the most general-purpose extensions you can find may not work with your project <em>exactly<\/em> like you want it to.<\/p>\n<p>Therein lies the beauty of creating your own extensions; you&#8217;re in control of how they function. You can have them do as much or as little as you need them to. They&#8217;ll be made specifically with your needs in mind. At the end of the day, nobody knows your needs as well as you do. The Unity engine arrives out of the box with a lot of tools and features, but, should you wish, you can easily add to Unity&#8217;s functionality using your own code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The next step in learning how to create games in C# using Unity is to add extensions to get custom functionality. Lance Talbert shows you how to create an extension that changes the properties of many objects at once.&hellip;<\/p>\n","protected":false},"author":317499,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[143538],"tags":[95509],"coauthors":[52549],"class_list":["post-78290","post","type-post","status-publish","format-standard","hentry","category-dotnet-development","tag-standardize"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/78290","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/317499"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=78290"}],"version-history":[{"count":5,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/78290\/revisions"}],"predecessor-version":[{"id":78318,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/78290\/revisions\/78318"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=78290"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=78290"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=78290"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=78290"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}