Unity 控制脚本中的变量是否在Inspector视图中显示

默认情况

默认情况下, p r i v a t e private private不显示, p u b l i c public public显示:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test : MonoBehaviour
{
    
    
    private Rigidbody2D rigidbody2D;
    private Animator animator;

    public float speed = 5f;
    public float jumpForce = 5f;
}

在这里插入图片描述

SerializeField

通过在 p r i v a t e private private前面或上面添加 [ S e r i a l i z e F i e l d ] [SerializeField] [SerializeField]可以让我们观察到私有变量的值:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test : MonoBehaviour
{
    
    
    [SerializeField]
    private Rigidbody2D rigidbody2D;
    private Animator animator;

    public float speed = 5f;
    public float jumpForce = 5f;
}

在这里插入图片描述

Space

[ S p a c e ] [Space] [Space]可以在 I n s p e c t o r Inspector Inspector视图中添加隔行:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test : MonoBehaviour
{
    
    
    [SerializeField]
    private Rigidbody2D rigidbody2D;
    private Animator animator;
    [Space]
    public float speed = 5f;
    public float jumpForce = 5f;
}

在这里插入图片描述

Header

[ H e a d e r ( … … ) ] [Header(……)] [Header()]相当于 I n s p e c t o r Inspector Inspector视图下的注释(分类):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test : MonoBehaviour
{
    
    
    [SerializeField]
    private Rigidbody2D rigidbody2D;
    private Animator animator;
    [Header("速度设定")]
    public float speed = 5f;
    public float jumpForce = 5f;
}

在这里插入图片描述

Range

[ R a n g e ( m i n , m a x ) ] [Range(min,max)] [Range(min,max)]可以给一个 i n t 、 f l o a t int、float intfloat变量限定取值范围:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test : MonoBehaviour
{
    
    
    [SerializeField]
    private Rigidbody2D rigidbody2D;
    private Animator animator;
    [Header("速度设定")]
    [Range(3f,7f)]
    public float speed = 5f;
    public float jumpForce = 5f;
}

在这里插入图片描述

HideInInspector

[ H i d e I n I n s p e c t o r ] [HideInInspector] [HideInInspector]可以让这个变量不显示在 I n s p e c t o r Inspector Inspector面板中,当你调整了某个变量并且不想再修改后,就可以让他隐藏起来:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test : MonoBehaviour
{
    
    
    [SerializeField]
    private Rigidbody2D rigidbody2D;
    private Animator animator;
    [Header("速度设定")]
    [Range(3f,7f)]
    public float speed = 5f;
    [HideInInspector]
    public float jumpForce = 5f;
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/xiji333/article/details/109490551