BITKit/Src/Unity/Scripts/Reference/DictionaryReferenceScriptab...

80 lines
1.7 KiB
C#

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<int,string> dictionary;
public static DictionaryReferenceScriptableObject Singleton
{
get
{
if (_singleton == null)
{
#if UNITY_EDITOR
_singleton =
AssetDatabase.LoadAssetAtPath<DictionaryReferenceScriptableObject>(
"Assets/Artists/Configs/reference_dictionary.asset");
#else
var task = YooAssets.LoadAssetAsync("reference_directory");
task.WaitForAsyncComplete();
_singleton=task.AssetObject as DictionaryReferenceScriptableObject;
#endif
}
return _singleton;
}
}
private static DictionaryReferenceScriptableObject _singleton;
public static IDictionary<int,string> 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();
}
}
}