unity3d 在Rider中创建的公共变量未在Unity中显示

uqjltbpv  于 2023-01-31  发布在  其他
关注(0)|答案(1)|浏览(298)

我刚开始使用Unity(VSC附带的),但我在使用IntelliJ IDEA等JetBarin产品方面有更好的经验,所以我去切换到Rider。然而,我现在无法将公共变量(int,float,GameObject)连接到我的Unity项目。
我试着更新骑士和改变一些设置,但没有明智的。
更新:有(明显的)要求我的代码看到确切的问题,所以我希望这有助于澄清问题一点点:
Code written in VSC
The resulting public variables showing up in Unity
Similar code written using Rider
No interactive variables showing up in Unity

qxsslcnc

qxsslcnc1#

Unity只序列化MonoBehaviours的字段(不是getset的属性)。除非有[System.NonSerialized]属性,否则所有公共字段都被序列化。
不要与[HideInInspector]属性混淆,它在检查器中不可见(如果您没有自定义检查器),但将被序列化。

class Foo
{
    // Bar won't be shown in the inspector or serialized.
    [System.NonSerialized]
    public int Bar = 5;
}

若要序列化非公共字段,请对基元类型(如int、float、bool)使用[SerializeField]属性。

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 isMale = true;
}

如果要序列化自己的类或结构,请使用[System.Serializable]属性。

[System.Serializable]
public struct PlayerStats
{
    public int level;
    public int health;
}

相关问题