Unity3D Game Development Editor Extension Program Development Instance, unity3d Program Development

Source: Internet
Author: User

Unity3D Game Development Editor Extension Program Development Instance, unity3d Program Development

Hello everyone, welcome to my blog. My blog address is http://www.qinyuanpei.com. Today we will talk about how to develop extensions for the editor in Unity3D. When talking about extensions, I believe everyone will be familiar with them. Whether Google's Chrome browser or classic FireFox, these browsers are most praised for their support for various extensions. An extension is a plug-in that follows the plug-in design principles and can be installed and uninstalled in the Host Program at any time without affecting the normal operation of the Host Program. We know that there are various plug-ins in Unity3D, such as NGUI, 2 DToolKit, and EasyTouch, which are all extensions. Based on the rich functions of the Host Program, the extension can help the Host Program complete a lot of additional work. It can be said that because Unity3D has a lot of plug-ins and resource support, Unity3D can be so sought after by everyone. However, as a program programmer, if only tools are used, what is the difference between them and common users, so in today's article, the blogger will use three specific examples to teach you how to develop extensions for the Unity3D editor. I hope this will help you learn the Unity3D technology!

Frequently Used namespaces and Classes

The namespace for developing the Unity3D editor extension is mainly UnityEditor. the frequently used classes in this namespace include EditorGUI, EditorGUILayout, and EditorWindow (there may be other classes, however, this has been used by bloggers so far. If there are other classes, you are welcome to add them ). The extension developed for the Unity3D Editor is also a script. Usually, you need to put the script file in the Editor folder under the project resource folder, that is, Assets/Editor. However, this script is no longer inherited from MonoBehaviour. The specific content will be put in the following examples. there are usually two Unity3D editor extensions, one is non-interface (such as case 1), and the other is interface (such as case 2 and Case 3 ). For such extensions without interfaces, we only need to define a class (without inheriting any parent class) and then define a static method in this class; for such extensions with interfaces, we need to let the defined class inherit EditorWindow and implement the OnGUI () method, because in OnGUI () in the method, we will plot the interface of the extension program, but there is no need to worry about it. Therefore, the interface drawing of the extension program is similar to the OnGUI () method in the Unity3D script, we need to familiarize ourselves with common controls. Now, let's take a look at today's practice. Are you ready?

Unity3D editor Extended Program Development Example 1 quick modification of Texture Type

An important update of Unity3D4.6 is the support of UGUI and Unity2D. As Unity2D is supported, the texture type of Unity3D is increased to a Sprite type. If the texture imported to Unity3D is the gallery of small images, Unity3D can be automatically recognized as Sprite. However, for a single texture, Unity3D is still processed by default. Therefore, if you need to use these images each time, you must manually set the TextureType to sprite, if the number of textures is small, there is no manual modification. However, if the number of textures in the project is large, adjusting TextureType one by one may waste a lot of time! What should we do? Simple! Write code!

