【问题标题】:I have some trouble with nested class in UnityUnity 中的嵌套类有一些问题
【发布时间】:2019-10-17 02:44:01
【问题描述】:

我想我有一些问题尝试解释我的意思,所以我要解释我做什么以及我的代码从一开始就变成了什么。

我有这段代码可以将我的数据保存到 json,它工作正常,完美打印 json

脚本:GiftValue

[SerializeField]
 public class Gift
 {
    public int GValue 
    public string GName 
    public int GAmount
    public float GWeight
 }

脚本:EditAddGift

 [SerializeField]
 public List<Gifts> _Gift = new List<Gifts>();

///those code above is what i missing before.
 Gifts NewGift = new Gifts
    {
        GValue = DropDownValueGift,
        GName = ifGiftDetailName.text,,
        GAmount = iAmount,
        GWeight = fWeight,


    };
_Gift.Add(NewGift);
SaveToJson(_Gift, DATABASE_NAME);

在我意识到我的班级需要拆分为 GiftDetail 和 Gift 之前,这是我的代码。 之后我意识到我需要将“礼物”分成两部分,“礼物”和“礼物细节”。 Soo 我在谷歌上发现了嵌套类,一段时间后我的 Script GiftValue 变成了这个

[System.Serializable]
public class GiftDetail
{
    public int GDetailValue 
    public string GDetailName 
    public double GDetailWeight 
    public int GDetailAmount 
}

[System.Serializable]
public class Gift
{
    public int GValue 
    public string GName 
    public List<GiftDetail> GiftDetail 
}

更改后我无法编写代码,因为我只知道如何像这样调用普通类:

礼物 NewGift= 新礼物

{ ...... }

这就是我尝试做的事情

        GiftDetail NewGiftDetail = new GiftDetail
        {

            GDetailValue = DropDownValueGiftDetail,
            GDetailAmount = iAmount,
            GDetailName = ifGiftDetailName.text,
            GDetailWeight = Fweight,
        };



        Gifts NewGift = new Gifts
        {
            GValue = DropDownValueGift,
            GName = txtDropDownGift,


            //What i want to know is how to change  NewGiftDetail
            //or change class Gift or class GiftDetail or....
            // so i can put GiftDetail in here



           //GiftDetail = NewGiftDetail,// let just delete this,
           // because i don't know how to call this
        };

所以最后我的 json 文件看起来像这样

    [
      {
        "GValue": 1,
        "GName": "Gift 3",
        "GiftDetail": 
        [
        {
          "GDetailValue": 3,
          "GDetailName": "2",
          "GDetailWeight": 3.0,
          "GDetailAmount": 4
        },
        {
          "GDetailValue": 3,
          "GDetailName": "2",
          "GDetailWeight": 3.0,
          "GDetailAmount": 4
        }

        ],
      },
]

我之前尝试过某种方式,例如将 Gift 更改为 public class Gift&lt;T&gt; blah blah .. 但我认为我应该在它垮台之前展示我的代码,以帮助你们更清楚我想要实现的目标


在更改为 Gift 和 GiftDetail 之前我用来删除的代码

            if (_Gift.Exists(x => x.GValue== DropDownValuePhanQua ))
            {  

                int i = 0;        
                while (i <_Gift.Count && _Gift[i].GValue != DropDownValuePhanQua)
                        i++;
                _Gift.RemoveAt(i);

            }
            _Gift.Add(NewGift);
            SaveToJson(_Gift, DATABASE_NAME);

我改成这个

if (_Gift.Exists(x => x.GValue== DropDownValuePhanQua ))
///still don't know how to get GDetailValue , it only let me access to x.GiftDetail
                {  
                    GiftDetail NewGiftDetail3 = new GiftDetail
                    {
                        GDetailValue = DropDownValuePhanQua,
                        GDetailAmount = iSoLuongPhanQua,
                        GDetailName = ifTenPhanQua.text,
                        GDetailWeight = fTileRaPhanQua,
                    };
                    int i = 0;        
                    while (i <_Gift.Count && _Gift[i].GValue != DropDownValuePhanQua)
                            i++;
                    NewGift.GiftDetails.Add(NewGiftDetail3);
                    _Gift.RemoveAt(i);
                }
                _Gift.Add(NewGift);
                SaveToJson(_Gift, DATABASE_NAME);

如果我使用关于它的代码将返回 json 但它会继续替换第二个 GiftDetail

 [ {
        "GValue": 1,
        "GName": "Phần Quà 1",
        "GiftDetails": [
          {
            "GDetailValue": 5,
            "GDetailName": "3",
            "GDetailWeight": 2.0,
            "GDetailAmount": 4
          },
          {
            "GDetailValue": 6,
            "GDetailName": "Old One",
            "GDetailWeight": 33.0,
            "GDetailAmount": 3
          }
        ]
      }
]

