1. excel导出json直接用第三方: https://github.com/neil3d/excel2json/releases
2.读取json使用 JsonUtility.FromJson参考:http://blog.sina.com.cn/s/blog_140bb6bd40102xa4i.html
3.确保每个变量和json的变量名相同
4.添加类可序列化标识:Serializable
1 1 using System; 2 2 using System.Collections; 3 3 using System.Collections.Generic; 4 4 using System.IO; 5 5 using System.Text; 6 6 using UnityEngine; 7 7 8 8 9 9 10 10 /// <summary> 11 11 /// 1.确保每个变量和json的变量名相同 12 12 /// 2.添加类可序列化标识:Serializable 13 13 /// </summary> 14 14 public class ExampleDataTestManager 15 15 { 16 16 #region 类型信息 17 17 [Serializable] 18 18 public class TestJsonObjectTXT 19 19 { 20 20 public string player; 21 21 } 22 22 23 23 [Serializable] 24 24 public class NPCTest 25 25 { 26 26 public string ID; // 编号 27 27 public string Name; // 名称 28 28 public string AssetName; // 资源编号 29 29 public int HP; // 血 30 30 public int Attack; // 攻击 31 31 public int Defence; // 防御 32 32 public string DateTest; // 测试日期 33 33 public List<string> TestJsonArray; // 测试单元格内的Json数组 34 34 public TestJsonObjectTXT TestJsonObject; // 测试单元格内的Json对象 35 35 } 36 36 [Serializable] 37 37 public class ItemTest 38 38 { 39 39 public string ID; // 编号 40 40 public string Name; // 名称 41 41 public string AssetName; // 资源编号 42 42 } 43 43 [Serializable] 44 44 public class TestArray 45 45 { 46 46 public List<NPCTest> NPC; 47 47 public List<ItemTest> Item; 48 48 } 49 49 #endregion 50 50 51 51 public static Dictionary<string, NPCTest> mNPCTextDict = new Dictionary<string, NPCTest>(); 52 52 public static Dictionary<string, ItemTest> mItemDict = new Dictionary<string, ItemTest>(); 53 53 //private bool isFirstUse = true; 54 54 55 55 /// <summary> 56 56 /// 如果考虑异步加载,需要再加Callback回调 57 57 /// </summary> 58 58 public static void Init() 59 59 { 60 60 Clear(); 61 61 string jsonTest = ((TextAsset)Resources.Load("ExampleData")).text; 62 62 TestArray jsonObject = JsonUtility.FromJson<TestArray>(jsonTest); 63 63 64 64 if(jsonObject == null) 65 65 { 66 66 Debug.LogError("ExampleData data null"); 67 67 } 68 68 69 69 if (jsonObject.NPC == null) 70 70 { 71 71 Debug.LogError("NPC data null"); 72 72 } 73 73 if (jsonObject.Item == null) 74 74 { 75 75 Debug.LogError("Item data null"); 76 76 } 77 77 78 78 foreach (NPCTest item in jsonObject.NPC) 79 79 { 80 80 mNPCTextDict[item.ID] = item; 81 81 } 82 82 foreach (ItemTest item in jsonObject.Item) 83 83 { 84 84 mItemDict[item.ID] = item; 85 85 } 86 86 } 87 87 /// <summary> 88 88 /// 如果不是全局的配置,用完记得clear 89 89 /// 数据不大,一直缓存也可以。 90 90 /// </summary> 91 91 public static void Clear() 92 92 { 93 93 mNPCTextDict.Clear(); 94 94 mItemDict.Clear(); 95 95 } 96 96 97 97 } 98 98 99 99 100 100 101 101 public class JsonTest : MonoBehaviour 102 102 { 103 103 // Start is called before the first frame update 104 104 void Start() 105 105 { 106 106 107 107 //string jsonTest = File.ReadAllText(@"E:\unity\JsonTest\New Unity Project\Assets\Resources\jsonFile.json", Encoding.UTF8); 108 108 // string jsonTest = ((TextAsset)Resources.Load("jsonFile")).text; 109 109 // JsonObjectModel jsonObject = JsonUtility.FromJson<JsonObjectModel>(jsonTest); 110 110 111 111 ExampleDataTestManager.Init(); 112 112 Debug.Log(""); 113 113 114 114 } 115 115 116 116 // Update is called once per frame 117 117 void Update() 118 118 { 119 119 120 120 } 121 121 }