架构大更
This commit is contained in:
64
docs/References/AttributeCollection.cs
Normal file
64
docs/References/AttributeCollection.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Sirenix.OdinInspector;
|
||||
using SLSUtilities.General;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Cielonos.MainGame.Characters
|
||||
{
|
||||
[CreateAssetMenu(fileName = "AttributeCollection", menuName = "Cielonos/Characters/AttributeCollection", order = 1)]
|
||||
public class AttributeCollection : SerializedScriptableObject
|
||||
{
|
||||
[Title("Attributes")]
|
||||
[Tooltip("角色的通常属性:第一栏是属性名,第二栏是属性值")]
|
||||
public SerializedDictionary<string, float, CharacterAttributePair> originalAttributes;
|
||||
|
||||
[Tooltip("初始化时赋予给CurrentAttributes的属性:\n" +
|
||||
"第一栏是属性名,第二栏是初始化时使用对应名称的originalAttributes的数据,留空则默认为0,如果是float数字则直接使用该数字")]
|
||||
public SerializedDictionary<string, string> runtimeAttributes;
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public struct CharacterAttributePair : ISerializedPair<string, float>
|
||||
{
|
||||
[SerializeField, HideInInspector] public string attributeKey;
|
||||
|
||||
[SerializeField, HideInInspector] private bool useManualInput; // 记录当前是否处于手动输入模式
|
||||
|
||||
[ShowInInspector]
|
||||
[PropertyOrder(0)]
|
||||
[HideIf("useManualInput")]
|
||||
[HorizontalGroup("H")]
|
||||
[HideLabel]
|
||||
[ValueDropdown("@EditorBaseCollection.GetCharacterAttributesDropdown($property)", IsUniqueList = true, DropdownHeight = 400)]
|
||||
[InlineButton("ToggleMode", Icon = SdfIconType.ListUl, Label = "")]
|
||||
public string DropdownKey
|
||||
{
|
||||
get => attributeKey;
|
||||
set => attributeKey = value;
|
||||
}
|
||||
|
||||
[ShowInInspector]
|
||||
[PropertyOrder(0)]
|
||||
[ShowIf("useManualInput")]
|
||||
[HorizontalGroup("H")]
|
||||
[HideLabel]
|
||||
[InlineButton("ToggleMode", Icon = SdfIconType.PencilSquare, Label = "")]
|
||||
public string ManualKey
|
||||
{
|
||||
get => attributeKey;
|
||||
set => attributeKey = value;
|
||||
}
|
||||
|
||||
[PropertyOrder(10)] [HorizontalGroup("H", MarginLeft = 10, Width = 100)] [HideLabel]
|
||||
public float attributeValue;
|
||||
|
||||
public string Key => attributeKey;
|
||||
public float Value => attributeValue;
|
||||
|
||||
private void ToggleMode()
|
||||
{
|
||||
useManualInput = !useManualInput;
|
||||
}
|
||||
}
|
||||
}
|
||||
42
docs/References/BaseCollection.cs
Normal file
42
docs/References/BaseCollection.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using Sirenix.OdinInspector;
|
||||
using SLSUtilities.General;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Cielonos.Core
|
||||
{
|
||||
public partial class BaseCollection<T> : SerializedScriptableObject where T : BaseCollection<T>
|
||||
{
|
||||
private static T _instance;
|
||||
|
||||
public static T Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
string type = typeof(T).Name;
|
||||
if (_instance == null)
|
||||
{
|
||||
string path = $"BaseCollections/{type}";
|
||||
_instance = Resources.Load<T>(path);
|
||||
}
|
||||
#if UNITY_EDITOR
|
||||
if (_instance == null)
|
||||
{
|
||||
string[] guids = UnityEditor.AssetDatabase.FindAssets($"t:{type}");
|
||||
if (guids.Length > 0)
|
||||
{
|
||||
string path = UnityEditor.AssetDatabase.GUIDToAssetPath(guids[0]);
|
||||
_instance = UnityEditor.AssetDatabase.LoadAssetAtPath<T>(path);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
if (_instance == null)
|
||||
{
|
||||
Debug.LogError($"项目中没有找到类型为 {typeof(T).Name} 的 BaseCollection。" +
|
||||
$"请确保已创建该 ScriptableObject 并将其放置在 Resources 文件夹中。");
|
||||
}
|
||||
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
56
docs/References/EditorBaseCollection.cs
Normal file
56
docs/References/EditorBaseCollection.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Cielonos.Core;
|
||||
using Sirenix.OdinInspector;
|
||||
using SLSUtilities.General;
|
||||
using UnityEngine;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
using Sirenix.OdinInspector.Editor;
|
||||
#endif
|
||||
|
||||
namespace Cielonos.MainGame
|
||||
{
|
||||
[CreateAssetMenu(fileName = "EditorBaseCollection", menuName = "Cielonos/BaseCollections/EditorBaseCollection")]
|
||||
public partial class EditorBaseCollection : BaseCollection<EditorBaseCollection>
|
||||
{
|
||||
[Tooltip("Key 为词条 ID,Value 为词条描述(将显示在 Tooltip 中)")]
|
||||
[SerializedDictionarySettings("Key", "Description")]
|
||||
[ToolbarButton("OpenAttributeGenerator", SdfIconType.FileCode, "打开词条生成器窗口")]
|
||||
public SerializedDictionary<string, string> characterAttributes = new SerializedDictionary<string, string>();
|
||||
|
||||
#if UNITY_EDITOR
|
||||
public static IEnumerable<ValueDropdownItem<string>> GetCharacterAttributesDropdown(InspectorProperty property)
|
||||
{
|
||||
IEnumerable<ValueDropdownItem<string>> allItems = Instance.characterAttributes
|
||||
.Select(kvp => new ValueDropdownItem<string>
|
||||
{
|
||||
Text = $"{kvp.Key} ({kvp.Value})",
|
||||
Value = kvp.Key
|
||||
});
|
||||
|
||||
object dict = SerializedDictionaryHelper.GetDictionaryFromKey(property);
|
||||
if (dict is IEnumerable<KeyValuePair<string, float>> existingDict)
|
||||
{
|
||||
var usedKeys = existingDict.Select(k => k.Key).ToHashSet();
|
||||
string currentKey = property.ValueEntry.WeakSmartValue as string;
|
||||
return allItems.Where(x => !usedKeys.Contains(x.Value) || x.Value == currentKey);
|
||||
}
|
||||
|
||||
return allItems;
|
||||
}
|
||||
|
||||
private void OpenAttributeGenerator(SerializedDictionary<string, string> dict)
|
||||
{
|
||||
// 打开我们刚才写的窗口,并传入当前的字典数据
|
||||
DictionaryCodeGeneratorWindow.Open(dict, "CharacterAttribute", "Cielonos.MainGame");
|
||||
}
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
public partial class EditorBaseCollection
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user