当前位置:   article > 正文

Unity 控制脚本中的变量是否在Inspector视图中显示_unity中开发script inspector,如何根据条件动态确定是否在 inspector中显

unity中开发script inspector,如何根据条件动态确定是否在 inspector中显示public

默认情况

默认情况下, 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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

在这里插入图片描述

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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

在这里插入图片描述

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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

在这里插入图片描述

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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

在这里插入图片描述

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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

在这里插入图片描述

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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/140741
推荐阅读
相关标签
  

闽ICP备14008679号