到这里

[
{
    "GValue": 1,
    "GName": "Phần Quà 1",
    "GiftDetails": [
      {
        "GDetailValue": 5,
        "GDetailName": "3",
        "GDetailWeight": 2.0,
        "GDetailAmount": 4
      },
      {
        "GDetailValue": 3,
        "GDetailName": "new Gift",
        "GDetailWeight": 33.0,
        "GDetailAmount": 3
      }
    ]
  }
]

【问题讨论】:

  • 你能把一辆车变成多辆车吗? Gift 接受 List&lt;GiftDetail&gt;,而您提供的是单个 GiftDetail 对象。
  • 我确实尝试将 GiftDetail NewGiftDetail = new GiftDetail 更改为 List NewGiftDetail = new List 它解决了该错误,但它告诉我我在 GiftDetail 中没有 GDetailValue
  • 大概是告诉你List&lt;GiftDetail&gt; 中没有GDetailValue,而不是GiftDetail 中没有GDetailValue。请仔细阅读编译器错误。 9/10 他们会告诉你到底出了什么问题。我这么说是因为有人盯着编译器警告看了 5 分钟,却看不出过去出了什么问题。
  • 我不好,我刚回来查看,它确实说 List,我了解错误以及它告诉我要更改的内容,问题是我不知道如何解决它。跨度>
  • 你最后的作业没有意义。首先,代码编写得好像Gift 是一个泛型类型,但这不是您在此处显示的Gift 类。其次,您将NewGiftDetail 分配给GiftDetail 属性。前者是GiftDetail 的单个实例,而后者是List&lt;GiftDetail&gt;。自然,您不能将一个分配给另一个。也许你的意思是GiftDetail = new List&lt;GiftDetail&gt; { NewGiftDetail }?至少,如果您要发布带有问题的代码(并且您应该),那么该代码应该是您正在使用的实际代码。

标签: c# json unity3d


【解决方案1】:

有几种方法。这是一个。

拥有

[System.Serializable]
public class GiftDetail
{
    public int GDetailValue;
    public string GDetailName;
    public double GDetailWeight;
    public int GDetailAmount;
}

[System.Serializable]
public class Gift
{
    public int GValue;
    public string GName;
    public List<GiftDetail> GiftDetails; // Added 's'
}

你可以

GiftDetail NewGiftDetail = new GiftDetail
{
    GDetailValue = DropDownValueGiftDetail,
    GDetailAmount = iAmount,
    GDetailName = ifGiftDetailName.text,
    GDetailWeight = Fweight,
};

GiftDetail NewGiftDetail2 = new GiftDetail
{
    //(...)
};

Gift NewGift = new Gift  //Gift - not gifts
{
    GValue = 1,
    GName = "One",
    GiftDetails = new List<GiftDetail>() { NewGiftDetail, NewGiftDetail2 }
};

GiftDetail d3 = new GiftDetail
{
    //(...)
};

NewGift.GiftDetails.Add(d3);
// NewGift has now 3 details
//Access first detail
NewGift.GiftDetails[0]; // Or .First() with 'using System.Linq`

【讨论】:

  • 为了让它再次与 Unity 一起工作,但是你应该而不是属性坚持字段(删除所有 { get; set; })并创建类 [Serializable]
  • 谢谢!我更新了代码。 (这里没有 Unity 经验)
  • @tymtam 谢谢^_^,这就是我想要的。还有一件事,你知道如何访问 GiftDetails.GDetailValue 吗? NewGift 只允许我访问 NewGift.GiftDetails。我需要 GDetailValue 从 _Gift 中删除礼物并在礼物已经退出时添加新礼物
  • 我只是在将 Gift 更改为 Gift 和 GiftDetail 之前添加删除代码
  • 对不起你的代码工作,但它不会让我添加新的,只能替换旧的.-.-。我添加了我在帖子中使用的代码
【解决方案2】:
List<GiftDetail> GiftDetailList =new List<GiftDetail>()
var GD1 = new GiftDetailList ()
{
          GDetailValue= 3,
          GDetailName= "2",
          GDetailWeight= 3.0,
          GDetailAmount= 4
}

同样地创建尽可能多的细节,然后

    Gifts NewGift = new Gifts
    {
        GValue = DropDownValueGift,
        GName = txtDropDownGift,    
       GDteails=GiftDetailList 
    };

【讨论】:

  • = new GiftDetailList .. 这种类型不存在,但它是您的 List&lt;GiftDetail&gt; 的名称 .. 还添加一些 ; 以减少混淆 ;) 我猜你提到类似 List&lt;GiftDetail&gt; GiftDetailList =new List&lt;GiftDetail&gt; { new GiftDetail () { GDetailValue= 3, GDetailName= "2", GDetailWeight= 3.0, GDetailAmount= 4 } /*, more GiftDetails here*/ };
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多