From 2893033dcec34be1b29d08f169822cf4797be438 Mon Sep 17 00:00:00 2001 From: ls9512 <598914653@qq.com> Date: Mon, 26 Feb 2024 17:53:39 +0800 Subject: [PATCH] Ver 2024.2.26 Preview 1. Integrate most interfaces into the UBind static class and call them uniformly 2. Some APIs and class names have been changed and are incompatible with old versions. 3. The version number is changed to year and date format, consistent with other UPlugin series plug-ins. 4. Optimize editor style --- .github/README.md | 12 +- .github/README_CN.md | 14 +- Editor/Script/BaseBinderEditor.cs | 6 +- Editor/Script/ComponentBinderEditor.cs | 6 +- Editor/Script/TypeBinderEditor.cs | 6 +- Editor/Script/Util/EditorStyle.cs | 8 +- Editor/Script/Util/EditorUtil.cs | 87 ++++++++--- Editor/Script/Util/GUIStruct.cs | 39 ++++- Extension.meta | 2 +- .../Script/TMPTextFormatValueBinder.cs | 2 +- Runtime/Script/Attribute/BindAttribute.cs | 6 +- Runtime/Script/Attribute/BindTypeAttribute.cs | 2 +- .../Attribute/BindTypeSourceAttribute.cs | 2 +- .../Script/Bindable/BindableMonoBehaviour.cs | 4 +- Runtime/Script/Bindable/BindableObject.cs | 4 +- .../Script/Binder/RuntimePropertyBinder.cs | 4 +- Runtime/Script/Binder/RuntimeTypeBinder.cs | 8 +- Runtime/Script/Binder/RuntimeValueBinder.cs | 8 +- .../Script/Component/Base/ComponentBinder.cs | 6 +- Runtime/Script/Component/PropertyBinder.cs | 2 +- Runtime/Script/Component/TypeBinder.cs | 4 +- .../Component/UI/TextFormatValueBinder.cs | 2 +- Runtime/Script/Converter/DataConverter.cs | 9 +- Runtime/Script/Core/BindMap.cs | 58 +++---- Runtime/Script/Core/DataBinder(T).cs | 4 +- Runtime/Script/Core/DataBinder(T2).cs | 2 +- Runtime/Script/Core/DataBinder.cs | 10 +- Runtime/Script/Core/DataContainer.cs | 133 ++++++++++++++++ ...aContext.cs.meta => DataContainer.cs.meta} | 0 Runtime/Script/Core/DataContext.cs | 146 ------------------ Runtime/Script/Core/TypeCaches.cs | 64 ++++---- Runtime/Script/UBind.cs | 118 ++++++++------ Samples.meta | 2 +- package.json | 6 +- 34 files changed, 430 insertions(+), 356 deletions(-) create mode 100644 Runtime/Script/Core/DataContainer.cs rename Runtime/Script/Core/{DataContext.cs.meta => DataContainer.cs.meta} (100%) delete mode 100644 Runtime/Script/Core/DataContext.cs diff --git a/.github/README.md b/.github/README.md index 5e5f3a0..396fdc6 100644 --- a/.github/README.md +++ b/.github/README.md @@ -40,7 +40,7 @@ * 4.1. [Attribute](#Attribute) * 4.1.1. [Bind Value Attribute](#BindValueAttribute) * 4.1.2. [Bind Type Attribute](#BindTypeAttribute) - * 4.2. [Data Context](#DataContext) + * 4.2. [Data Container](#DataContainer) * 4.3. [Data Binder](#DataBinder) * 4.4. [Data Converter](#DataConverter) * 4.5. [Component Binder](#ComponentBinder) @@ -158,12 +158,12 @@ public class ExanokeMonoBehaviour : MonoBehaviour public void OnEnable() { - BindMap.Bind(this); + UBind.RegisterMap(this); } public void OnDisable() { - BindMap.UnBind(this); + UBind.DeRegisterMap(this); } } ``` @@ -253,7 +253,7 @@ When the data source does not have the ability to actively trigger data changes, It is used to mark attributes and fields that need to be bound in a class that has the ability to handle binding relationships. It is only recommended to bind common basic data types. The marked object will dynamically create a **RuntimeValueBinder** for processing. #### 4.1.2. Bind Type Attribute Unlike **BindValueAttribute**, it is used to mark custom class and structure type objects, and **RuntimeTypeBinder** is dynamically created for processing. -### 4.2. Data Context +### 4.2. Data Container The data container is used to maintain a set of Data Binder, which can contain multiple data sources and data destinations. Each data container is independent of each other. ### 4.3. Data Binder @@ -289,7 +289,7 @@ Used to cache all the binding structure information of the objects marked by **B *** -## 5. Built-in components +## 5. Built-in Components |Component|Binding object type|Default binding property|Binding property type| |-|-|-|-| |Text Binder|Text|text|string| @@ -311,7 +311,7 @@ Used to cache all the binding structure information of the objects marked by **B *** -## 6. Custom extension +## 6. Custom Extension ### 6.1. Custom Data Binder Implement a runtime data binder for a specific object and property by inheriting **DataBinder**: ``` cs diff --git a/.github/README_CN.md b/.github/README_CN.md index 25d313b..e33e836 100644 --- a/.github/README_CN.md +++ b/.github/README_CN.md @@ -40,7 +40,7 @@ * 4.1. [Attribute](#Attribute) * 4.1.1. [Bind Value Attribute](#BindValueAttribute) * 4.1.2. [Bind Type Attribute](#BindTypeAttribute) - * 4.2. [Data Context](#DataContext) + * 4.2. [Data Container](#DataContainer) * 4.3. [Data Binder](#DataBinder) * 4.4. [Data Converter](#DataConverter) * 4.5. [Component Binder](#ComponentBinder) @@ -158,12 +158,12 @@ public class ExanokeMonoBehaviour : MonoBehaviour public void OnEnable() { - BindMap.Bind(this); + UBind.RegisterMap(this); } public void OnDisable() { - BindMap.UnBind(this); + UBind.DeRegisterMap(this); } } ``` @@ -253,7 +253,7 @@ public class ExampleClass 用于在拥有处理绑定关系能力的类中标记需要绑定的属性和字段,只推荐绑定常见基础数据类型。被标记对象会动态创建 **RuntimeValueBinder** 进行处理。 #### 4.1.2. Bind Type Attribute 与 **BindValueAttribute** 的不同,用于标记自定义类和结构体类型对象,会动态创建 **RuntimeTypeBinder** 进行处理。 -### 4.2. Data Context +### 4.2. Data Container 数据容器,用于维护一组 Data Binder,可以包含多个数据源和数据目标点。每个数据容器相互独立。 ### 4.3. Data Binder @@ -275,7 +275,7 @@ Convert.ChangeType(object data, Type type); ``` 可以按需要使用以下接口提前注册特定类型的自定义转换器: ``` cs -DataConvert.Register(Type sourceType, Type targetType, DataConverter dataConverter); +UBind.RegisterConverter(Type sourceType, Type targetType, DataConverter dataConverter); ``` ### 4.5. Component Binder @@ -285,7 +285,7 @@ DataConvert.Register(Type sourceType, Type targetType, DataConverter dataConvert 基于 **MonoBehaviour** 生命周期实现,用于统一维护需要主动更新数据的 **DataBinder** 的更新周期。 ### 4.7. Bind Map - +用于缓存类中所有被 **BindAttribute** 所标记的字段和属性信息。 *** @@ -365,7 +365,7 @@ public class CustomDataConverter : DataConverter ``` cs var sourcetType = sourceData.GetType(); var targetType = targetData.GetType(); -DataConverter.Register((sourceType, targetType), new CustomDataConverter()); +UBind.RegisterConverter((sourceType, targetType), new CustomDataConverter()); ``` *** diff --git a/Editor/Script/BaseBinderEditor.cs b/Editor/Script/BaseBinderEditor.cs index 3bc77b0..02ba95d 100644 --- a/Editor/Script/BaseBinderEditor.cs +++ b/Editor/Script/BaseBinderEditor.cs @@ -6,9 +6,9 @@ namespace Aya.DataBinding { public class BaseBinderEditor : Editor { - protected void DrawContextKey(SerializedProperty property) + protected void DrawContainerKey(SerializedProperty property) { - if (property.stringValue == DataContext.Default) + if (property.stringValue == DataContainer.Default) { using (new ColorScope(Color.gray)) { @@ -34,7 +34,7 @@ protected void DrawDataKey(SerializedProperty property) protected void DrawDirection(SerializedProperty property) { - EditorGUILayout.PropertyField(property); + EditorUtil.DrawToolbarEnum(property, typeof(DataDirection)); } protected void DrawTarget(SerializedProperty property, Transform root) where TComponent : Component diff --git a/Editor/Script/ComponentBinderEditor.cs b/Editor/Script/ComponentBinderEditor.cs index 181864e..b4dfccd 100644 --- a/Editor/Script/ComponentBinderEditor.cs +++ b/Editor/Script/ComponentBinderEditor.cs @@ -11,7 +11,7 @@ public abstract class ComponentBinderEditor : B { public ComponentBinder ComponentBinder => target as ComponentBinder; - protected SerializedProperty ContextKeyProperty; + protected SerializedProperty ContainerKeyProperty; protected SerializedProperty DataKeyProperty; protected SerializedProperty DirectionProperty; protected SerializedProperty UpdateTypeProperty; @@ -19,7 +19,7 @@ public abstract class ComponentBinderEditor : B public virtual void OnEnable() { - ContextKeyProperty = serializedObject.FindProperty(nameof(ComponentBinder.Context)); + ContainerKeyProperty = serializedObject.FindProperty(nameof(ComponentBinder.Container)); DataKeyProperty = serializedObject.FindProperty(nameof(ComponentBinder.Key)); DirectionProperty = serializedObject.FindProperty(nameof(ComponentBinder.Direction)); UpdateTypeProperty = serializedObject.FindProperty(nameof(ComponentBinder.UpdateType)); @@ -30,7 +30,7 @@ public override void OnInspectorGUI() { serializedObject.Update(); - DrawContextKey(ContextKeyProperty); + DrawContainerKey(ContainerKeyProperty); DrawDataKey(DataKeyProperty); DrawDirection(DirectionProperty); diff --git a/Editor/Script/TypeBinderEditor.cs b/Editor/Script/TypeBinderEditor.cs index 4f4dd82..6aa505f 100644 --- a/Editor/Script/TypeBinderEditor.cs +++ b/Editor/Script/TypeBinderEditor.cs @@ -10,7 +10,7 @@ public class TypeBinderEditor : BaseBinderEditor { public TypeBinder TypeBinder => target as TypeBinder; - protected SerializedProperty ContextKeyProperty; + protected SerializedProperty ContainerKeyProperty; protected SerializedProperty DataKeyProperty; protected SerializedProperty DirectionProperty; protected SerializedProperty UpdateTypeProperty; @@ -21,7 +21,7 @@ public class TypeBinderEditor : BaseBinderEditor public virtual void OnEnable() { - ContextKeyProperty = serializedObject.FindProperty("Context"); + ContainerKeyProperty = serializedObject.FindProperty("Container"); DataKeyProperty = serializedObject.FindProperty("Key"); DirectionProperty = serializedObject.FindProperty("Direction"); UpdateTypeProperty = serializedObject.FindProperty("UpdateType"); @@ -35,7 +35,7 @@ public override void OnInspectorGUI() { serializedObject.Update(); - DrawContextKey(ContextKeyProperty); + DrawContainerKey(ContainerKeyProperty); DrawDataKey(DataKeyProperty); DrawDirection(DirectionProperty); diff --git a/Editor/Script/Util/EditorStyle.cs b/Editor/Script/Util/EditorStyle.cs index 160e520..e239cf6 100644 --- a/Editor/Script/Util/EditorStyle.cs +++ b/Editor/Script/Util/EditorStyle.cs @@ -14,11 +14,9 @@ public static GUIStyle RichLabel { get { - if (_richLabel == null) - { - _richLabel = EditorStyles.label; - _richLabel.richText = true; - } + if (_richLabel != null) return _richLabel; + _richLabel = EditorStyles.label; + _richLabel.richText = true; return _richLabel; } diff --git a/Editor/Script/Util/EditorUtil.cs b/Editor/Script/Util/EditorUtil.cs index 703dfa6..200e1fb 100644 --- a/Editor/Script/Util/EditorUtil.cs +++ b/Editor/Script/Util/EditorUtil.cs @@ -17,8 +17,9 @@ public static SearchableDropdownItem CreateAssemblyMenu() root.AddSeparator(); var assemblies = TypeCaches.Assemblies; - foreach (var assembly in assemblies) + for (var i = 0; i < assemblies.Length; i++) { + var assembly = assemblies[i]; var assemblyName = assembly.GetName().Name; var child = new SearchableDropdownItem(assemblyName, assemblyName); root.AddChild(child); @@ -57,19 +58,18 @@ public static SearchableDropdownItem CreateTypeMenu(string assemblyName) root.AddSeparator(); var assembly = TypeCaches.GetAssemblyByName(assemblyName); - if (assembly != null) + if (assembly == null) return root; + var types = assembly.GetTypes(); + for (var i = 0; i < types.Length; i++) { - var types = assembly.GetTypes(); - foreach (var type in types) - { - if (type.IsAbstract) continue; - if (type.IsInterface) continue; - if (type.IsGenericType) continue; - if (type.IsEnum) continue; + var type = types[i]; + if (type.IsAbstract) continue; + if (type.IsInterface) continue; + if (type.IsGenericType) continue; + if (type.IsEnum) continue; - var child = new SearchableDropdownItem(type.Name, type.FullName); - root.AddChild(child); - } + var child = new SearchableDropdownItem(type.Name, type.FullName); + root.AddChild(child); } return root; @@ -113,8 +113,9 @@ public static SearchableDropdownItem CreateComponentsTreeMenu(Transf private static void CreateComponentsTreeMenuRecursion(SearchableDropdownItem root, Transform parent, string path) where TComponent : Component { var components = parent.GetComponents(); - foreach (var component in components) + for (var i = 0; i < components.Length; i++) { + var component = components[i]; var componentName = component.GetType().Name; var child = new SearchableDropdownItem(path + componentName, component) { @@ -192,8 +193,9 @@ public static GenericMenu CreatePropertyMenu(Type type, SerializedProperty prope var propertyInfos = TypeCaches.GetTypeProperties(type); var prefix = "Property/"; - foreach (var propertyInfo in propertyInfos) + for (var i = 0; i < propertyInfos.Count; i++) { + var propertyInfo = propertyInfos[i]; // if (!TypeCaches.BindableTypes.Contains(propertyInfo.PropertyType)) continue; var displayName = propertyInfo.Name + "\t\t" + propertyInfo.PropertyType.Name; menu.AddItem(new GUIContent(prefix + displayName), propertyInfo.Name == property.stringValue, () => @@ -206,8 +208,9 @@ public static GenericMenu CreatePropertyMenu(Type type, SerializedProperty prope var filedInfos = TypeCaches.GetTypeFields(type); prefix = "Field/"; - foreach (var fieldInfo in filedInfos) + for (var i = 0; i < filedInfos.Count; i++) { + var fieldInfo = filedInfos[i]; // if (!TypeCaches.BindableTypes.Contains(fieldInfo.FieldType)) continue; var displayName = fieldInfo.Name + "\t\t" + fieldInfo.FieldType.Name; menu.AddItem(new GUIContent(prefix + displayName), fieldInfo.Name == property.stringValue, () => @@ -236,12 +239,7 @@ public static void PropertyTreeMenu(string propertyName, Type type, SerializedPr return (propertyInfo.Name, propertyInfo.PropertyType.Name); } - if (filedInfo != null) - { - return (filedInfo.Name, filedInfo.FieldType.Name); - } - - return (EditorStyle.NoneStr, ""); + return filedInfo != null ? (filedInfo.Name, filedInfo.FieldType.Name) : (EditorStyle.NoneStr, ""); } var (currentPropertyName, currentPropertyTypeName) = GetCurrentPropertyInfo(); @@ -328,7 +326,52 @@ public static void ColorLine(Color color, float height) GUILayout.EndVertical(); var rect = GUILayoutUtility.GetLastRect(); EditorGUI.DrawRect(rect, color); - } + } + + #endregion + + #region Enum + + public static void DrawToolbarEnum(SerializedProperty property, Type enumType) + { + DrawToolbarEnum(property, property.displayName, enumType); + } + + public static void DrawToolbarEnum(SerializedProperty property, string propertyName, Type enumType) + { + property.intValue = DrawToolbarEnum(property.intValue, propertyName, enumType); + } + + public static int DrawToolbarEnum(int value, string propertyName, Type enumType) + { + using (GUIHorizontal.Create()) + { + GUILayout.Label(propertyName, EditorStyles.label, GUILayout.Width(EditorGUIUtility.labelWidth)); + var buttons = Enum.GetNames(enumType); + var style = EditorStyles.miniButton; + style.margin = new RectOffset(); + var rect = EditorGUILayout.GetControlRect(false, EditorGUIUtility.singleLineHeight, style); + var btnWidth = rect.width / buttons.Length; + for (var i = 0; i < buttons.Length; i++) + { + var button = buttons[i]; + var index = i; + var btnRect = rect; + btnRect.x += i * btnWidth; + btnRect.width = btnWidth; + using (GUIColorArea.Create(Color.white, Color.gray * 1.5f, value == index)) + { + var btn = GUI.Button(btnRect, button, style); + if (btn) + { + return index; + } + } + } + } + + return value; + } #endregion } diff --git a/Editor/Script/Util/GUIStruct.cs b/Editor/Script/Util/GUIStruct.cs index 2ca30e7..a3c771e 100644 --- a/Editor/Script/Util/GUIStruct.cs +++ b/Editor/Script/Util/GUIStruct.cs @@ -160,13 +160,44 @@ public static void SetState(Object target, string title, bool value) public static Dictionary GetStateDic(Object target) { - if (!StateCacheDic.TryGetValue(target, out var stateDic)) + if (StateCacheDic.TryGetValue(target, out var stateDic)) return stateDic; + stateDic = new Dictionary(); + StateCacheDic.Add(target, stateDic); + + return stateDic; + } + } + + internal struct GUIColorArea : IDisposable + { + public Color OriginalColor; + + public static GUIColorArea Create(Color color, bool check = true) + { + return new GUIColorArea(color, Color.white, check); + } + + public static GUIColorArea Create(Color enableColor, Color disableColor, bool check = true) + { + return new GUIColorArea(enableColor, disableColor, check); + } + + public GUIColorArea(Color enableColor, Color disableColor, bool check = true) + { + OriginalColor = GUI.color; + if (check) + { + GUI.color = enableColor; + } + else { - stateDic = new Dictionary(); - StateCacheDic.Add(target, stateDic); + GUI.color = disableColor; } + } - return stateDic; + public void Dispose() + { + GUI.color = OriginalColor; } } } diff --git a/Extension.meta b/Extension.meta index 2bff7c7..59c8726 100644 --- a/Extension.meta +++ b/Extension.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 16a3d1bb53850d74dae9d7d04ab414df +guid: 207507ed0e2b6c54b9f611daf05c40ed folderAsset: yes DefaultImporter: externalObjects: {} diff --git a/Extension/TextMeshPro/Script/TMPTextFormatValueBinder.cs b/Extension/TextMeshPro/Script/TMPTextFormatValueBinder.cs index 9529539..48fffe4 100644 --- a/Extension/TextMeshPro/Script/TMPTextFormatValueBinder.cs +++ b/Extension/TextMeshPro/Script/TMPTextFormatValueBinder.cs @@ -17,7 +17,7 @@ public override RuntimeTMPTextFormatValueBinder CreateDataBinder() var dataBinder = new RuntimeTMPTextFormatValueBinder { Target = Target, - Context = Context, + Container = Container, Direction = Direction, Key = Key, Text = Text diff --git a/Runtime/Script/Attribute/BindAttribute.cs b/Runtime/Script/Attribute/BindAttribute.cs index e708f18..ff96344 100644 --- a/Runtime/Script/Attribute/BindAttribute.cs +++ b/Runtime/Script/Attribute/BindAttribute.cs @@ -5,7 +5,7 @@ namespace Aya.DataBinding [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)] public abstract class BindAttribute : Attribute { - public string Context = DataContext.Default; + public string Container = DataContainer.Default; public string Key; public DataDirection Direction; @@ -15,9 +15,9 @@ protected BindAttribute(string dataKey, DataDirection direction) Direction = direction; } - protected BindAttribute(string contextKey, string dataKey, DataDirection direction) + protected BindAttribute(string containerKey, string dataKey, DataDirection direction) { - Context = contextKey; + Container = containerKey; Key = dataKey; Direction = direction; } diff --git a/Runtime/Script/Attribute/BindTypeAttribute.cs b/Runtime/Script/Attribute/BindTypeAttribute.cs index 4736804..13cb457 100644 --- a/Runtime/Script/Attribute/BindTypeAttribute.cs +++ b/Runtime/Script/Attribute/BindTypeAttribute.cs @@ -9,7 +9,7 @@ public BindTypeAttribute(string dataKey, DataDirection direction) : base(dataKey { } - public BindTypeAttribute(string contextKey, string dataKey, DataDirection direction) : base(contextKey, dataKey, direction) + public BindTypeAttribute(string containerKey, string dataKey, DataDirection direction) : base(containerKey, dataKey, direction) { } } diff --git a/Runtime/Script/Attribute/BindTypeSourceAttribute.cs b/Runtime/Script/Attribute/BindTypeSourceAttribute.cs index 593235e..85b42ab 100644 --- a/Runtime/Script/Attribute/BindTypeSourceAttribute.cs +++ b/Runtime/Script/Attribute/BindTypeSourceAttribute.cs @@ -9,7 +9,7 @@ public BindTypeBothAttribute(string dataKey) : base(dataKey, DataDirection.Both) { } - public BindTypeBothAttribute(string contextKey, string dataKey) : base(contextKey, dataKey, DataDirection.Both) + public BindTypeBothAttribute(string containerKey, string dataKey) : base(containerKey, dataKey, DataDirection.Both) { } } diff --git a/Runtime/Script/Bindable/BindableMonoBehaviour.cs b/Runtime/Script/Bindable/BindableMonoBehaviour.cs index b0bade7..5bc5fc9 100644 --- a/Runtime/Script/Bindable/BindableMonoBehaviour.cs +++ b/Runtime/Script/Bindable/BindableMonoBehaviour.cs @@ -18,12 +18,12 @@ public BindMap BindMap public virtual void OnEnable() { - BindMap.Bind(this); + UBind.RegisterMap(this); } public virtual void OnDisable() { - BindMap.UnBind(this); + UBind.DeRegisterMap(this); } } } diff --git a/Runtime/Script/Bindable/BindableObject.cs b/Runtime/Script/Bindable/BindableObject.cs index 4e28305..4b36394 100644 --- a/Runtime/Script/Bindable/BindableObject.cs +++ b/Runtime/Script/Bindable/BindableObject.cs @@ -17,12 +17,12 @@ public BindMap BindMap protected BindableObject() { - BindMap.Bind(this); + UBind.RegisterMap(this); } ~BindableObject() { - BindMap.UnBind(this); + UBind.DeRegisterMap(this); } } } diff --git a/Runtime/Script/Binder/RuntimePropertyBinder.cs b/Runtime/Script/Binder/RuntimePropertyBinder.cs index 578f39c..207acbd 100644 --- a/Runtime/Script/Binder/RuntimePropertyBinder.cs +++ b/Runtime/Script/Binder/RuntimePropertyBinder.cs @@ -9,9 +9,9 @@ public class RuntimePropertyBinder : RuntimePropertyBinder public override Type TargetType => Target.GetType(); - public RuntimePropertyBinder(string context, string key, DataDirection direction, object target, PropertyInfo propertyInfo, FieldInfo fieldInfo) + public RuntimePropertyBinder(string container, string key, DataDirection direction, object target, PropertyInfo propertyInfo, FieldInfo fieldInfo) { - Context = context; + Container = container; Key = key; Direction = direction; Target = target; diff --git a/Runtime/Script/Binder/RuntimeTypeBinder.cs b/Runtime/Script/Binder/RuntimeTypeBinder.cs index f60751b..6bdcc18 100644 --- a/Runtime/Script/Binder/RuntimeTypeBinder.cs +++ b/Runtime/Script/Binder/RuntimeTypeBinder.cs @@ -9,9 +9,9 @@ public class RuntimeTypeBinder : DataBinder private List _binderCaches; - public RuntimeTypeBinder(string context, string key, DataDirection direction, object target) + public RuntimeTypeBinder(string container, string key, DataDirection direction, object target) { - Context = context; + Container = container; Key = key; Direction = direction; Target = target; @@ -27,14 +27,14 @@ public override void Bind() foreach (var propertyInfo in properties) { var key = type.Name + "." + propertyInfo.Name + "." + Key; - var binder = new RuntimePropertyBinder(Context, key, Direction, Target, propertyInfo, null); + var binder = new RuntimePropertyBinder(Container, key, Direction, Target, propertyInfo, null); _binderCaches.Add(binder); } foreach (var fieldInfo in fields) { var key = type.Name + "." + fieldInfo.Name + "." + Key; - var binder = new RuntimePropertyBinder(Context, key, Direction, Target, null, fieldInfo); + var binder = new RuntimePropertyBinder(Container, key, Direction, Target, null, fieldInfo); _binderCaches.Add(binder); } } diff --git a/Runtime/Script/Binder/RuntimeValueBinder.cs b/Runtime/Script/Binder/RuntimeValueBinder.cs index 4b6ea81..b6fafe8 100644 --- a/Runtime/Script/Binder/RuntimeValueBinder.cs +++ b/Runtime/Script/Binder/RuntimeValueBinder.cs @@ -9,17 +9,17 @@ public class RuntimeValueBinder : DataBinder public Func Getter; public Action Setter; - public RuntimeValueBinder(string context, string key, Func getter) : this(context, key, DataDirection.Source, getter, null) + public RuntimeValueBinder(string container, string key, Func getter) : this(container, key, DataDirection.Source, getter, null) { } - public RuntimeValueBinder(string context, string key, Action setter) : this(context, key, DataDirection.Target, null, setter) + public RuntimeValueBinder(string container, string key, Action setter) : this(container, key, DataDirection.Target, null, setter) { } - public RuntimeValueBinder(string context, string key, DataDirection direction, Func getter, Action setter) + public RuntimeValueBinder(string container, string key, DataDirection direction, Func getter, Action setter) { - Context = context; + Container = container; Key = key; Direction = direction; Getter = getter; diff --git a/Runtime/Script/Component/Base/ComponentBinder.cs b/Runtime/Script/Component/Base/ComponentBinder.cs index e603b67..fa5008e 100644 --- a/Runtime/Script/Component/Base/ComponentBinder.cs +++ b/Runtime/Script/Component/Base/ComponentBinder.cs @@ -6,14 +6,14 @@ public abstract class ComponentBinder : MonoBeh where TDataBinder : DataBinder, new() where TComponent : Component { - public string Context = DataContext.Default; + public string Container = DataContainer.Default; public string Key; public DataDirection Direction = DataDirection.Target; public UpdateType UpdateType = UpdateType.Update; public TComponent Target; public TDataBinder DataBinder { get; internal set; } - public DataContext DataContext => DataBinder?.DataContext; + public DataContainer DataContainer => DataBinder?.DataContainer; public bool IsDestination => Direction == DataDirection.Target || Direction == DataDirection.Both; public bool IsSource => Direction == DataDirection.Source || Direction == DataDirection.Both; @@ -47,7 +47,7 @@ public virtual TDataBinder CreateDataBinder() { Target = Target, TargetType = typeof(TComponent), - Context = Context, + Container = Container, Direction = Direction, UpdateType = UpdateType, Key = Key diff --git a/Runtime/Script/Component/PropertyBinder.cs b/Runtime/Script/Component/PropertyBinder.cs index 5ed6369..a8e5530 100644 --- a/Runtime/Script/Component/PropertyBinder.cs +++ b/Runtime/Script/Component/PropertyBinder.cs @@ -19,7 +19,7 @@ public override RuntimePropertyBinder CreateDataBinder() Target = Target, TargetType = Target.GetType(), Property = Property, - Context = Context, + Container = Container, Direction = Direction, Key = Key }; diff --git a/Runtime/Script/Component/TypeBinder.cs b/Runtime/Script/Component/TypeBinder.cs index 578ebb8..1f0f1be 100644 --- a/Runtime/Script/Component/TypeBinder.cs +++ b/Runtime/Script/Component/TypeBinder.cs @@ -15,7 +15,7 @@ public class TypeBindMap [AddComponentMenu("Data Binding/Type Binder")] public class TypeBinder : MonoBehaviour { - public string Context = DataContext.Default; + public string Container = DataContainer.Default; public string Key; public DataDirection Direction = DataDirection.Target; @@ -35,7 +35,7 @@ public virtual void OnEnable() { var key = type.Name + "." + map.Property + "." + Key; var (property, field) = TypeCaches.GetTypePropertyOrFieldByName(map.Target.GetType(), map.TargetProperty); - var binder = new RuntimePropertyBinder(Context, key, Direction, map.Target, property, field); + var binder = new RuntimePropertyBinder(Container, key, Direction, map.Target, property, field); _binderCaches.Add(binder); } } diff --git a/Runtime/Script/Component/UI/TextFormatValueBinder.cs b/Runtime/Script/Component/UI/TextFormatValueBinder.cs index 47ddd7b..9fc41f3 100644 --- a/Runtime/Script/Component/UI/TextFormatValueBinder.cs +++ b/Runtime/Script/Component/UI/TextFormatValueBinder.cs @@ -17,7 +17,7 @@ public override RuntimeTextFormatValueBinder CreateDataBinder() var dataBinder = new RuntimeTextFormatValueBinder { Target = Target, - Context = Context, + Container = Container, Direction = Direction, Key = Key, Text = Text diff --git a/Runtime/Script/Converter/DataConverter.cs b/Runtime/Script/Converter/DataConverter.cs index ef0247c..2655dcc 100644 --- a/Runtime/Script/Converter/DataConverter.cs +++ b/Runtime/Script/Converter/DataConverter.cs @@ -10,19 +10,14 @@ public abstract class DataConverter public abstract object To(object value, Type convertType); - public static void Register(Type sourceType, Type targetType, DataConverter dataConverter) + public static void RegisterConverter(Type sourceType, Type targetType, DataConverter dataConverter) { ConverterDic[(sourceType, targetType)] = dataConverter; } public static DataConverter GetConverter(Type sourceType, Type targetType) { - if (ConverterDic.TryGetValue((sourceType, targetType), out var converter)) - { - return converter; - } - - return Default; + return ConverterDic.TryGetValue((sourceType, targetType), out var converter) ? converter : Default; } } } diff --git a/Runtime/Script/Core/BindMap.cs b/Runtime/Script/Core/BindMap.cs index 0aadf78..72550b4 100644 --- a/Runtime/Script/Core/BindMap.cs +++ b/Runtime/Script/Core/BindMap.cs @@ -24,29 +24,29 @@ public static BindMap GetBindMap(object target) public static BindMap GetBindMap(Type type) { - if (!MapDic.TryGetValue(type, out var bindMap)) + if (MapDic.TryGetValue(type, out var bindMap)) return bindMap; + bindMap = new BindMap() { - bindMap = new BindMap() - { - Type = type - }; + Type = type + }; - var bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static; - var propertyInfos = type.GetProperties(bindingFlags); - foreach (var propertyInfo in propertyInfos) - { - var bindAttribute = propertyInfo.GetCustomAttribute(); - if (bindAttribute == null) continue; - bindMap.PropertyInfos.Add(propertyInfo, bindAttribute); - } + var bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static; + var propertyInfos = type.GetProperties(bindingFlags); + for (var i = 0; i < propertyInfos.Length; i++) + { + var propertyInfo = propertyInfos[i]; + var bindAttribute = propertyInfo.GetCustomAttribute(); + if (bindAttribute == null) continue; + bindMap.PropertyInfos.Add(propertyInfo, bindAttribute); + } - var fieldInfos = type.GetFields(bindingFlags); - foreach (var fieldInfo in fieldInfos) - { - var bindAttribute = fieldInfo.GetCustomAttribute(); - if (bindAttribute == null) continue; - bindMap.FieldInfos.Add(fieldInfo, bindAttribute); - } + var fieldInfos = type.GetFields(bindingFlags); + for (var i = 0; i < fieldInfos.Length; i++) + { + var fieldInfo = fieldInfos[i]; + var bindAttribute = fieldInfo.GetCustomAttribute(); + if (bindAttribute == null) continue; + bindMap.FieldInfos.Add(fieldInfo, bindAttribute); } return bindMap; @@ -54,9 +54,9 @@ public static BindMap GetBindMap(Type type) #endregion - #region Bind / UnBind + #region Register / DeRegister - public static void Bind(object target) + public static void RegisterMap(object target) { var bindMap = GetBindMap(target); foreach (var kv in bindMap.PropertyInfos) @@ -64,20 +64,20 @@ public static void Bind(object target) var propertyInfo = kv.Key; var bindAttribute = kv.Value; - var context = bindAttribute.Context; + var container = bindAttribute.Container; var key = bindAttribute.Key; var direction = bindAttribute.Direction; if (bindAttribute is BindValueAttribute) { - var propertyBinder = new RuntimePropertyBinder(context, key, direction, target, propertyInfo, null); + var propertyBinder = new RuntimePropertyBinder(container, key, direction, target, propertyInfo, null); propertyBinder.Bind(); } if (bindAttribute is BindTypeAttribute) { var obj = propertyInfo.GetValue(target, null); - var propertyBinder = new RuntimeTypeBinder(context, key, direction, obj); + var propertyBinder = new RuntimeTypeBinder(container, key, direction, obj); propertyBinder.Bind(); } } @@ -87,26 +87,26 @@ public static void Bind(object target) var fieldInfo = kv.Key; var bindAttribute = kv.Value; - var context = bindAttribute.Context; + var container = bindAttribute.Container; var key = bindAttribute.Key; var direction = bindAttribute.Direction; if (bindAttribute is BindValueAttribute) { - var propertyBinder = new RuntimePropertyBinder(context, key, direction, target, null, fieldInfo); + var propertyBinder = new RuntimePropertyBinder(container, key, direction, target, null, fieldInfo); propertyBinder.Bind(); } if (bindAttribute is BindTypeAttribute) { var obj = fieldInfo.GetValue(target); - var propertyBinder = new RuntimeTypeBinder(context, key, direction, obj); + var propertyBinder = new RuntimeTypeBinder(container, key, direction, obj); propertyBinder.Bind(); } } } - public static void UnBind(object target) + public static void DeRegisterMap(object target) { foreach (var binder in BindUpdater.Ins.UpdateSourceList) { diff --git a/Runtime/Script/Core/DataBinder(T).cs b/Runtime/Script/Core/DataBinder(T).cs index 694df9c..14b8159 100644 --- a/Runtime/Script/Core/DataBinder(T).cs +++ b/Runtime/Script/Core/DataBinder(T).cs @@ -54,7 +54,7 @@ public virtual void OnValueChangedCallback(T data) public override void Broadcast() { if (!IsSource) return; - var dataBinders = DataContext.GetDestinations(Key); + var dataBinders = DataContainer.GetDestinations(Key); var data = Value; PreviousData = data; PreviousDataHashCode = data.GetHashCode(); @@ -86,7 +86,7 @@ public override void UpdateSource() public override void UpdateTarget() { if (!IsDestination) return; - var latestData = DataContext.GetData(Context, Key); + var latestData = DataContainer.GetData(Container, Key); Value = latestData; } diff --git a/Runtime/Script/Core/DataBinder(T2).cs b/Runtime/Script/Core/DataBinder(T2).cs index fde0553..79ebe9b 100644 --- a/Runtime/Script/Core/DataBinder(T2).cs +++ b/Runtime/Script/Core/DataBinder(T2).cs @@ -10,7 +10,7 @@ public abstract class DataBinder : DataBinder public override void UpdateSource() { - if(Target == null) + if (Target == null) { return; } diff --git a/Runtime/Script/Core/DataBinder.cs b/Runtime/Script/Core/DataBinder.cs index 77c6965..fb95d65 100644 --- a/Runtime/Script/Core/DataBinder.cs +++ b/Runtime/Script/Core/DataBinder.cs @@ -20,7 +20,7 @@ public enum UpdateType public abstract class DataBinder { - public string Context = DataContext.Default; + public string Container = DataContainer.Default; public string Key; public DataDirection Direction = DataDirection.Target; @@ -53,7 +53,7 @@ public virtual PropertyInfo ValuePropertyInfo public bool IsDestination => Direction == DataDirection.Target || Direction == DataDirection.Both; public bool IsSource => Direction == DataDirection.Source || Direction == DataDirection.Both; - public DataContext DataContext { get; internal set; } + public DataContainer DataContainer { get; internal set; } public virtual bool NeedUpdate => false; @@ -80,7 +80,7 @@ public virtual void Bind() AddListener(); } - DataContext.Bind(this); + DataContainer.Bind(this); } public virtual void RemoveListener() @@ -99,7 +99,7 @@ public virtual void UnBind() RemoveListener(); } - DataContext.UnBind(this); + DataContainer.UnBind(this); } public abstract void Broadcast(); @@ -127,4 +127,4 @@ internal virtual object GetDataInternal(Type convertType) return convertData; } } -} +} \ No newline at end of file diff --git a/Runtime/Script/Core/DataContainer.cs b/Runtime/Script/Core/DataContainer.cs new file mode 100644 index 0000000..2e23cae --- /dev/null +++ b/Runtime/Script/Core/DataContainer.cs @@ -0,0 +1,133 @@ +using System.Collections.Generic; + +namespace Aya.DataBinding +{ + public class DataContainer + { + public const string Default = "Default"; + + public string Key { get; protected set; } + + #region Data Context + + public static Dictionary ContextDic = new Dictionary(); + + public static DataContainer GetContainer(string key) + { + if (ContextDic.TryGetValue(key, out var dataContainer)) return dataContainer; + dataContainer = new DataContainer() + { + Key = key + }; + + ContextDic.Add(key, dataContainer); + + return dataContainer; + } + + #endregion + + #region Data Binder + + public Dictionary> DestinationDic = new Dictionary>(); + public Dictionary> SourceDic = new Dictionary>(); + + public List GetDestinations(string key) + { + if (DestinationDic.TryGetValue(key, out var dataBinders)) return dataBinders; + dataBinders = new List(); + DestinationDic.Add(key, dataBinders); + + return dataBinders; + } + + public List GetSources(string key) + { + if (SourceDic.TryGetValue(key, out var dataBinders)) return dataBinders; + dataBinders = new List(); + SourceDic.Add(key, dataBinders); + + return dataBinders; + } + + #endregion + + #region Bind / UnBind + + public static void Bind(DataBinder dataBinder) + { + var dataContext = GetContainer(dataBinder.Container); + if (dataBinder.IsDestination) BindDestination(dataContext, dataBinder); + if (dataBinder.IsSource) BindSource(dataContext, dataBinder); + } + + internal static void BindDestination(DataContainer dataContainer, DataBinder dataBinder) + { + var dataBinders = dataContainer.GetDestinations(dataBinder.Key); + if (dataBinders.Contains(dataBinder)) return; + dataBinder.DataContainer = dataContainer; + dataBinders.Add(dataBinder); + } + + internal static void BindSource(DataContainer dataContainer, DataBinder dataBinder) + { + var dataBinders = dataContainer.GetSources(dataBinder.Key); + if (dataBinders.Contains(dataBinder)) return; + dataBinder.DataContainer = dataContainer; + dataBinders.Add(dataBinder); + } + + public static void UnBind(DataBinder dataBinder) + { + var dataContext = GetContainer(dataBinder.Container); + if (dataBinder.IsDestination) UnBindDestination(dataContext, dataBinder); + if (dataBinder.IsSource) UnBindSource(dataContext, dataBinder); + } + + internal static void UnBindDestination(DataContainer dataContainer, DataBinder dataBinder) + { + var dataBinders = dataContainer.GetDestinations(dataBinder.Key); + if (!dataBinders.Contains(dataBinder)) return; + dataBinder.DataContainer = null; + dataBinders.Remove(dataBinder); + } + + internal static void UnBindSource(DataContainer dataContainer, DataBinder dataBinder) + { + var dataBinders = dataContainer.GetSources(dataBinder.Key); + if (!dataBinders.Contains(dataBinder)) return; + dataBinder.DataContainer = null; + dataBinders.Remove(dataBinder); + } + + #endregion + + #region Data + + public static T GetData(string containerKey, string key) + { + var dataContext = GetContainer(containerKey); + var result = dataContext.GetData(key); + return result; + } + + public T GetData(string key) + { + var dataBinders = GetSources(key); + if (dataBinders.Count == 0) return default; + for (var i = 0; i < dataBinders.Count; i++) + { + var dataBinder = dataBinders[i]; + var sourceDataBinder = dataBinder as DataBinder; + if (sourceDataBinder == null) continue; + var result = sourceDataBinder.Value; + return result; + } + + var tryConvertData = (T)dataBinders[0].GetDataInternal(typeof(T)); + return tryConvertData; + } + + #endregion + } +} diff --git a/Runtime/Script/Core/DataContext.cs.meta b/Runtime/Script/Core/DataContainer.cs.meta similarity index 100% rename from Runtime/Script/Core/DataContext.cs.meta rename to Runtime/Script/Core/DataContainer.cs.meta diff --git a/Runtime/Script/Core/DataContext.cs b/Runtime/Script/Core/DataContext.cs deleted file mode 100644 index 9132faa..0000000 --- a/Runtime/Script/Core/DataContext.cs +++ /dev/null @@ -1,146 +0,0 @@ -using System.Collections.Generic; - -namespace Aya.DataBinding -{ - public class DataContext - { - public const string Default = "Default"; - - public string Key { get; protected set; } - - #region Data Context - - public static Dictionary ContextDic = new Dictionary(); - - public static DataContext GetContext(string key) - { - if (!ContextDic.TryGetValue(key, out var dataContext)) - { - dataContext = new DataContext() - { - Key = key - }; - - ContextDic.Add(key, dataContext); - } - - return dataContext; - } - - #endregion - - #region Data Binder - - public Dictionary> DestinationDic = new Dictionary>(); - public Dictionary> SourceDic = new Dictionary>(); - - public List GetDestinations(string key) - { - if (!DestinationDic.TryGetValue(key, out var dataBinders)) - { - dataBinders = new List(); - DestinationDic.Add(key, dataBinders); - } - - return dataBinders; - } - - public List GetSources(string key) - { - if (!SourceDic.TryGetValue(key, out var dataBinders)) - { - dataBinders = new List(); - SourceDic.Add(key, dataBinders); - } - - return dataBinders; - } - - #endregion - - #region Bind / UnBind - - public static void Bind(DataBinder dataBinder) - { - var dataContext = GetContext(dataBinder.Context); - if (dataBinder.IsDestination) BindDestination(dataContext, dataBinder); - if (dataBinder.IsSource) BindSource(dataContext, dataBinder); - } - - internal static void BindDestination(DataContext dataContext, DataBinder dataBinder) - { - var dataBinders = dataContext.GetDestinations(dataBinder.Key); - if (!dataBinders.Contains(dataBinder)) - { - dataBinder.DataContext = dataContext; - dataBinders.Add(dataBinder); - } - } - - internal static void BindSource(DataContext dataContext, DataBinder dataBinder) - { - var dataBinders = dataContext.GetSources(dataBinder.Key); - if (!dataBinders.Contains(dataBinder)) - { - dataBinder.DataContext = dataContext; - dataBinders.Add(dataBinder); - } - } - - public static void UnBind(DataBinder dataBinder) - { - var dataContext = GetContext(dataBinder.Context); - if (dataBinder.IsDestination) UnBindDestination(dataContext, dataBinder); - if (dataBinder.IsSource) UnBindSource(dataContext, dataBinder); - } - - internal static void UnBindDestination(DataContext dataContext, DataBinder dataBinder) - { - var dataBinders = dataContext.GetDestinations(dataBinder.Key); - if (dataBinders.Contains(dataBinder)) - { - dataBinder.DataContext = null; - dataBinders.Remove(dataBinder); - } - } - - internal static void UnBindSource(DataContext dataContext, DataBinder dataBinder) - { - var dataBinders = dataContext.GetSources(dataBinder.Key); - if (dataBinders.Contains(dataBinder)) - { - dataBinder.DataContext = null; - dataBinders.Remove(dataBinder); - } - } - - #endregion - - #region Data - - public static T GetData(string contextKey, string key) - { - var dataContext = GetContext(contextKey); - var result = dataContext.GetData(key); - return result; - } - - public T GetData(string key) - { - var dataBinders = GetSources(key); - if (dataBinders.Count == 0) return default; - foreach (var dataBinder in dataBinders) - { - var sourceDataBinder = dataBinder as DataBinder; - if (sourceDataBinder == null) continue; - var result = sourceDataBinder.Value; - return result; - } - - var tryConvertData = (T)dataBinders[0].GetDataInternal(typeof(T)); - return tryConvertData; - } - - #endregion - } -} diff --git a/Runtime/Script/Core/TypeCaches.cs b/Runtime/Script/Core/TypeCaches.cs index 0b29d5e..b655971 100644 --- a/Runtime/Script/Core/TypeCaches.cs +++ b/Runtime/Script/Core/TypeCaches.cs @@ -80,8 +80,9 @@ public static Assembly[] Assemblies public static Assembly GetAssemblyByName(string assemblyName) { if (string.IsNullOrEmpty(assemblyName)) return null; - foreach (var assembly in Assemblies) + for (var i = 0; i < Assemblies.Length; i++) { + var assembly = Assemblies[i]; var temp = assembly.GetName().Name; if (temp == assemblyName) return assembly; } @@ -100,30 +101,27 @@ public static Type GetTypeByName(string assemblyName, string typeName) public static List GetAssemblyTypes(Assembly assembly) { - if (!AssemblyTypeDic.TryGetValue(assembly, out var result)) + if (AssemblyTypeDic.TryGetValue(assembly, out var result)) return result; + result = new List(); + var types = assembly.GetTypes(); + for (var i = 0; i < types.Length; i++) { - result = new List(); - var types = assembly.GetTypes(); - foreach (var type in types) - { - result.Add(type); - } - - AssemblyTypeDic.Add(assembly, result); + var type = types[i]; + result.Add(type); } + AssemblyTypeDic.Add(assembly, result); + return result; } public static List GetTypeProperties(Type type) { - if (!TypePropertyDic.TryGetValue(type, out var result)) - { - result = new List(); - var properties = type.GetProperties(DefaultBindingFlags); - result.AddRange(properties); - TypePropertyDic.Add(type, result); - } + if (TypePropertyDic.TryGetValue(type, out var result)) return result; + result = new List(); + var properties = type.GetProperties(DefaultBindingFlags); + result.AddRange(properties); + TypePropertyDic.Add(type, result); return result; } @@ -137,13 +135,11 @@ public static PropertyInfo GetTypePropertyByName(Type type, string propertyName) TypeNamePropertyDic.Add(type, propertyDic); } - if (!propertyDic.TryGetValue(propertyName, out var propertyInfo)) + if (propertyDic.TryGetValue(propertyName, out var propertyInfo)) return propertyInfo; + propertyInfo = type.GetProperty(propertyName, DefaultBindingFlags); + if (propertyInfo != null) { - propertyInfo = type.GetProperty(propertyName, DefaultBindingFlags); - if (propertyInfo != null) - { - propertyDic.Add(propertyName, propertyInfo); - } + propertyDic.Add(propertyName, propertyInfo); } return propertyInfo; @@ -151,13 +147,11 @@ public static PropertyInfo GetTypePropertyByName(Type type, string propertyName) public static List GetTypeFields(Type type) { - if (!TypeFieldDic.TryGetValue(type, out var result)) - { - result = new List(); - var fields = type.GetFields(DefaultBindingFlags); - result.AddRange(fields); - TypeFieldDic.Add(type, result); - } + if (TypeFieldDic.TryGetValue(type, out var result)) return result; + result = new List(); + var fields = type.GetFields(DefaultBindingFlags); + result.AddRange(fields); + TypeFieldDic.Add(type, result); return result; } @@ -171,13 +165,11 @@ public static FieldInfo GetTypeFieldByName(Type type, string fieldName) TypeNameFiledDic.Add(type, fieldDic); } - if (!fieldDic.TryGetValue(fieldName, out var fieldInfo)) + if (fieldDic.TryGetValue(fieldName, out var fieldInfo)) return fieldInfo; + fieldInfo = type.GetField(fieldName, DefaultBindingFlags); + if (fieldInfo != null) { - fieldInfo = type.GetField(fieldName, DefaultBindingFlags); - if (fieldInfo != null) - { - fieldDic.Add(fieldName, fieldInfo); - } + fieldDic.Add(fieldName, fieldInfo); } return fieldInfo; diff --git a/Runtime/Script/UBind.cs b/Runtime/Script/UBind.cs index eae2ad0..9578509 100644 --- a/Runtime/Script/UBind.cs +++ b/Runtime/Script/UBind.cs @@ -5,15 +5,43 @@ namespace Aya.DataBinding { public static class UBind { + #region Converter + + public static void RegisterConverter(Type sourceType, Type targetType, DataConverter dataConverter) + { + DataConverter.RegisterConverter(sourceType, targetType, dataConverter); + } + + public static DataConverter GetConverter(Type sourceType, Type targetType) + { + return DataConverter.GetConverter(sourceType, targetType); + } + + #endregion + + #region Bind Map + + public static void RegisterMap(object target) + { + BindMap.RegisterMap(target); + } + + public static void DeRegisterMap(object target) + { + BindMap.DeRegisterMap(target); + } + + #endregion + #region Bind Value - + public static RuntimeValueBinder BindSource(string key, Func getter) { return Bind(key, DataDirection.Source, getter, null); } - public static RuntimeValueBinder BindSource(string context, string key, Func getter) + public static RuntimeValueBinder BindSource(string container, string key, Func getter) { - return Bind(context, key, DataDirection.Source, getter, null); + return Bind(container, key, DataDirection.Source, getter, null); } public static RuntimeValueBinder BindTarget(string key, Action setter) @@ -21,28 +49,28 @@ public static RuntimeValueBinder BindTarget(string key, Action setter) return Bind(key, DataDirection.Target, null, setter); } - public static RuntimeValueBinder BindTarget(string context, string key, Action setter) + public static RuntimeValueBinder BindTarget(string container, string key, Action setter) { - return Bind(context, key, DataDirection.Target, null, setter); + return Bind(container, key, DataDirection.Target, null, setter); } public static RuntimeValueBinder BindBoth(string key, Func getter, Action setter) { return Bind(key, DataDirection.Both, getter, setter); } - public static RuntimeValueBinder BindBoth(string context, string key, Func getter, Action setter) + public static RuntimeValueBinder BindBoth(string container, string key, Func getter, Action setter) { - return Bind(context, key, DataDirection.Both, getter, setter); + return Bind(container, key, DataDirection.Both, getter, setter); } public static RuntimeValueBinder Bind(string key, DataDirection direction, Func getter, Action setter) { - return Bind(DataContext.Default, key, direction, getter, setter); ; + return Bind(DataContainer.Default, key, direction, getter, setter); ; } - public static RuntimeValueBinder Bind(string context, string key, DataDirection direction, Func getter, Action setter) + public static RuntimeValueBinder Bind(string container, string key, DataDirection direction, Func getter, Action setter) { - var dataBinder = new RuntimeValueBinder(context, key, direction, getter, setter); + var dataBinder = new RuntimeValueBinder(container, key, direction, getter, setter); dataBinder.Bind(); return dataBinder; } @@ -53,13 +81,13 @@ public static RuntimeValueBinder Bind(string context, string key, DataDire public static (RuntimeValueBinder, RuntimeValueBinder) Bind(string key, Func sourceGetter, Action targetSetter) { - return Bind(DataContext.Default, key, sourceGetter, targetSetter); + return Bind(DataContainer.Default, key, sourceGetter, targetSetter); } - public static (RuntimeValueBinder, RuntimeValueBinder) Bind(string context, string key, Func sourceGetter, Action targetSetter) + public static (RuntimeValueBinder, RuntimeValueBinder) Bind(string container, string key, Func sourceGetter, Action targetSetter) { - var sourceDataBinder = new RuntimeValueBinder(context, key, DataDirection.Source, sourceGetter, null); - var targetDataBinder = new RuntimeValueBinder(context, key, DataDirection.Target, null, targetSetter); + var sourceDataBinder = new RuntimeValueBinder(container, key, DataDirection.Source, sourceGetter, null); + var targetDataBinder = new RuntimeValueBinder(container, key, DataDirection.Target, null, targetSetter); sourceDataBinder.Bind(); targetDataBinder.Bind(); return (sourceDataBinder, targetDataBinder); @@ -74,9 +102,9 @@ public static RuntimeTypeBinder BindSource(string key, object target) return Bind(key, DataDirection.Source, target); } - public static RuntimeTypeBinder BindSource(string context, string key, object target) + public static RuntimeTypeBinder BindSource(string container, string key, object target) { - return Bind(context, key, DataDirection.Source, target); + return Bind(container, key, DataDirection.Source, target); } public static RuntimeTypeBinder BindTarget(string key, object target) @@ -84,19 +112,19 @@ public static RuntimeTypeBinder BindTarget(string key, object target) return Bind(key, DataDirection.Target, target); } - public static RuntimeTypeBinder BindTarget(string context, string key, object target) + public static RuntimeTypeBinder BindTarget(string container, string key, object target) { - return Bind(context, key, DataDirection.Target, target); + return Bind(container, key, DataDirection.Target, target); } public static RuntimeTypeBinder Bind(string key, DataDirection direction, object target) { - return Bind(DataContext.Default, key, direction, target); + return Bind(DataContainer.Default, key, direction, target); } - public static RuntimeTypeBinder Bind(string context, string key, DataDirection direction, object target) + public static RuntimeTypeBinder Bind(string container, string key, DataDirection direction, object target) { - var dataBinder = new RuntimeTypeBinder(context, key, direction, target); + var dataBinder = new RuntimeTypeBinder(container, key, direction, target); dataBinder.Bind(); return dataBinder; } @@ -110,9 +138,9 @@ public static RuntimePropertyBinder BindSource(string key, object target, string return Bind(key, DataDirection.Source, target, propertyName); } - public static RuntimePropertyBinder BindSource(string context, string key, object target, string propertyName) + public static RuntimePropertyBinder BindSource(string container, string key, object target, string propertyName) { - return Bind(context, key, DataDirection.Source, target, propertyName); + return Bind(container, key, DataDirection.Source, target, propertyName); } public static RuntimePropertyBinder BindTarget(string key, object target, string propertyName) @@ -120,9 +148,9 @@ public static RuntimePropertyBinder BindTarget(string key, object target, string return Bind(key, DataDirection.Target, target, propertyName); } - public static RuntimePropertyBinder BindTarget(string context, string key, object target, string propertyName) + public static RuntimePropertyBinder BindTarget(string container, string key, object target, string propertyName) { - return Bind(context, key, DataDirection.Target, target, propertyName); + return Bind(container, key, DataDirection.Target, target, propertyName); } public static RuntimePropertyBinder BindBoth(string key, object target, string propertyName) @@ -130,20 +158,20 @@ public static RuntimePropertyBinder BindBoth(string key, object target, string p return Bind(key, DataDirection.Both, target, propertyName); } - public static RuntimePropertyBinder BindBoth(string context, string key, object target, string propertyName) + public static RuntimePropertyBinder BindBoth(string container, string key, object target, string propertyName) { - return Bind(context, key, DataDirection.Both, target, propertyName); + return Bind(container, key, DataDirection.Both, target, propertyName); } public static RuntimePropertyBinder Bind(string key, DataDirection direction, object target, string propertyName) { - return Bind(DataContext.Default, key, direction, target, propertyName); + return Bind(DataContainer.Default, key, direction, target, propertyName); } - public static RuntimePropertyBinder Bind(string context, string key, DataDirection direction, object target, string propertyName) + public static RuntimePropertyBinder Bind(string container, string key, DataDirection direction, object target, string propertyName) { var (propertyInfo, fieldInfo) = TypeCaches.GetTypePropertyOrFieldByName(target.GetType(), propertyName); - return Bind(context, key, direction, target, propertyInfo, fieldInfo); + return Bind(container, key, direction, target, propertyInfo, fieldInfo); } #endregion @@ -155,9 +183,9 @@ public static RuntimePropertyBinder BindSource(string key, object target, Proper return Bind(key, DataDirection.Source, target, propertyInfo, null); } - public static RuntimePropertyBinder BindSource(string context, string key, object target, PropertyInfo propertyInfo) + public static RuntimePropertyBinder BindSource(string container, string key, object target, PropertyInfo propertyInfo) { - return Bind(context, key, DataDirection.Source, target, propertyInfo, null); + return Bind(container, key, DataDirection.Source, target, propertyInfo, null); } public static RuntimePropertyBinder BindSource(string key, object target, FieldInfo fieldInfo) @@ -165,9 +193,9 @@ public static RuntimePropertyBinder BindSource(string key, object target, FieldI return Bind(key, DataDirection.Source, target, null, fieldInfo); } - public static RuntimePropertyBinder BindSource(string context, string key, object target, FieldInfo fieldInfo) + public static RuntimePropertyBinder BindSource(string container, string key, object target, FieldInfo fieldInfo) { - return Bind(context, key, DataDirection.Source, target, null, fieldInfo); + return Bind(container, key, DataDirection.Source, target, null, fieldInfo); } public static RuntimePropertyBinder BindTarget(string key, object target, PropertyInfo propertyInfo) @@ -175,9 +203,9 @@ public static RuntimePropertyBinder BindTarget(string key, object target, Proper return Bind(key, DataDirection.Target, target, propertyInfo, null); } - public static RuntimePropertyBinder BindTarget(string context, string key, object target, PropertyInfo propertyInfo) + public static RuntimePropertyBinder BindTarget(string container, string key, object target, PropertyInfo propertyInfo) { - return Bind(context, key, DataDirection.Target, target, propertyInfo, null); + return Bind(container, key, DataDirection.Target, target, propertyInfo, null); } public static RuntimePropertyBinder BindTarget(string key, object target, FieldInfo fieldInfo) @@ -185,9 +213,9 @@ public static RuntimePropertyBinder BindTarget(string key, object target, FieldI return Bind(key, DataDirection.Target, target, null, fieldInfo); } - public static RuntimePropertyBinder BindTarget(string context, string key, object target, FieldInfo fieldInfo) + public static RuntimePropertyBinder BindTarget(string container, string key, object target, FieldInfo fieldInfo) { - return Bind(context, key, DataDirection.Target, target, null, fieldInfo); + return Bind(container, key, DataDirection.Target, target, null, fieldInfo); } public static RuntimePropertyBinder BindBoth(string key, object target, PropertyInfo propertyInfo) @@ -195,9 +223,9 @@ public static RuntimePropertyBinder BindBoth(string key, object target, Property return Bind(key, DataDirection.Both, target, propertyInfo, null); } - public static RuntimePropertyBinder BindBoth(string context, string key, object target, PropertyInfo propertyInfo) + public static RuntimePropertyBinder BindBoth(string container, string key, object target, PropertyInfo propertyInfo) { - return Bind(context, key, DataDirection.Both, target, propertyInfo, null); + return Bind(container, key, DataDirection.Both, target, propertyInfo, null); } public static RuntimePropertyBinder BindBoth(string key, object target, FieldInfo fieldInfo) @@ -205,19 +233,19 @@ public static RuntimePropertyBinder BindBoth(string key, object target, FieldInf return Bind(key, DataDirection.Both, target, null, fieldInfo); } - public static RuntimePropertyBinder BindBoth(string context, string key, object target, FieldInfo fieldInfo) + public static RuntimePropertyBinder BindBoth(string container, string key, object target, FieldInfo fieldInfo) { - return Bind(context, key, DataDirection.Both, target, null, fieldInfo); + return Bind(container, key, DataDirection.Both, target, null, fieldInfo); } public static RuntimePropertyBinder Bind(string key, DataDirection direction, object target, PropertyInfo propertyInfo, FieldInfo fieldInfo) { - return Bind(DataContext.Default, key, direction, target, propertyInfo, fieldInfo); + return Bind(DataContainer.Default, key, direction, target, propertyInfo, fieldInfo); } - public static RuntimePropertyBinder Bind(string context, string key, DataDirection direction, object target, PropertyInfo propertyInfo, FieldInfo fieldInfo) + public static RuntimePropertyBinder Bind(string container, string key, DataDirection direction, object target, PropertyInfo propertyInfo, FieldInfo fieldInfo) { - var dataBinder = new RuntimePropertyBinder(context, key, direction, target, propertyInfo, fieldInfo); + var dataBinder = new RuntimePropertyBinder(container, key, direction, target, propertyInfo, fieldInfo); dataBinder.Bind(); return dataBinder; } diff --git a/Samples.meta b/Samples.meta index 00d66d3..0a8a5ed 100644 --- a/Samples.meta +++ b/Samples.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 1ccd7b67e2e14bf4880fc0ff2d606151 +guid: 14093d199ecdd1743a861964a4127cca folderAsset: yes DefaultImporter: externalObjects: {} diff --git a/package.json b/package.json index d3300da..c7cfe84 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "com.ls9512.ubind", "displayName": "UBind", "description": "UBind is a value binding component for Unity, which is used to quickly realize the association binding between UI and logical data.", - "version": "0.9.1", + "version": "2024.2.26", "unity": "2019.4", "license": "MIT", "repository": { @@ -24,12 +24,12 @@ { "displayName": "Samples", "description": "Samples", - "path": "Samples" + "path": "Samples~" }, { "displayName": "Extension", "description": "TextMeshPro Support", - "path": "Extension" + "path": "Extension~" } ] } \ No newline at end of file