我有一些我需要序列化/反序列化的数据,但JsonUtility并没有做到它应该做的事情.这是我正在使用的对象:
public class SpriteData {
public string sprite_name;
public Vector2 sprite_size;
public List<Vector2> subimage;
}
public class SpriteDataCollection
{
public SpriteData[] sprites;
}
Run Code Online (Sandbox Code Playgroud)
如果我创建一个SpriteDataCollection,并尝试使用JsonUtility序列化它,我只得到一个空对象{}.以下是它的构建方式:
SpriteData data = new SpriteData();
data.sprite_name = "idle";
data.sprite_size = new Vector2(64.0f, 64.0f);
data.subimage = new List<Vector2> { new Vector2(0.0f, 0.0f) };
SpriteDataCollection col = new SpriteDataCollection();
col.sprites = new SpriteData[] { data };
Debug.Log(JsonUtility.ToJson(col));
Run Code Online (Sandbox Code Playgroud)
调试日志仅打印"{}".它为什么不序列化任何东西?我已经测试了它,并且序列化一个SpriteData完全按照它应该做的,但它在SpriteDataCollection中不起作用.
下面的代码用于从我们自己的系统中托管的Thingworx服务器获取温度值.这在团结中非常有效.但是并非在andoird中,一旦生成了apk,它将不会从服务器获取任何数据,并且将建立连接.但是,它只是不会获取数据并将其放入文本网格中.
我正在使用统一5.4.1 32位.检查Android - 5.0.2和6.
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
using System.Text.RegularExpressions;
using System;
using UnityEngine.UI;
public class GETTempValue : MonoBehaviour {
public GameObject TempText;
static string TempValue;
void Start()
{
StartCoroutine(GetText());
}
IEnumerator GetText()
{
Debug.Log("Inside Coroutine");
while (true)
{
yield return new WaitForSeconds(5f);
string url = "http://Administrator:ZZh7y6dn@*IP Address*:8080/Thingworx/Things/SimulationData/Properties/OvenTemperature/";
Debug.Log("Before UnityWebRequest");
UnityWebRequest www = UnityWebRequest.Get (url);
yield return www.Send();
Debug.Log("After UnityWebRequest");
if (www.isError) {
Debug.Log ("Error while Receiving: "+www.error);
} else {
Debug.Log("Success. Received: …Run Code Online (Sandbox Code Playgroud) 我一直在使用Unity中的保存和加载,我将序列化的类保存到文件中.我有一个Serializable类:
[Serializable]
class Save
{
public List<int> ID = new List<int>();
public List<int> Amounts = new List<int>();
}
Run Code Online (Sandbox Code Playgroud)
并将其保存到文件A-OK.我可以加载它没有错误,但如果我想稍后添加:
[Serializable]
class Save
{
public List<int> ID = new List<int>();
public List<int> Amounts = new List<int>();
public int extra = 0;
}
Run Code Online (Sandbox Code Playgroud)
我运行我的脚本它给了我一个反序列化错误,我完全理解,当我将反序列化文件转换为我的新类'保存'时,我添加的新变量不存在,它给了我错误.
我在商店中修复资产时发现了这个错误,我知道一个修复可以只是更改文件名,因此创建了一个新文件,但我不想只删除之前保存的内容.
所以我的问题是,如果我想在我的序列化类中添加更多变量,如果人们要更新资产,我将如何捕获并调整旧版本的变量?
谢谢!
作为我们实习的一部分,我们的任务是使用Unity WebGL创建业务应用程序.在阅读了JS和C#代码之间的交互之后,我们就在一个角落里.
我们试图从C#中取回一个列表来填充我们网页上的选择,我们只是不知道该怎么做.我们遵循了Unity文档,可以轻松地与我们的网页上的C#进行通信并获取数据.
但是,我们无法在浏览器中访问C#数据.SendMessage()方法不允许返回.
到目前为止,这是我们的代码
的index.html
<select id="wallsSelect"></select>
Run Code Online (Sandbox Code Playgroud)
jsfile
function getWallsList() {
//function is called at the creation of our object
var wallsSelect = document.getElementById("wallsSelect");
index = 0;
var wallsList = gameInstance.SendMessage('WallCreator', 'GetGameObjects'); //would like to get back our list of walls and populate our Select with it
for (item in wallsList) {
var newOption = document.createElement("option");
newOption.value = index;
newOption.innerHTML = item;
wallsSelect.appendChild(newOption);
index++;
}
Run Code Online (Sandbox Code Playgroud)
最后是C#代码
public List<string> GetGameObjects()
{
List<string> goNames = new List<string>();
foreach (var item in …Run Code Online (Sandbox Code Playgroud) 这是代码
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
public class csharpfile:MonoBehaviour{
public void LoadJson()
{
using (StreamReader r = new StreamReader("file.json"))
{
string json = r.ReadToEnd();
List<Item> items = JsonConvert.DeserializeObject<List<Item>>(json);
}
}
public class Item
{
public int millis;
public string stamp;
public DateTime datetime;
public string light;
public float temp;
public float vcc;
}
}
Run Code Online (Sandbox Code Playgroud)
现在我想解析文件内容(file.json)
[
{ "millis": "1000",
"stamp": "1273010254",
"datetime": "2010/5/4 21:57:34",
"light": "333",
"temp": "78.32",
"vcc": "3.54" },
]
Run Code Online (Sandbox Code Playgroud)
文件解析后我将如何在屏幕上打印内容以及如何写入文件 .do help …
将json字符串转换为ac#List时遇到一些问题!
这是我从服务器端获得的JSON.
[
{"roundid":1,"coins":700,"created":"2016-03-16 11:13:26","duration":198,"score":765230},
{"roundid":3,"coins":330,"created":"2016-03-16 11:13:56","duration":123,"score":425726},
{"roundid":4,"coins":657,"created":"2016-03-16 11:21:23","duration":432,"score":75384},
{"roundid":8,"coins":980,"created":"2016-03-16 11:23:19","duration":271,"score":827200}
]
Run Code Online (Sandbox Code Playgroud)
在我的C#程序中,我尝试使用此函数将我的json字符串转换为可用对象
public List<Round> getPlayerRounds(string username)
{
string url = BaseURL + "op=findUserRounds&username=" + username;
var json = new WebClient().DownloadString(url);
RoundDataList rounds = JsonUtility.FromJson<RoundDataList>(json);
List<Round> playerRounds = new List<Round>();
//for (var i = 0; i < rounds.roundList.Count; i++)
for (var i = 0; i < rounds.roundList.Length; i++)
{
RoundData rd = rounds.roundList[i];
Round r = rd.getRound();
playerRounds.Add(r);
}
return playerRounds;
}
Run Code Online (Sandbox Code Playgroud)
我在这里得到错误
ArgumentException:JSON必须表示对象类型.
我环顾四周,没有找到任何有用的东西,试过2-3解决方案甚至尝试编辑我的php webservice创建JSON字符串.
我的类看起来像这样
/* This is …Run Code Online (Sandbox Code Playgroud) 我想用椅子(在编辑器中)填充这个礼堂座位区,让它们都面对相同的焦点(舞台)。然后,我将在运行期间将椅子随机地放进不同的人。每次跑步后,椅子应保持不变,但应将人员清理干净,以便在下次跑步时,人群看起来有所不同。
座位区目前没有连接对撞机,椅子或人员也没有。
我发现这段代码已经完成了椅子的旋转操作,因此它们的目标是同一焦点。但是我仍然很好奇是否有更好的方法可以做到这一点。
//C# Example (LookAtPoint.cs)
using UnityEngine;
[ExecuteInEditMode]
public class LookAtPoint : MonoBehaviour
{
public Vector3 lookAtPoint = Vector3.zero;
void Update()
{
transform.LookAt(lookAtPoint);
}
}
Run Code Online (Sandbox Code Playgroud)
其他截图
我正在 Unity 中编写一个按类别提问和回答的游戏。这些类别是通过返回 JSON 文本的 PHP 脚本获取的,*当我在 UnityEditor 中使用此解决方案时,它可以正常工作,但是当我在移动设备上安装 .apk 时,反序列化不起作用*。
与 mysql 数据库的连接和 PHP 脚本工作正常,因为在我登录之前它工作正常
string json = [
{ "id_cat":"1",
"nombre_cat":"DAM",
"id_cat_padre":"0"
},
{ "id_cat":"4",
"nombre_cat":"ASIR",
"id_cat_padre":"0"
},
{ "id_cat":"5",
"nombre_cat":"DAW",
"id_cat_padre":"0"
}
]
Run Code Online (Sandbox Code Playgroud)
然后我将此字符串转换为类别列表
lsSubCategorias = JsonConvert.DeserializeObject<List<Categoria>>(json, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
Run Code Online (Sandbox Code Playgroud)
我已经在代码中添加了痕迹,它就在它停止的那一行上。
我使用 NuGet 安装了 Newtonsoft.Json,它出现在参考文献中。
我也只处理了一个类别对象而不是一个列表,但它也不起作用。这不是可视化问题,因为我已经创建了类别对象并用它们创建了按钮。
问题是它可以在 Unity 编辑器中运行,但不能在我的 Android 设备上运行
我的手机出现以下错误:
类型错误:System.PlatformNotSupportedException ToString 错误():Error.ToString()
我有一个解析这个json的问题:
{
"product_info":
{
"title": "Product Name"
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
using UnityEngine;
using System.Collections;
using System.IO;
using System.Net;
using UnityEngine.UI;
public class ReadJson : MonoBehaviour
{
public Text myText;
[System.Serializable]
public class ProductInfo
{
public string title { get; set; }
}
[System.Serializable]
public class RootObject
{
public ProductInfo product_info { get; set; }
}
void Start () {
TextAsset asset = Resources.Load (Path.Combine ("Json", "toulouse")) as TextAsset;
RootObject m = JsonUtility.FromJson<RootObject> (asset.text);
Debug.Log (m.product_info.title);
}
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误消息:"对象引用未设置为对象的实例".我已经尝试过,成功地解析了一个没有嵌套的json但是我不明白为什么但是在创建了适当的类之后仍然无法工作.
我正在尝试使用UnityEngine.JsonUtility. 它工作正常,但我的枚举类型没有得到正确转换。我尝试使用该EnumMember属性,但仍然没有运气。
我该如何解决?
笔记
我正在使用此解决方案读取多个文件并将它们存储在数组中。
[Serializable]
public class EquipementItem
{
public enum ItemTypes
{
None,
Armor,
Weapon
}
public enum SlotTypes
{
Head,
Shoulders,
Chest,
Bracers,
Gloves,
Waist,
Legs,
Boots,
Weapon
}
public int ID;
public string Name;
public ItemTypes ItemType;
public SlotTypes SlotType;
}
Run Code Online (Sandbox Code Playgroud)
和 json 文件
{
"Items": [
{
"ID": "1",
"Name": "Basic Sword",
"ItemType": "Weapon",
"SlotType": "Weapon"
},
{
"ID": "2",
"Name": "Advanced Sword",
"ItemType": "Weapon",
"SlotType": "Weapon"
},
{ …Run Code Online (Sandbox Code Playgroud) c# ×9
json ×6
android ×1
distribute ×1
enums ×1
evenly ×1
html ×1
javascript ×1
json.net ×1
jsonconvert ×1
jsonobject ×1
networking ×1
placement ×1
unity-webgl ×1