亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

System.Serializable在Unity中的List <MyClass>上不起作用?

System.Serializable在Unity中的List <MyClass>上不起作用?

C#
慕絲7291255 2021-05-11 09:22:36
我正在創建一個Loot系統。我幾乎快結束了,只剩下DropTable在Enemy腳本中的Inspector中填寫了。由于某種原因,我的DropTable腳本正在序列化,但是我的LootDrop班級卻沒有。我的班級基本上是這樣設置的:DropTable 班級:[System.Serializable]public class DropTable{ public List<LootDrop> loot = new List<LootDrop>(); public Item GetDrop() {    int roll = Random.Range(0, 101);    int chanceSum = 0;    foreach (LootDrop drop in loot)    {        chanceSum += drop.Chance;        if (roll < chanceSum)        {            return ItemDatabase.Instance.GetItem(drop.itemSlug); //return the item here        }    }        return null; //Didn't get anything from the roll }}LootDrop 班級:[System.Serializable]public class LootDrop{    public string itemSlug { get; set; }    public int Chance { get; set; }}本質上,我DropTable只包含的列表LootDrop。但是,我無法從檢查器訪問其中的各個LootDrop實例List<LootDrop>。我要做的就是public DropTable在Enemy腳本上創建一個變量。我覺得我以前做過類似的事情,沒有問題。我在這里做錯什么了嗎?我真的很想DropTable成為與我的敵人不同的班級,因為敵人不應該真正在乎該GetDrop()方法。但是,如果那是唯一的方法,那么我想它必須這樣做。在這個問題上的任何幫助將不勝感激。
查看完整描述

2 回答

?
Helenr

TA貢獻1780條經驗 獲得超4個贊

Unity將序列化字段,而不是屬性??梢郧袚Q到以下字段:


[Serializable]

public class LootDrop

{

    public int Chance;

}

或使用序列化的后備字段:


[Serializable]

public class LootDrop

{

    public int Chance

    {

        get { return _chance; }

        set { _chance = value; }

    }


    [SerializeField]

    private int _chance;

}


查看完整回答
反對 回復 2021-05-23
?
慕俠2389804

TA貢獻1719條經驗 獲得超6個贊

您應loot在嘗試添加項目之前初始化變量。


[System.Serializable]

public class DropTable

{

    public List<LootDrop> Loot;


    public DropTable()

    {

        Loot = new List<LootDrop>();

    }

}

另外,請謹慎使用名稱約定。


查看完整回答
反對 回復 2021-05-23
  • 2 回答
  • 0 關注
  • 292 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號