using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; namespace YooAsset.Editor { public class SettingLoader { /// /// 加载相关的配置文件 /// public static TSetting LoadSettingData() where TSetting : ScriptableObject { var settingType = typeof(TSetting); var guids = AssetDatabase.FindAssets($"t:{settingType.Name}"); if (guids.Length == 0) { Debug.LogWarning($"Create new {settingType.Name}.asset"); var setting = ScriptableObject.CreateInstance(); string filePath = $"Assets/{settingType.Name}.asset"; AssetDatabase.CreateAsset(setting, filePath); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); return setting; } else { if (guids.Length != 1) { foreach (var guid in guids) { string path = AssetDatabase.GUIDToAssetPath(guid); Debug.LogWarning($"Found multiple file : {path}"); } throw new System.Exception($"Found multiple {settingType.Name} files !"); } string filePath = AssetDatabase.GUIDToAssetPath(guids[0]); var setting = AssetDatabase.LoadAssetAtPath(filePath); return setting; } } } }