zoukankan      html  css  js  c++  java
  • unity excel导出json 并解析 存在项目 dict内

    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、

    5.excel的填写,数组或者class类型,不能不填(可以只填[]、{}确保类型正确),否则json读取异常。

      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:unityJsonTestNew Unity ProjectAssetsResourcesjsonFile.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 }

    json文件  从excel里拿的例子:

    {
      "NPC": [
        {
          "ID": "BS001",
          "Name": "鬼道士",
          "AssetName": "BS001",
          "HP": 100,
          "Attack": 15,
          "Defence": 0,
          "DateTest": "1998/01/05",
          "#TestExclude": "hello",
          "TestJsonArray": [
            "player",
            "enemy"
          ],
          "TestJsonObject": {
            "player": "enemy"
          }
        },
        {
          "ID": "BS002",
          "Name": "钟馗",
          "AssetName": "BS002",
          "HP": 352,
          "Attack": 22,
          "Defence": 3,
          "DateTest": "1997/02/01",
          "#TestExclude": "hello",
          "TestJsonArray": [
            "player",
            "enemy"
          ],
          "TestJsonObject": {
            "player": "enemy"
          }
        },
        {
          "ID": "BS003",
          "Name": "",
          "AssetName": "BS003",
          "HP": 332,
          "Attack": 3,
          "Defence": 44,
          "DateTest": "1996/03/07",
          "#TestExclude": "hello",
          "TestJsonArray": [
            "bad array",
            "error"
          ],
          "TestJsonObject": {
            "player": "enemy"
          }
        }
      ],
      "Item": [
        {
          "ID": "WP001",
          "Name": "倚天剑",
          "AssetName": "ICON01"
        },
        {
          "ID": "WP002",
          "Name": "屠龙刀",
          "AssetName": "ICON02"
        }
      ]
    }
    改变自己
  • 相关阅读:
    HDU 4348 To the moon(可持久化线段树)
    HDU 5875 Function 大连网络赛 线段树
    HDU 5877 2016大连网络赛 Weak Pair(树状数组,线段树,动态开点,启发式合并,可持久化线段树)
    HDU 5876 大连网络赛 Sparse Graph
    HDU 5701 中位数计数 百度之星初赛
    CodeForces 708B Recover the String
    Java实现 蓝桥杯 算法提高 套正方形(暴力)
    ASP.NET生成验证码
    ASP.NET生成验证码
    ASP.NET生成验证码
  • 原文地址:https://www.cnblogs.com/sun-shadow/p/13586911.html
Copyright © 2011-2022 走看看