Unity基础 用C#脚本读取JSON文件数据

  读取JSON文件数据网上有很多方法吗,这里采用SimpleJSON,关于SimpleJSON的介绍参考以下链接:http://wiki.unity3d.com/index.php/SimpleJSON,使用之前要先导入SimpleJSON的相关文件。

JSON文件名:achieve.json,文件内容:

{

"30002":{"achieve_id":30002,"achieve_name":"连胜","achieve_race":3,"achieve_type":41,"achieve_des":"排位赛连续胜利10场","achieve_gold":20,"achieve_times":10,"image":"athletics_2"},
"30003":{"achieve_id":30003,"achieve_name":"百战百胜","achieve_race":3,"achieve_type":41,"achieve_des":"排位赛连续胜利100场","achieve_gold":100,"achieve_times":100,"image":"athletics_3"},
"30004":{"achieve_id":30004,"achieve_name":"不屈","achieve_race":3,"achieve_type":42,"achieve_des":"排位赛连续失败10场","achieve_gold":5,"achieve_times":10,"image":"athletics_4"}

     }
View Code

这里直接贴出脚本代码:

ConfigAchieve类代码:

using UnityEngine;
using System.Collections;

using SimpleJSON;

public class ConfigAchieve
{
    private Hashtable hash = new Hashtable();  

    // 从Json文件中载入信息保存至Hashtable
    public void loadFromJson(JSONNode data)
    {
        if (data == null)
            Debug.Log("read faild");
        else
        {
            for(int i=0 ; i<data.Count; i++)
            {
                ConfigAchieveItem node = new ConfigAchieveItem();

                node.Id = data [i] ["achieve_id"];
                node.Name = data [i] ["achieve_name"];
                node.Race = data [i] ["achieve_race"];
                node.Type = data [i] ["achieve_type"];
                node.Des = data [i] ["achieve_des"];
                node.Gold = data [i] ["achieve_gold"];
                node.Times = data [i] ["achieve_times"];
                node.Image = data [i] ["image"];

                hash.Add(node.Id, node); 
            }
        }
    }

    // 从Hashtable中返回键为key的值
    public ConfigAchieveItem GetItem(string key)
    { 
        ConfigAchieveItem itemAchieve = (ConfigAchieveItem)hash[key];
        return itemAchieve;
    }
}
ConfigAchieve

ConfigAchieveItem类代码:

using System;
using SimpleJSON;

public class ConfigAchieveItem
{
    private string _configId = "";
    private string _configName = "";
    private string _configRace = "";
    private string _configType = "";
    private string _configDes = "";
    private string _configGold = "";
    private string _configTimes = "";
    private string _configImage = "";

    public string Id
    {
        get { return _configId; }
        set { _configId = value; }
    }
    
    public string Name
    {
        get { return _configName; }
        set { _configName = value; }
    }
    
    public string Race
    {
        get { return _configRace; }
        set { _configRace = value; }
    }
    
    public string Type
    {
        get { return _configType; }
        set { _configType = value; }
    }
    
    public string Des
    {
        get { return _configDes; }
        set { _configDes = value; }
    }
    
    public string Gold
    {
        get { return _configGold; }
        set { _configGold = value; }
    }
    
    public string Times
    {
        get { return _configTimes; }
        set { _configTimes = value; }
    }
    
    public string Image
    {
        get { return _configImage; }
        set { _configImage = value; }
    }

    public void loadDataFromJson(ConfigAchieve achieve, string key)
    {
        ConfigAchieveItem temp = (ConfigAchieveItem)achieve.GetItem(key);

        _configId = temp.Id;
        _configName = temp.Name;
        _configRace = temp.Race;
        _configType = temp.Type;
        _configDes = temp._configDes;
        _configGold = temp.Gold;
        _configTimes = temp.Times;
        _configImage = temp.Image;
    }
}
ConfigAchieveItem

ConfigManager类代码:

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using SimpleJSON;

public class ConfigManager : MonoBehaviour
{
    public ConfigAchieve _config_achieve = new ConfigAchieve();

    // Use this for initialization
    void Start()
    {
        Test(); // 测试函数
    }
    
    // Update is called once per frame
    void Update()
    {   
    }

    // 打开对应路径的json文件
    public JSONNode jsonFromFile(string filename)
    {
        JSONNode json_object = null;
        TextAsset filePath = (TextAsset)Resources.Load(filename);
        
        if (filePath == null)   // Json文件读取失败,打印日志
            Debug.Log("find json file faild!");
        else
            json_object = JSON.Parse(filePath.text);    // 转为json格式

        return json_object;
    }

    public void Test()
    {
        _config_achieve.loadFromJson(jsonFromFile("Config/achieve"));
        ConfigAchieveItem temp = _config_achieve.GetItem("10001");

        Debug.Log("id = " + temp.Id + " name = " + temp.Name);
        Debug.Log("race = " + temp.Race + " type = " + temp.Type);
        Debug.Log("des = " + temp.Des + " gold = " + temp.Gold);
        Debug.Log("times = " + temp.Times + " image = " + temp.Image);
    }
}
ConfigManager  

  其中ConfigManager用来控制打开json文件,ConfigAchieve用来读取数据,ConfigAchieveItem用来保存每条数据然后就可以用代码中的Test函数来测试读取是否正确。(很久了,大概是这个样子,具体的以代码为准)~~

Unity基础 用C#脚本读取JSON文件数据,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。