C# 为什么在Unity 5.3的对象类中使用setter和getter时返回空JSON字符串?

C# 为什么在Unity 5.3的对象类中使用setter和getter时返回空JSON字符串?,c#,json,unity3d,C#,Json,Unity3d,我尝试在Unity 5.3中使用新的JSON序列化功能,并通过参考Unity网站上提供的使用示例编写了以下代码。我所做的唯一不同的部分是通过使用setter和getter创建对象类的变量(在我的例子中是FruitItem类),而不是使它们成为纯公共变量。通过这样做,我只得到了一对括号,里面没有任何内容。但是,如果我删除getter和setter并使类变量成为纯公共变量,我将能够得到正确的结果。有谁能给我一些提示,为什么会这样?提前感谢你的帮助 正常工作的代码: using UnityEngine

我尝试在Unity 5.3中使用新的JSON序列化功能,并通过参考Unity网站上提供的使用示例编写了以下代码。我所做的唯一不同的部分是通过使用setter和getter创建对象类的变量(在我的例子中是FruitItem类),而不是使它们成为纯公共变量。通过这样做,我只得到了一对括号,里面没有任何内容。但是,如果我删除getter和setter并使类变量成为纯公共变量,我将能够得到正确的结果。有谁能给我一些提示,为什么会这样?提前感谢你的帮助

正常工作的代码:

using UnityEngine;
using UnityEditor;
using System.Collections;
using System;

public class testJson : MonoBehaviour {


    // Use this for initialization
    void Start () {

        FruitItem myFruit = new FruitItem (){ name = "apple", price = 52, quantity = 53 };

        string jsonString = JsonUtility.ToJson (myFruit);
        Debug.Log (jsonString);

    }



    // Update is called once per frame
    void Update () {

    }
}

[Serializable]
public class FruitItem{

    //using the pure public variables and the output will be:
    //{"name":"apple","quantity":53,"price":52}

    public string name;
    public int quantity;
    public int price;

}
using UnityEngine;
using UnityEditor;
using System.Collections;
using System;

public class testJson : MonoBehaviour {


    // Use this for initialization
    void Start () {

        FruitItem myFruit = new FruitItem (){ name = "apple", price = 52, quantity = 53 };

        string jsonString = JsonUtility.ToJson (myFruit);
        Debug.Log (jsonString);

    }



    // Update is called once per frame
    void Update () {

    }
}

[Serializable]
public class FruitItem{

    //using the pure public variables and the output will be:
    //{}

    public string name{ get; set;}
    public int quantity{ get; set;}
    public int price{ get; set;}

}
无法正常工作的代码:

using UnityEngine;
using UnityEditor;
using System.Collections;
using System;

public class testJson : MonoBehaviour {


    // Use this for initialization
    void Start () {

        FruitItem myFruit = new FruitItem (){ name = "apple", price = 52, quantity = 53 };

        string jsonString = JsonUtility.ToJson (myFruit);
        Debug.Log (jsonString);

    }



    // Update is called once per frame
    void Update () {

    }
}

[Serializable]
public class FruitItem{

    //using the pure public variables and the output will be:
    //{"name":"apple","quantity":53,"price":52}

    public string name;
    public int quantity;
    public int price;

}
using UnityEngine;
using UnityEditor;
using System.Collections;
using System;

public class testJson : MonoBehaviour {


    // Use this for initialization
    void Start () {

        FruitItem myFruit = new FruitItem (){ name = "apple", price = 52, quantity = 53 };

        string jsonString = JsonUtility.ToJson (myFruit);
        Debug.Log (jsonString);

    }



    // Update is called once per frame
    void Update () {

    }
}

[Serializable]
public class FruitItem{

    //using the pure public variables and the output will be:
    //{}

    public string name{ get; set;}
    public int quantity{ get; set;}
    public int price{ get; set;}

}

Unity无法序列化属性

使用的序列化系统可以执行以下操作:

  • 可以序列化公共非静态字段(可序列化类型)
  • 可以序列化用[SerializeField]属性标记的非公共非静态字段
  • 无法序列化静态字段
  • 无法序列化属性
只有在Unity可以序列化的类型下,您的字段才会序列化:

可序列化类型包括:

  • 从UnityEngine.Object继承的所有类,例如GameObject、Component、MonoBehavior、Texture2D、AnimationClip
  • 所有基本数据类型,如int、string、float、bool
  • 一些内置类型,如Vector2、Vector3、Vector4、四元数、Matrix4x4、Color、Rect、LayerMask
  • 可序列化类型的数组
  • 可序列化类型的列表
  • 列举
  • 结构
  • 列表项
编辑: 仅支持普通类和结构;从UnityEngine.Object派生的类(如MonoBehavior或ScriptableObject)不可用


感谢您的回复和提供的参考,Mattias。我没有意识到unity序列化支持的类型有限制。但是,“使用系统”引用了关键字“[Serializable]”。它看起来像一个.Net的东西。我想知道对Unity设置的序列化限制是否会影响到这一点。再次感谢。谢谢你的回答。我不确定这是否仍然准确。根据最新的文档:“只支持普通类和结构;不支持从UnityEngine.Object派生的类(如MonoBehavior或ScriptableObject)。”您的可序列化类型列表中的第一个项目符号现在似乎不正确。