Unity 读取.Json内容

 
using System;
using System.IO;
using UnityEngine;

public class ReadJson:MonoBehaviour {
    public static TestSetting test = new TestSetting();
    //文件路径
    public static string testJsonPath;
    public static int Port;
    public static int CutTime;
    public static int exeX;
    public static int exeY;
    public static int exeWhith;
    public static int exeHeight;

    [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
    public static void Awake() {
        testJsonPath =System.Environment.CurrentDirectory + "/资源/配置文件/Config.json";
        ReadMyJson();
    }
    //读取方法
    public static void ReadMyJson() {

        if (File.Exists(testJsonPath)) {
            string jsonString = File.ReadAllText(testJsonPath);
            TestSetting mytest = JsonUtility.FromJson<TestSetting>(jsonString);
            test = mytest;
            Port = mytest.Port;
            CutTime = mytest.CutTime;
            exeX = mytest.exeX;
            exeY = mytest.exeY;
            exeWhith = mytest.exeWhith;
            exeHeight = mytest.exeHeight;
            print("端口号:" + Port);
            print("倒计时:" + CutTime);
        }
    }
}


//数据类型,用来存储从json中读到的键值对
public struct TestSetting {
    public int Port;
    public int CutTime;
    public int exeX;
    public int exeY;
    public int exeWhith;
    public int exeHeight;
}

 

posted on 2021-04-09 16:36  zqiang0803  阅读(88)  评论(0编辑  收藏  举报

导航