我想創建用于保存航點位置及其鏈接的 json 資料。
當前的專案
using Newtonsoft.Json;
using System.Collections.Generic;
[System.Serializable]
public class Point
{
public double x { get; set; }
public double y { get; set; }
public double z { get; set; }
}
[System.Serializable]
public class Link
{
[JsonProperty("$ref")]
public string Ref { get; set; }
public Point Point { get; set; } = new Point();
public List<RoadLink> RoadLinks { get; set; } = new List<RoadLink>();
public int? LineCount { get; set; }
public double? SpeedLimit { get; set; }
[JsonProperty("$id")]
public string Id { get; set; }
}
[System.Serializable]
public class RoadLink
{
public Link Link { get; set; } = new Link();
public double SpacerLineWidth { get; set; }
}
[System.Serializable]
public class Content
{
public Point Point { get; set; } = new Point();
public List<RoadLink> RoadLinks { get; set; } = new List<RoadLink>();
public int LineCount { get; set; }
public double SpeedLimit { get; set; }
[JsonProperty("$id")]
public string Id { get; set; }
[JsonProperty("$ref")]
public string Ref { get; set; }
}
[System.Serializable]
public class DataFile
{
[JsonProperty("$content")]
public List<Content> Content { get; set; } = new List<Content>();
[JsonProperty("$type")]
public string Type { get; set; }
}
第二個腳本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
using UnityEditor;
using System.IO;
public class JsonKeyWords : MonoBehaviour
{
public Transform[] wayPosition;
List<Vector3> wayPos = new List<Vector3>();
public string typeValue;
//public s
void AssignPositions()
{
for (int i = 0; i < wayPosition.Length; i )
{
wayPos.Add(new Vector3(wayPosition[i].position.x, wayPosition[i].position.y, wayPosition[i].position.z));
//wayPos[i].x = wayPosition[i].position.x;
//wayPos[i].y = wayPosition[i].position.y;
//wayPos[i].z = wayPosition[i].position.z;
}
}
void main()
{
DataFile mainSource = new DataFile();
mainSource.Content.Add(new Content()
{
Point = new Point { x = wayPos[0].x, y = wayPos[0].y, z = wayPos[0].z },
RoadLinks = new List<RoadLink>(),
LineCount = 1,
Id = "1",
Ref = "0",
SpeedLimit = 3.13
});
mainSource.Type = typeValue;
var jsonString = JsonConvert.SerializeObject(mainSource);
var newPklayer = JsonConvert.DeserializeObject(jsonString);
File.WriteAllText(Application.dataPath "/test.json", jsonString);
Debug.Log(newPklayer);
}
private void Awake()
{
AssignPositions();
main();
}
}
我想要的資料 :我正在學習的一個專案的 Json 檔案沒有關于他如何制作這個的參考,但有腳本來消毒和使用這些資料
{
"$content": [
{
"Point": {
"x": 644.6,
"y": 11.2,
"z": -827.37
},
"RoadLinks": [
{
"Link": {
"Point": {
"x": 595.9198,
"y": 11.06416,
"z": -827.0071
},
"RoadLinks": [
{
"Link": {
"$ref": "0"
},
"SpacerLineWidth": 0
},
{
"Link": {
"Point": {
"x": 547.2396,
"y": 10.92832,
"z": -826.6442
},
"RoadLinks": [
{
"Link": {
"$ref": "2"
},
"SpacerLineWidth": 0
},
{
"Link": {
"Point": {
"x": 498.5594,
"y": 10.79249,
"z": -826.2813
},
"RoadLinks": [
{
"Link": {
"$ref": "5"
},
"SpacerLineWidth": 0
},
{
"Link": {
"Point": {
"x": 473.6187,
"y": 10.79246,
"z": -840.8074
},
"RoadLinks": [
{
"Link": {
"Point": {
"x": 446.4278,
"y": 6.237495,
"z": -854.9285
},
"RoadLinks": [
{
"Link": {
"$ref": "11"
},
"SpacerLineWidth": 0
},
{
"Link": {
"Point": {
"x": 419.2369,
"y": 1.682533,
"z": -869.0497
},
"RoadLinks": [
{
"Link": {
"Point": {
"x": 410.5,
"y": 1.682472,
"z": -893
},
"RoadLinks": [
{
"Link": {
"$ref": "8"
},
"SpacerLineWidth": 0
},
{
"Link": {
"$ref": "16"
},
"SpacerLineWidth": 0
}
],
"LineCount": 1,
"SpeedLimit": 3.40282346638529e 38,
"$id": "18"
},
"SpacerLineWidth": 0
},
{
"Link": {
"Point": {
"x": 409.2,
"y": 1.682472,
"z": -907.2071
},
"RoadLinks": [
{
"Link": {
"Point": {
"x": 383.9,
"y": 1.682533,
"z": -907.5
},
"RoadLinks": [
{
"Link": {
"Point": {
"x": 382.6,
"y": 1.682533,
"z": -923
},
"RoadLinks": [
{
"Link": {
"$ref": "11"
},
"SpacerLineWidth": 0
},
{
"Link": {
"$ref": "24"
},
"SpacerLineWidth": 0
},
{
"Link": {
"Point": {
"x": 379.7,
"y": 1.682503,
"z": -935.3
},
"RoadLinks": [
{
"Link": {
"$ref": "26"
},
"SpacerLineWidth": 0
},
{
"Link": {
"Point": {
"x": 376.9,
"y": 1.682503,
"z": -942.5
},
"RoadLinks": [
{
"Link": {
"$ref": "30"
},
"SpacerLineWidth": 0
},
{
"Link": {
"Point": {
"x": 371.1,
"y": 1.682503,
"z": -944.7
},
"RoadLinks": [
{
"Link": {
"$ref": "33"
},
"SpacerLineWidth": 0
},
{
"Link": {
"Point": {
"x": 332.1663,
"y": 1.682513,
"z": -944.7
},
"RoadLinks": [
{
"Link": {
"$ref": "36"
},
"SpacerLineWidth": 0
},
{
"Link": {
"Point": {
"x": 293.2326,
"y": 1.682523,
"z": -944.7
},
"RoadLinks": [
{
"Link": {
"$ref": "39"
},
"SpacerLineWidth": 0
},
{
"Link": {
"Point": {
"x": 254.299,
"y": 1.682533,
"z": -944.7
},
{
"$ref": "222"
},
{
"$ref": "224"
},
{
"$ref": "245"
},
{
"$ref": "251"
},
{
"$ref": "258"
},
{
"$ref": "261"
},
{
"$ref": "263"
},
{
"$ref": "265"
},
{
"$ref": "269"
},
{
"$ref": "272"
},
{
"$ref": "278"
},
{
"$ref": "280"
},
{
"$ref": "282"
},
{
"$ref": "284"
},
{
"$ref": "289"
},
{
"$ref": "291"
},
{
"$ref": "314"
},
{
"$ref": "316"
},
{
"$ref": "318"
},
{
"$ref": "320"
}
],
"$type": "Game.Traffic.RoadPoint[]"
}
現在到主要問題,
- 我無法理解他是如何使用的,
$ref因為它們不合時宜 - 我無法理解如何創建
ref參考,就像在 json 檔案中創建的一樣。 - 我正在創建一個 RoadLink 串列,其中它指的是 Link 類,然后 Link 又指的是 RoadLink,我該如何使用它?是鏈接串列嗎?任何指導將不勝感激。感謝你。
uj5u.com熱心網友回復:
據我了解,這基本上是一個圖表。既是Link節點又RoadLink是邊。對于序列化,我假設這些示例依賴于序列化庫的自動參考功能來避免回圈。
我可能會避免這種情況并使用手動參考來代替,即至少讓 Link 或 RoadLink 類中的一個類使用索引而不是實際參考來參考另一個類。這應該有助于使序列化更緊湊,并希望更容易理解。
作為一個例子,你可以描述一個圖表
public class Node
{
public List<Edge> Edges {get;}
}
public class Edge
{
public int TargetNodeIndex {get;}
}
public class Graph{
public List<Node> Nodes {get;}
public IEnumerable<Node> GetNeighbors(Node node){
foreach(var e in node.Edges){
yield return Nodes[e.TargetNodeIndex];
}
}
}
這種表示完全可用,但如果愿意,也可以將其轉換為純物件表示,這取決于您的特定用例。請注意,此表示不允許簡單地洗掉節點,如果您需要重新映射所有索引。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/346592.html