Using UnityEngine; using UnityEditor; using System. collections; public class ImportSprite {// <summary> // convert the texture format to Sprite in batches /// </summary> [MenuItem ("Tools/ConvertToSprite")] static void ConvertToSprite () {// obtain all selected Object [] selection = (Object []) Selection. objects; // if (selection. length = 0) return; // import Texture foreach (Object obj in selection) in batches {// obtain each texture Texture = (texture) obj; // obtain the texture path string localpath = AssetDatabase. getAssetPath (texture); // import TextureImporter importer = (TextureImporter) AssetImporter. getAtPath (localpath); // sets the Texture Type importer. textureType = TextureImporterType. sprite; // import the project resource AssetDatabase. importAsset (localpath);} // refresh the project resource AssetDatabase. refresh ();}}

We put this script in the Editor folder. If nothing happens, a tool menu item will be added to the menu bar of Unity3D. There is only one sub menu item before the menu item ConvertToSprite. Now, all we need to do is select the texture to be converted to the sprite type in the project resource folder, and click Tools> ConvertToSprite. Soon (how fast can I try it in the editor window by myself, in short, it will be right soon, haha) All the textures are converted to the sprite type as we wish, at this moment, do you regret the 92 empty objects manually created in the past? The Anti-DDoS pro regretted the manual creation of 92 empty objects during the first anti-DDoS tower game, if at that time I knew that Unity3D could do these things, I would not manually create 92 empty objects when I killed them. Now I admire my courage. Well, as the first editor extension, let's summarize the main content:
1. In Unity3D, we can use TextureImporter, ModelImporter, AudioImporter, and MovieImporter to import textures, models, audios, and videos to Unity3D, and finally use AssetDatabase. importAsset () to add it to the project in the heat completely, and finally need to use AssetDatabase. refresh () method to Refresh local resources and make the imported resources take effect.
2. The objects obtained by Selection. objects cannot be distinguished from the selected objects in the scenario or from the project resource folder. If you need to select objects from the scenario, we recommend that you use Selection. transforms instead.

Case 2 dynamically generate Prefab

First, let's review the process of preparing Prefab:
1. Drag and Drop clips from the project resource folder to the scene.
2. Adjust the name, position, scale, component, and so on in the scene
3. Drag an object to the Prefabs folder to generate a Prefab
Although this is officially recommended by Unity3D, what should we do if we have a large number of Prefab products? The most intuitive example is the enemy in the game. In a medium-scale game, there are usually many types of enemies, and each enemy may behave differently. From a macro perspective, most of the enemy's features are the same. Therefore, we consider using programs to dynamically generate Prefab. Here we assume that Prefab does not need to be appended with scripts, because how to attach a script to Prefab has not been studied yet. Now let's look at the Code:

Using UnityEngine; using UnityEditor; using System. collections; public class PrefabWrap: EditorWindow {// default object name private string prefabName; // default OBJECT tag private static string prefabTag; // default object Layer private static int prefabLayer; // private static PrefabWrap instance of the current plug-in window; // <summary> // display the plug-in window /// </summary> [MenuItem ("Tools/PrefabWrapTool")] static void PrefabWrapTool () {// obtain the current window instance = Edi TorWindow. getWindow <PrefabWrap> (); // display the window instance. show () ;}/// <summary> /// interface customization in the OnGUI method /// </summary> private void OnGUI () {// draw a text box prefabName = EditorGUILayout. textField ("preset Object Name:", prefabName); // draw the preset OBJECT tag selection box prefabTag = EditorGUILayout. tagField ("preset OBJECT tag:", prefabTag); // draw the preset object hierarchy selection box prefabLayer = EditorGUILayout. layerField ("preset object Layer:", prefabLayer); // draw a button if (GUILayout. button ("generate preset objects", GUILayo Ut. Height (20) {if (prefabName! = String. empty) {CreatePrefab (prefabName) ;}}/// <summary> /// create Prefab in batches /// </summary> static void CreatePrefab (string name) {// obtain all selected Object [] selection = (Object []) Selection. objects; // if (selection. length = 0) return; // batch processing foreach (Object obj in selection) {// generate preset GameObject prefab = (GameObject) PrefabUtility. createPrefab ("Assets/Prefabs/" + name + ". prefab ", (GameObject) obj); // sets the tag and Layer prefab. tag = prefabTag; prefab. layer = prefabLayer; // import the project AssetDatabase. importAsset (AssetDatabase. getAssetPath (prefab);} // refresh the local resource AssetDatabase. refresh ();} // redraw void OnInspectorUpdate () {Repaint () ;}} when the interface changes ();}}

First, let the script inherit from EditorWindow, so that it will display a window in Unity3D. In the OnGUI () method, we define the content to be drawn in the window as a text box, two selection boxes, and a button. When you click the button, the CreatePrefab () method is executed. When the interface changes, you need to re-paint the window. The final program demonstration effect is as follows:

After selecting an object in the scene, you only need to fill in the name, tag, and Layer of the preset object to directly generate the Prefab. However, there is a problem here, to generate a Prefab file, you must pass in a GameObject. Therefore, if you directly select the content in the project resource folder, an error may be reported because you choose not a GameObject. The original intention of the blogger to make such a function was to directly generate a preset file for each sprite image. Now it seems that you need to find other methods, but the basic idea is to create an empty object, then add a sub-object to this empty object. If you are interested in this, you can use the methods in this article to try it yourself.

Case 3: quickly set gallery tags for Sprite

In this case, it is also related to textures. We know that before UGUI is available, the first thing we need to do when using NGUI is to map the textures to be used, now in Unity3D, we can use the Packing Tag of the texture to package the gallery. That is to say, objects with the same Packing Tag will be pushed to a large image. The advantage of doing so is to save resources. If you are not familiar with this part of content, you can understand my article. Now that we understand the principle, why don't we try to complete it once through a program? Okay. The code is provided directly:

Using UnityEngine; using UnityEditor; using System. collections; public class PackageTools: EditorWindow {// <summary> // gallery tag /// </summary> private string tagName; /// <summary> /// current instance /// </summary> private static PackageTools instance; /// <summary> /// implement interface customization in the OnGUI method /// </summary> private void OnGUI () {// draw a text box tagName = EditorGUILayout. textField ("PackageTagName:", tagName); // draw a button if (GUI Layout. Button ("Package", GUILayout. Height (20) {if (tagName! = String. empty) {PackgeTextureWidthTag (tagName) ;}}/// <summary> // display the plug-in window /// </summary> [MenuItem ("Tools/ShowPackageTools")] static void ShowPackageTools () {// obtain the current window instance = EditorWindow. getWindow <PackageTools> (); // display the window instance. show () ;}/// <summary> /// quickly generate an image gallery /// </summary> static void PackgeTextureWidthTag (string tagName) {// obtain all selected Object [] selection = (Object []) Selection. objects; // if (selection. length = 0) return; // batch process the Texture foreach (Object obj in selection) {// obtain each texture Texture = (texture) obj; // obtain the texture path string localpath = AssetDatabase. getAssetPath (texture); // import TextureImporter importer = (TextureImporter) AssetImporter. getAtPath (localpath); // determines the Texture type. Only Sprite and SpriteMode if (importer. textureType = TextureImporterType. sprite) {importer. spritePackingTag = tagName; // import the project resource AssetDatabase. importAsset (localpath) ;}// refresh the local resource AssetDatabase. refresh ();} // redraw void OnInspectorUpdate () {Repaint () ;}} when the interface changes ();}}

Because the packaging gallery only requires one parameter, this packaging tool only requires one text box and one button. The entire process is the same as that in Case 2, so we will not analyze it here. The demo of this extension is as follows:

Well, this is what we are today. Today's content basically covers the basic content of developing extended programs for Unity3D, what we need to do next is to actively find and solve problems in daily life, work, and study. "How to teach people to fish is better than to teach people to fish" and teach others knowledge and skills, this is a pleasure for the bloggers, and the bloggers hope to enjoy the content today. Okay, thank you!

Welcome to the independent blog of the blogger. My blog address is http://www.qinyuanpei.com.

Daily proverbs

The beauty is at the front, and it is hard to move forward, so do not stop. I don't know where to go, but I am on the road.

(Can the CSDN MarkDown be ugly ...............)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.