gpt4 book ai didi

c# - 变量在脚本检查器窗口中不可见

转载 作者:行者123 更新时间:2023-12-04 02:09:44 24 4
gpt4 key购买 nike

几天前我开始使用 Unity,但我很困惑。
这是我当前的工作区:

这是应该的样子:

我应该能够填写变量,但我不能。

最佳答案

  1. 要在检查器中显示属性/变量,它们必须是public,否则它们将不会出现。

  2. 您还可以通过将它们归为 [SerializeField] 来查看私有(private)字段

  3. 要在检查器中查看自定义类,您应该将它们标记为[Serializable]

  4. 要对检查器隐藏公共(public)变量,请使用 [HideInInspector][System.NonSerialized] 属性。

这是一个示例:

public class SomePerson : MonoBehaviour 
{
//This field gets serialized because it is public.
public string name = "John";

//This field does not get serialized because it is private.
private int age = 40;

//This field gets serialized even though it is private
//because it has the SerializeField attribute applied.
[SerializeField]
private bool hasHealthPotion = true;

// This will be displayed in the inspector because the class has Serialized attribute.
public SomeCustomClass somCustomClassInstance;

// This will not be shown in inspector even if it is public.
[HideInInspector]
public bool hiddenBool;

// Same with this one.
[System.NonSerialized]
public int nonSerializedVariable = 5;
}

[System.Serializable]
public class SomeCustomClass
{
public string someProperty;
}

关于c# - 变量在脚本检查器窗口中不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39745357/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com