Unity Editor Basics (2): Customizing the Inspector Panel

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

Let me introduce you to the custom Inspector view panel. Students who don't know it can take a look.


Or use the Unity project from the previous article, then create a new C# script in the Scripts folder, name it "Player", then double-click to open the script, and add the following code to it:



The Player class records some basic information of the Player, such as: ID, name, background story, health, damage and so on.


Some basics and considerations for customizing the Inspector property panel are shown in the following figure:



Portal: http://www.ceeger.com/Script/Editor/Editor.html


Next, we started to make our own Inpector. For the custom Inpector panel, you can refer to the API in the following figure:



Portal: http://www.ceeger.com/Script/EditorGUILayout/EditorGUILayout.BeginVertical.html


Now, please create a new C# script in the Editor folder, double-click to open the script, and add the following code to it:




Okey, let's analyze it one by one. Although it has been remarked, Kesen feels that it is not very clear.


The layouts made in this pair of brothers are arranged in a vertical direction.



As shown in the image above, the entire page elements are laid out in a vertical orientation.


The layouts made in this pair of brothers are arranged in a horizontal direction.


Since we used the Horizontal brothers in the place circled above, the elements in the brothers are all arranged in a horizontal direction.


Use EditorGUILayout.Space() to space a line between two elements.





Their rule is that the method name ends with Field, and everyone can choose the corresponding method according to the type of drawing.


Generally, the parameters in the parentheses, the first is the name of the field to be drawn (string type), and the second is the value of the field to be drawn, as shown below:




1 is the label field

2 is an integer field

3 is the text field

4. For the text area


Slider: EditorGUILayout.Slider()



EditorGUILayout.Slider() is used to draw a slider, as can be seen from the above:


  1. The first parameter is the name of the slider

  2. The second parameter is the value of the slider to change

  3. The third and fourth parameters are the range of the slider


Results as shown below:



Progress bar: EditorGUI.ProgressBar()





EditorGUI.ProgressBar() is used to draw a progress bar, as can be seen from the above:


  1. The first parameter is to set the size of the progress bar, the type is a Rect.

  2. The second parameter is to set the displayed value,

  3. The third parameter is to set the name of the progress bar


hint:

    1. For the first parameter, we use the GetRect() method of the GUILayoutUtility.GetRect() tool class to return a set rectangle. In the case, we set a 50*50 size rectangle.


    2. For the second parameter, we use player.health/100.0f. That's because the maximum value of the progress bar is 1. If you don't divide by 100, when the slider value is 1, the progress bar will fill up, so we want to synchronize the value with the scale of the progress bar, then divide by 100. (The language is not good, I don't know how to explain it).


Effect picture:



Help Box: EditorGUILayout.HelpBox()



EditorGUILayout.HelpBox() is used to draw a box (which can also be regarded as a rectangular box), and then display prompt information inside the box, as can be seen from the above figure:


  1. The first parameter is the incoming prompt information

  2. The second parameter is the type of the prompt message


renderings;


error type


normal type


type of warning



Well, at this point, this article is over. You can use the knowledge you just learned to simply design your own Inspector panel properties and experience the experience. The following tutorials will be more exciting, so stay tuned.

Guess you like

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