簡體   English   中英

在 Rider 中創建的公共變量不會顯示在 Unity 中

[英]Public variables created in Rider do not show up in Unity

我剛開始使用 Unity(VSC 附帶),但我在使用 JetBarin 產品(如 IntelliJ IDEA)時有更好的體驗,所以我轉而使用 Rider。 但是,我現在無法將公共變量(int、float、GameObject)連接到我的 Unity 項目。

我嘗試更新 Rider 並更改一些設置,但沒有任何效果。

更新:已經(明顯)要求我的代碼查看確切的問題,所以我希望這有助於稍微解決問題:

用 VSC 編寫的代碼

生成的公共變量顯示在 Unity 中

使用 Rider 編寫的類似代碼

Unity 中沒有顯示交互變量

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;
}

如果您想序列化自己的 class 或結構,請使用 [System.Serializable] 屬性。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM