using System.Collections; using System.Collections.Generic; using System.IO; using AYellowpaper.SerializedCollections; using YooAsset; #if UNITY_EDITOR using UnityEditor; #endif using UnityEngine; namespace BITKit { public sealed class DictionaryReferenceScriptableObject : ScriptableObject { [SerializeField] private string mark; [SerializeField] internal SerializedDictionary dictionary; public static DictionaryReferenceScriptableObject Singleton { get { if (_singleton == null) { #if UNITY_EDITOR _singleton = AssetDatabase.LoadAssetAtPath( "Assets/Artists/Configs/reference_dictionary.asset"); #else var task = YooAssets.LoadAssetAsync("reference_dictionary"); task.WaitForAsyncComplete(); _singleton=task.AssetObject as DictionaryReferenceScriptableObject; #endif } return _singleton; } } private static DictionaryReferenceScriptableObject _singleton; public static IDictionary Dictionary => Singleton.dictionary; public static void Save() { #if UNITY_EDITOR EditorUtility.SetDirty(Singleton); #endif } public static void Add(string value) { #if UNITY_EDITOR if (string.IsNullOrEmpty(value)) { return; } var random = Random.Range(int.MinValue, int.MaxValue); while (Dictionary.ContainsKey(random)) { random = Random.Range(int.MinValue, int.MaxValue); } Singleton.dictionary.AddConflictAllowed(random,value); Save(); #endif } public static void AddOrUpdate(int key, string value) { if (string.IsNullOrEmpty(value)) { return; } if (Singleton.dictionary.TryAdd(key,value) is false) { Singleton.dictionary[key] = value; } Save(); } } }