在Unity Inspector中显示class变量
2016-12-16 11:59
435 查看
通过Unity Inspector,我们能够很方便的给脚本中变量赋值。变量要在Inspector中显示,需要满足下面两个条件:
1. 变量是内置类型的,比如float, string, int, double类型的变量
2. 变量访问限制为public
例如如下脚本:
[csharp] view
plain copy
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour
{
public float f;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
}
在Inspector中显示为这样:
如果我们想要显示在Inspector中一个custom class 类型的变量呢?比如:
[csharp] view
plain copy
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour
{
public float f;
public Person person;
public class Person
{
public string name;
public string address;
public int age;
}
void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
}
在Inspector中显示为:
可以看到,Person类型的变量并没有在Inspector中显示。为了显示person变量,我们可以采用下面的方法,在Person class类型声明前面加上[System.Serializable]:
[csharp] view
plain copy
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour
{
public float f;
public Person person;
[System.Serializable]
public class Person
{
public string name;
public string address;
public int age;
}
void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
}
现在在Inspector视图中就能看到Person变量显示了:
Unity Inspector会默认显示脚本中的public变量,有时我们不想让这些变量显示,则可以在变量前面加上[HideInInspector],这样就能隐藏这个变量了。
[csharp] view
plain copy
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour
{
[HideInInspector]
public float f;
public Person person;
[System.Serializable]
public class Person
{
public string name;
public string address;
public int age;
}
void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
}
1. 变量是内置类型的,比如float, string, int, double类型的变量
2. 变量访问限制为public
例如如下脚本:
[csharp] view
plain copy
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour
{
public float f;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
}
在Inspector中显示为这样:
如果我们想要显示在Inspector中一个custom class 类型的变量呢?比如:
[csharp] view
plain copy
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour
{
public float f;
public Person person;
public class Person
{
public string name;
public string address;
public int age;
}
void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
}
在Inspector中显示为:
可以看到,Person类型的变量并没有在Inspector中显示。为了显示person变量,我们可以采用下面的方法,在Person class类型声明前面加上[System.Serializable]:
[csharp] view
plain copy
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour
{
public float f;
public Person person;
[System.Serializable]
public class Person
{
public string name;
public string address;
public int age;
}
void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
}
现在在Inspector视图中就能看到Person变量显示了:
Unity Inspector会默认显示脚本中的public变量,有时我们不想让这些变量显示,则可以在变量前面加上[HideInInspector],这样就能隐藏这个变量了。
[csharp] view
plain copy
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour
{
[HideInInspector]
public float f;
public Person person;
[System.Serializable]
public class Person
{
public string name;
public string address;
public int age;
}
void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
}
相关文章推荐
- unity中可以使一个私有的变量在Inspector面板中显示出来 和 SerializeField的使用
- 将类对象显示在Unity的Inspector面板上
- 【Unity】【技巧】如何在代码里面引用到Inspector里面显示的值
- 讲解如何在Unity的Inspector面板中用滑动条来控制变量的大小
- Unity根据条件控制Inspector面板中的属性显示
- Unity控制在面板上显示变量
- Unity入门(3)常用函数总结(移动/脚本间变量传值/ui text中显示变量
- Unity曝露变量的显示与隐藏
- Unity中私有变量如何显示在Inpsector面板中?
- [Unity]在inspector视图下 分类 公共变量
- 【Unity编辑器】面板中显示Private修饰的变量
- Unity根据条件控制Inspector面板中的属性显示
- 4. 在Inspector面板中显示类中变量+ 拓展编辑器
- 个人对unity inspector面板中不显示二维数组的解决方法
- eclipse 调试jdk源码class文件,变量无法显示问题
- [Unity&C#]变量太多,如何模块化显示
- 如何在ogre游戏界面添加自己的信息,比如显示某变量的值
- CLASS_ALV_F4_LISTBOX ALV显示界面下拉LIST
- 在编辑状态下变量或函数不能显示提示时如何打开提示功能(或编程助手)
- ClassView 不能正常显示