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

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

Parameter values are not getting assigned in class constructor

问题

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

  1. [field: SerializeField]
  2. public ulong StallID { get; private set; }
  3. [field: SerializeField]
  4. 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}");

  1. BackWallTextureID = backWallTextureID;
  2. LeftWallTextureID = leftTextureID;
  3. RightWallTextureID = rightTextureID;
  4. FloorTextureID = floorTextureID;
  5. }
  6. [field: SerializeField]
  7. public int BackWallTextureID { get; private set; }
  8. [field: SerializeField]
  9. public int LeftWallTextureID { get; private set; }
  10. [field: SerializeField]
  11. public int RightWallTextureID { get; private set; }
  12. [field: SerializeField]
  13. public int FloorTextureID { get; private set; }

}

英文:

My Class

  1. [System.Serializable]
  2. public class StallData
  3. {
  4. public StallData(ulong stallID, stallSurfaceTextureData)
  5. {
  6. StallID = stallID;
  7. StallSurfaceTextureData = stallSurfaceTextureData;
  8. }
  9. [field: SerializeField]
  10. public ulong StallID{ get; private set; }
  11. [field: SerializeField]
  12. public StallSurfaceTextureData StallSurfaceTextureData { get; private set; }
  13. }
  14. [System.Serializable]
  15. public class StallSurfaceTextureData
  16. {
  17. public StallSurfaceTextureData( int floorTextureID, int leftTextureID, int right, int back)
  18. {
  19. Debug.LogFormat($" f {floorTextureID} l {leftTextureID} r {right} b {back}");
  20. BackWallTextureID = back;
  21. LeftWallTextureID = leftTextureID;
  22. RightWallTextureID = right;
  23. FloorTextureID = floorTextureID;
  24. }
  25. [field: SerializeField]
  26. public int BackWallTextureID { get; private set; }
  27. [field: SerializeField]
  28. public int LeftWallTextureID { get; private set; }
  29. [field: SerializeField]
  30. public int RightWallTextureID { get;private set; }
  31. [field: SerializeField]
  32. public int FloorTextureID { get; private set; }
  33. }

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

  1. {
  2. "StallID": 889448,
  3. "StallSurfaceTextureData":
  4. {
  5. "BackWallWallTextureID": 0,
  6. "LeftWallWallTextureID": 1,
  7. "RightWallWallTextureID": 2,
  8. "FloorTextureID": 3
  9. }
  10. }

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

  1. [System.Serializable]
  2. public class StallSurfaceTextureData
  3. {
  4. public StallSurfaceTextureData( int floorTextureID, int leftWallTextureID, int rightWallTextureID, int backWallTextureID)
  5. {
  6. Debug.LogFormat($" f {floorTextureID} l {leftWallTextureID} r {rightWallTextureID} b {backWallTextureID}".ToGreen());
  7. BackWallTextureID = backWallTextureID;
  8. LeftWallTextureID = leftWallTextureID;
  9. RightWallTextureID = rightWallTextureID;
  10. FloorTextureID = floorTextureID;
  11. }
  12. [field: SerializeField]
  13. public int BackWallTextureID { get; private set; }
  14. [field: SerializeField]
  15. public int LeftWallTextureID { get; private set; }
  16. [field: SerializeField]
  17. public int RightWallTextureID { get;private set; }
  18. [field: SerializeField]
  19. public int FloorTextureID { get; private set; }
  20. }

答案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:

确定