unity读取配置文件confitData

using LitJson;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Events;

public class LoadConfigData : MonoBehaviour
{
    public Dictionary<string, string> configDic = new Dictionary<string, string>();
    public static LoadConfigData loadConfig;
    private UnityAction _loadDone;

    private void Awake()
    {
        loadConfig = this;
        ReadConfigData();
    }
    public void ReadConfigData()
    {
        LoadFileAndIntialSpeed(Application.streamingAssetsPath, "configData.txt");
    }
    void LoadFileAndIntialSpeed(string sPath, string sName)
    {
        StreamReader sr = null;
        sr = File.OpenText(sPath + "//" + sName);

        string t_Line = "";

        if ((t_Line += sr.ReadLine()) != null)
        {
            // Debug.Log(t_Line);
            ConfigLoadDone(t_Line);
        }
        else
        {
            Debug.Log("配置文件错误");
        }

        sr.Close();
        sr.Dispose();
    }

    public void WirteConfigData(string info)
    {
        WirteConfigData(Application.streamingAssetsPath, "configData.txt", info);
    }

    void WirteConfigData(string sPath, string sName, string info)
    {
        FileStream file = File.OpenWrite(sPath + "//" + sName);
        //得到字符串的UTF8 数据流
        byte[] bts = System.Text.Encoding.UTF8.GetBytes(info);
        // 文件写入数据流
        file.Write(bts, 0, bts.Length);
        if (file != null)
        {
            //清空缓存
            file.Flush();
            // 关闭流
            file.Close();
            //销毁资源
            file.Dispose();
        }
    }

    private void ConfigLoadDone(string jsonInfo)
    {
        JsonData json = JsonMapper.ToObject(jsonInfo);

        foreach (var item in json.Keys)
        {
            configDic.Add(item, json[item].ToString());
        }
        if (_loadDone != null)
        {
            _loadDone.Invoke();
        }
    }

    public string GetConfigInfo(string infoKeys)
    {
        if (configDic.ContainsKey(infoKeys))
        {
            return configDic[infoKeys];
        }
        else
        {
            //Debug.Log("当前的Config中没有该值");
            return "";
        }
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Unity中,读取配置文件可以通过使用Unity提供的API来实现。一种常见的配置文件格式是JSON,下面是一个简单的示例: ```json { "playerName": "John", "playerLevel": 10, "playerHealth": 100 } ``` 要读取这个配置文件,可以按照以下步骤进行操作: 1. 创建一个C#脚本,用于读取配置文件的数据。可以将该脚本附加到一个游戏对象上,或者作为一个单独的脚本使用。 2. 在脚本中使用`System.IO`命名空间下的`File.ReadAllText()`方法来读取配置文件的内容。该方法接受一个文件路径作为参数,并返回文件内容的字符串。 3. 使用Unity提供的JsonUtility类来解析JSON字符串,并将其转换为对应的数据结构。例如,可以定义一个与配置文件结构相匹配的C#类,然后使用`JsonUtility.FromJson()`方法将JSON字符串转换为该类的实例。 4. 可以通过访问该类的属性或字段来获取配置文件中的数据。 下面是一个示例代码: ```csharp using UnityEngine; using System.IO; public class ConfigReader : MonoBehaviour { public string configFilePath; // 配置文件路径 private void Start() { string json = File.ReadAllText(configFilePath); ConfigData configData = JsonUtility.FromJson<ConfigData>(json); Debug.Log("Player Name: " + configData.playerName); Debug.Log("Player Level: " + configData.playerLevel); Debug.Log("Player Health: " + configData.playerHealth); } } [System.Serializable] public class ConfigData { public string playerName; public int playerLevel; public int playerHealth; } ``` 请注意,上述示例中的`configFilePath`变量需要指定配置文件的路径。你可以将配置文件放在Unity项目中的任何位置,并将其路径分配给`configFilePath`变量。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陌上桑AGO

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值