This is the second day of my participation in the August Text Challenge.More challenges in August

Recommended reading

  • CSDN home page
  • GitHub open source address
  • Unity3D plugin sharing
  • Jane’s address book
  • My personal blog
  • QQ group: 1040082875

One, foreword

Unity has a powerful feature that allows you to modify values in the Inspector panel without any programming. This article covers everything we need to know to display our variables and custom classes in the Unity Inspector panel.

Second, display variables

Variables let’s create a C# script called “test.cs” that contains an int variable:

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {
    int something = 0;

    // Use this for initialization
    void Start (){}// Update is called once per frame
    void Update (){}}Copy the code

Now we create a new GameObject from the top menu (GameObject -> Create Null) and add the test.cs script to the GameObject. The Inspector panel now looks like this:

We can see that it has the test.cs script on it, but we don’t see something that has no variables at all.

Public variables let us modify our script by adding another variable, but this time with the public prefix. In a programming language, public means that the value can be seen by other classes. In Unity Public, this also means that variables are displayed in the Inspector panel.

Modified test.cs script:

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {
    int something = 0;
    public int i_am_public = 0;

    // Use this for initialization
    void Start (){}// Update is called once per frame
    void Update (){}}Copy the code

After saving it, here’s what the Inspector panel looks like now:

Now we can easily change variables without having to open the script again or do any programming.

Third, display class

Public class + variable

Our test script should now have an Address variable. Assuming we can also use an Address in other scripts, we can create a full class for it. Let’s get started:

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {
    int something = 0;
    public int i_am_public = 0;
    
    public Address address;

    // Use this for initialization
    void Start (){}// Update is called once per frame
    void Update (){}}public class Address {
    public string country = "";
    public string city = "";
    public string street = "";
    public int number = 0;
}
Copy the code

Now, if we save again and look at the Inspector panel, here’s what we see:

Even though we set the Address public variable in our exam class, we still don’t see it. The reason is that it is not a Unity class like “Public Class Test: MonoBehaviour” (every component needs: MonoBehaviour, but there is no reason for a simple class to be: MonoBehaviour).

Most people stop here and think it’s impossible to show our Address in the Inspector panel, but it is.

The trick is: If a class should appear in the Inspector, we simply write [System.erialable] above the class declaration

Here is the script we modified:

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {
    int something = 0;
    public int i_am_public = 0;
    
    public Address address;

    // Use this for initialization
    void Start (){}// Update is called once per frame
    void Update (){}} [System.Serializable]
public class Address {
    public string country = "";
    public string city = "";
    public string street = "";
    public int number = 0;
}
Copy the code

Now, if we save it and look again at the Inspector panel, this is what we get:

It’s that simple!