Unity Editor Basics (6): Property Drawers Custom Property Drawing

Source address: http://gad.qq.com/article/detail/33468

This article will introduce you to the custom property drawing of Property Drawers, you can create a new project or use the previous project, and then create two C# scripts in the Scripts folder, named: "Persion.cs " and "ShowPersionInfo.cs", as shown in the following image:



Then create a script named "PersionPropertiesDrawer.cs" in the Editor folder, as shown below:






First, open our "Persion.cs" script and add the following code to it:



This code does not need to be explained, it is a common class and enumeration. Next add the following code to our "ShowPersionInfo.cs" script:



Why has to be this way? I believe everyone knows that in order to attach a script to a game object, the script must inherit from MonoBehaviour. You can hang "Persion.cs" on the game object, and it will appear as shown below:



So "ShowPersionInfo.cs" is just an auxiliary class, its role is to hook our "Persion.cs" to the game object.


Alright, let's create an empty game object, name it "Persion", and add the "ShowPersionInfo.cs" script to it:



Hey, Kesen, are you cheating on me? It's nothing! ! It turned out that we missed a piece of code, let's make it up:



It turns out that in order to display the properties of a common class in the Inspector panel, the common class must be serialized.


Alright, let's go back to Unity and see what's changed.



Perfect! The properties in the Persion class are successfully displayed on the Inspector panel.


Well, let's briefly understand what serialization is, as shown in the following figure:


(The picture comes from Baidu Encyclopedia)


The simple understanding is that when a class is serialized, the value is read from the property, saved in a certain format, and transmitted to another place. Well, this process is handed over to the Unity engine to implement, and a simple understanding will do (that is, you can use it).


The next step is the core of this tutorial! ! !


First open our "PersionPropertiesDrawer.cs" script and add the following code to it:



Let our "PersionPropertiesDrawer.cs" inherit from the PropertyDrawer class, and then override the OnGUI and GetProperties functions.


Add [CustomPropertyDrawer(typeof(Persion))] to specify that this class is used for drawing custom properties.


Next, let's test what the parameters passed in these methods do. Add the following code to our script:



Ok, now go back to Unity and look at the test data:



The following points can be seen from the above data:

    1. The property parameter in OnGUI and GetPropertyHeight is the same parameter. This parameter stores the attribute information in the Persion.


    2. The Label parameter in OnGUI and GetPropertyHeight is also the same parameter, which stores the class name of the Persion class.


    3. The position parameter refers to the area information that needs to be drawn in the Inspector panel, which can be briefly understood from the following two figures:

    



PS: For testing, Kesen commented some code and added a 2D rigid body component.


By the way, there is one place Kesen missed, and that is a row height of 16 in the Inspector panel. We can know from the figure below.



Well, let's draw the properties of our Persion next. Our goal is shown below:


    

Take a look at our analysis chart below:



Ok, let's start coding our code, open "PersionPropertiesDrawer.cs", and add the following code to it:



The code above has been analyzed by Kesen. There may be some small partners who are dizzy in two places, that is, [Get the corresponding serialization attribute] and [Drawing attribute]. In fact, it is very simple, as can be understood from the following two figures:



Well, let's go back to Unity and see if our effect is achieved:



OK, that's perfectly done.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325699737&siteId=291194637
Recommended