参数值未在类构造函数中分配

huangapple go评论51阅读模式
英文:

Parameter values are not getting assigned in class constructor

问题

[System.Serializable]
public class StallData
{
public StallData(ulong stallID, StallSurfaceTextureData stallSurfaceTextureData)
{
StallID = stallID;
StallSurfaceTextureData = stallSurfaceTextureData;
}

[field: SerializeField]
public ulong StallID { get; private set; }

[field: SerializeField]
public StallSurfaceTextureData StallSurfaceTextureData { get; private set; }

}

[System.Serializable]
public class StallSurfaceTextureData
{
public StallSurfaceTextureData(int floorTextureID, int leftTextureID, int rightTextureID, int backWallTextureID)
{
Debug.LogFormat($"f {floorTextureID} l {leftTextureID} r {rightTextureID} b {backWallTextureID}");

    BackWallTextureID = backWallTextureID;
    LeftWallTextureID = leftTextureID;
    RightWallTextureID = rightTextureID;
    FloorTextureID = floorTextureID;
}

[field: SerializeField]
public int BackWallTextureID { get; private set; }

[field: SerializeField]
public int LeftWallTextureID { get; private set; }

[field: SerializeField]
public int RightWallTextureID { get; private set; }

[field: SerializeField]
public int FloorTextureID { get; private set; }

}

英文:

My Class

   [System.Serializable]
   public class StallData
   {
    public StallData(ulong stallID, stallSurfaceTextureData)
    {
      StallID = stallID;
      StallSurfaceTextureData = stallSurfaceTextureData;
    }
    
    [field: SerializeField]
    public ulong StallID{ get; private set; }

    [field: SerializeField]
    public StallSurfaceTextureData StallSurfaceTextureData { get; private set; }
}


[System.Serializable]
public class StallSurfaceTextureData
{
    public StallSurfaceTextureData( int floorTextureID, int leftTextureID, int right, int back)
    {
        Debug.LogFormat($" f {floorTextureID} l {leftTextureID} r {right} b {back}");

        BackWallTextureID = back;
        LeftWallTextureID = leftTextureID;
        RightWallTextureID = right;
        FloorTextureID = floorTextureID;
    }
    
    [field: SerializeField]
    public int BackWallTextureID { get; private set; }
    
    [field: SerializeField]
    public int LeftWallTextureID { get; private set; }
    
    [field: SerializeField]
    public int RightWallTextureID { get;private  set; }
    
    [field: SerializeField]
    public int FloorTextureID { get; private set; }
}

using newtonsoft-json when I am converting my JSON to this class I am getting back, left & right value 0 and for floor, I am getting the correct value.

I am unable to find the reason for it.

Unity version 2021.3.12f1

this is my JSON

{
  "StallID": 889448,
  "StallSurfaceTextureData": 
  {
    "BackWallWallTextureID": 0,
    "LeftWallWallTextureID": 1,
    "RightWallWallTextureID": 2,
    "FloorTextureID": 3
  }
}

I renamed the parameter name, and one time it worked then again I am getting the same issue.

Edit:
I have changed the parameter name in JSon but still the issue persists. The only way it is working is if I am keeping the parameter name and the variable name inside the class is same. I am not getting how the Parameter name is affecting the value assignment in the class?
for ex:
If I keep my class like this it will work

    [System.Serializable]
     public class StallSurfaceTextureData
     {
    public StallSurfaceTextureData( int floorTextureID, int leftWallTextureID, int rightWallTextureID, int backWallTextureID)
    {
        Debug.LogFormat($" f {floorTextureID} l {leftWallTextureID} r {rightWallTextureID} b {backWallTextureID}".ToGreen());

        BackWallTextureID = backWallTextureID;
        LeftWallTextureID = leftWallTextureID;
        RightWallTextureID = rightWallTextureID;
        FloorTextureID = floorTextureID;
    }
    
    [field: SerializeField]
    public int BackWallTextureID { get; private set; }
    
    [field: SerializeField]
    public int LeftWallTextureID { get; private set; }
    
    [field: SerializeField]
    public int RightWallTextureID { get;private  set; }
    
    [field: SerializeField]
    public int FloorTextureID { get; private set; }
}

答案1

得分: 1

Unity需要在JSON和类中的变量名称完全相同。
在JSON中你有BackWallTextureID,而在类中你有BackWallTextureId

英文:

Unity need to have the same to the letter variables name in JSON and class.
You have BackWallTextureID in JSON and BackWallTextureId in class.

答案2

得分: 0

我尝试了JsonConvert.DeserializeObject(json) - 没有任何问题,我猜这是因为它忽略了构造函数的输入参数,因为对于Newtonsoft.Json来说,它们不存在。如果你想要使用构造函数,输入参数应该与json相同(默认情况下不区分大小写)。

英文:

I tried JsonConvert.DeserializeObject(json) - no any problem, I guess because you constructor input parameters are ignored since they are not exist for Newtonsoft.Json. If you want to use a constructor, input parameters should be the same as json ( case insensitive by default).

huangapple
  • 本文由 发表于 2023年2月6日 17:02:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75359228.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定