更改文件架构
This commit is contained in:
21
Packages/Common~/Scripts/Config/AutoExec.cs
Normal file
21
Packages/Common~/Scripts/Config/AutoExec.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Sirenix.OdinInspector;
|
||||
using System.IO;
|
||||
using Cysharp.Threading.Tasks;
|
||||
namespace BITKit
|
||||
{
|
||||
public class AutoExec : Exec
|
||||
{
|
||||
public string path;
|
||||
public override FileInfo[] GetFiles()
|
||||
{
|
||||
var path = Utility.Path.Get(this.path);
|
||||
DirectoryInfo directoryInfo = new(path);
|
||||
BIT4Log.Log<AutoExec>($"正在读取配置文件:{path}");
|
||||
directoryInfo.Create();
|
||||
return directoryInfo.GetFiles();
|
||||
}
|
||||
}
|
||||
}
|
11
Packages/Common~/Scripts/Config/AutoExec.cs.meta
Normal file
11
Packages/Common~/Scripts/Config/AutoExec.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d8c1c55a1b059114e87a26ff5136b37c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,16 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using UnityEngine;
|
||||
namespace BITKit
|
||||
{
|
||||
public class AutoExecFromPersistentData : Exec
|
||||
{
|
||||
public override FileInfo[] GetFiles()
|
||||
{
|
||||
var path = Utility.Path.persistentDataPath;
|
||||
BIT4Log.Log<AutoExec>($"正在从{path}搜索配置文件");
|
||||
return new DirectoryInfo(path).GetFiles();
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d766da2268c198b488eae51e9745c9d7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
49
Packages/Common~/Scripts/Config/ConfigSO.cs
Normal file
49
Packages/Common~/Scripts/Config/ConfigSO.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
using System.IO;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
namespace BITKit
|
||||
{
|
||||
public static partial class Utility
|
||||
{
|
||||
public static partial class Config
|
||||
{
|
||||
public static bool TryGetConfig<T>(string configName, out T value)
|
||||
{
|
||||
var path = Utility.Path.CombinePath(Utility.Path.persistentDataPath, $"{configName}.json");
|
||||
if (File.Exists(path))
|
||||
{
|
||||
var json = File.ReadAllText(path);
|
||||
value = JsonConvert.DeserializeObject<T>(json);
|
||||
return true;
|
||||
}
|
||||
value = default;
|
||||
return false;
|
||||
}
|
||||
public static T GetConfig<T>(string configName) where T : new()
|
||||
{
|
||||
if (TryGetConfig<T>(configName, out var config))
|
||||
{
|
||||
}
|
||||
else
|
||||
{
|
||||
SaveConfig<T>(configName, new());
|
||||
TryGetConfig<T>(configName, out config);
|
||||
}
|
||||
return config;
|
||||
}
|
||||
public static void SaveConfig<T>(string configName, T value)
|
||||
{
|
||||
var json = JsonConvert.SerializeObject(value, Formatting.Indented);
|
||||
var path = Utility.Path.CombinePath(Utility.Path.persistentDataPath, $"{configName}.json");
|
||||
File.WriteAllText(path, json);
|
||||
}
|
||||
}
|
||||
}
|
||||
public class ConfigSO : BITKitSO
|
||||
{
|
||||
public string json;
|
||||
}
|
||||
}
|
11
Packages/Common~/Scripts/Config/ConfigSO.cs.meta
Normal file
11
Packages/Common~/Scripts/Config/ConfigSO.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 671b5c82445817f4cadcd9558e4f3fcf
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
37
Packages/Common~/Scripts/Config/Exec.cs
Normal file
37
Packages/Common~/Scripts/Config/Exec.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Sirenix.OdinInspector;
|
||||
using System.IO;
|
||||
using Cysharp.Threading.Tasks;
|
||||
namespace BITKit
|
||||
{
|
||||
public abstract class Exec : SerializedMonoBehaviour, IAction
|
||||
{
|
||||
public void Excute()
|
||||
{
|
||||
try
|
||||
{
|
||||
foreach (var file in GetFiles())
|
||||
{
|
||||
if (file.Extension is ".json")
|
||||
{
|
||||
BIT4Log.Log<AutoExec>($"正在加载配置文件:{file.Name}");
|
||||
var json = File.ReadAllText(file.FullName);
|
||||
DataParser.Set(json);
|
||||
Data.Set(file.Name, json);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
BIT4Log.LogException(e);
|
||||
}
|
||||
}
|
||||
public abstract FileInfo[] GetFiles();
|
||||
void Start()
|
||||
{
|
||||
Excute();
|
||||
}
|
||||
}
|
||||
}
|
11
Packages/Common~/Scripts/Config/Exec.cs.meta
Normal file
11
Packages/Common~/Scripts/Config/Exec.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ad0609e4a07bdcc49a3358bd26c38dd2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
15
Packages/Common~/Scripts/Config/PlayerConfig.cs
Normal file
15
Packages/Common~/Scripts/Config/PlayerConfig.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
namespace BITKit
|
||||
{
|
||||
public record PlayerConfig
|
||||
{
|
||||
[SaveData]
|
||||
public static PlayerConfig singleton = new();
|
||||
public float sensitivity = 1.81f;
|
||||
public float touchSensitivity = 0.22f;
|
||||
public float m_yaw = 0.022f;
|
||||
public float fov = 75;
|
||||
}
|
||||
}
|
11
Packages/Common~/Scripts/Config/PlayerConfig.cs.meta
Normal file
11
Packages/Common~/Scripts/Config/PlayerConfig.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6b05561bb11790146a42153ccc10ec81
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
40
Packages/Common~/Scripts/Config/TranslateSO.cs
Normal file
40
Packages/Common~/Scripts/Config/TranslateSO.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using RotaryHeart.Lib.SerializableDictionary;
|
||||
namespace BITKit
|
||||
{
|
||||
public sealed class TranslateSO : ScriptableObject
|
||||
{
|
||||
public SerializableDictionaryBase<string, string> dictionary = new();
|
||||
public string Get(string text, bool isLocalization = true)
|
||||
{
|
||||
foreach (var word in dictionary)
|
||||
{
|
||||
var value = word.Value;
|
||||
if (isLocalization)
|
||||
{
|
||||
value = DI.Get<ITranslator>().Translate(value);
|
||||
}
|
||||
text = text.Replace(word.Key, value);
|
||||
}
|
||||
return text;
|
||||
}
|
||||
public string GetAt(string key, bool isLocalization = true)
|
||||
{
|
||||
var translater = DI.Get<ITranslator>();
|
||||
if (dictionary.TryGetValue(key, out var value))
|
||||
{
|
||||
if (isLocalization)
|
||||
value = translater.Translate(value);
|
||||
return value;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* if (isLocalization)
|
||||
value = translater.Translate(value); */
|
||||
return key;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
11
Packages/Common~/Scripts/Config/TranslateSO.cs.meta
Normal file
11
Packages/Common~/Scripts/Config/TranslateSO.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1dbdf9cdb3f6eb14f9327143904fbc28
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user