【问题标题】:Class won't serialize into JSON using unity's JSONUtility类不会使用统一的 JSONUtility 序列化为 JSON
【发布时间】:2016-06-04 03:16:33
【问题描述】:

我有以下类,它是可序列化的,并且只有字符串作为字段:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[System.Serializable]
public class Cabeza
{

    string id, urlOBJ, urlTextura, pathOBJbajado, pathTexturaBajada;

    public string Id { get; set; }
    public string UrlOBJ { get; set; }
    public string UrlTextura { get; set; }
    public string PathOBJbajado { get; set; }
    public string PathTexturaBajada { get; set; }


    public Cabeza (string nuevoId)
    {
        Id = nuevoId;
        UrlOBJ =  nuevoId +".obj";
        UrlTextura =  nuevoId + ".png";

    }


}

据我所知,应该可以从中获取 JSON...但是,JsonUtility.ToJson() 仅返回 { }。这怎么可能?我错过了什么?

【问题讨论】:

  • 我认为它正在序列化您的字段,这些字段没有值,因此不会出现在序列化结果中。
  • 你能显示你调用ToJson(...)的代码吗?
  • @Maarten UrlOBJ ad UrlTextura 应该有值。

标签: c# json unity3d unity5


【解决方案1】:

The documentation 提到(但没有明确).ToJson() 序列化字段,而不是属性。

我认为以下代码可以按您的意愿工作:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[System.Serializable]
public class Cabeza
{
    public string Id;
    public string UrlOBJ;
    public string UrlTextura;
    public string PathOBJbajado;
    public string PathTexturaBajada;

    public Cabeza (string nuevoId)
    {
        Id = nuevoId;
        UrlOBJ =  nuevoId +".obj";
        UrlTextura =  nuevoId + ".png";
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多