BepInEx插件配置文件本地化示例代码-C#语言论坛-编程语言区-资源工坊-游戏模组资源分享

BepInEx插件配置文件本地化示例代码

using BepInEx;
using BepInEx.Configuration;
using UnityEngine;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using System.Diagnostics;
using Debug = UnityEngine.Debug;
using System.Reflection;

namespace YourNamespace
{
    [BepInPlugin("com.example.myplugin", "My Plugin", "1.0.0")]
    public class MyPlugin : BaseUnityPlugin
    {
        private ConfigEntry<int> myConfig;
        private Dictionary<string, string> localizationData;

        private void Awake()
        {

            // 加载和解析本地化文件
            string localizationFilePath = Path.Combine(Paths.PluginPath, "Localization", "en.json");
            string localizationDataJson = File.ReadAllText(localizationFilePath);
            localizationData = JsonConvert.DeserializeObject<Dictionary<string, string>>(localizationDataJson);

            // 创建配置项,使用本地化的描述文本
            myConfig = Config.Bind<int>("General", "MyConfig", 0, GetLocalizedDescription("MyConfigDescription"));
        }

        private string GetLocalizedDescription(string key)
        {
            if (localizationData.ContainsKey(key))
            {
                return localizationData[key];
            }
            else
            {
                Debug.LogError($"Localization key not found: {key}");
                return string.Empty;
            }
        }
    }
}

本地化文件json写法

{
  "MyConfigDescription": "测试"
}

 

 

请登录后发表评论

    没有回复内容