diff --git a/.github/README.md b/.github/README.md index 0cbc197..1cd8b56 100644 --- a/.github/README.md +++ b/.github/README.md @@ -314,14 +314,10 @@ Implement a runtime data binder for a specific object and property by inheriting ``` cs public class RuntimeImageFillAmountBinder : DataBinder { - public override void SetData(float data) + public override float Value { - Target.fillAmount = data; - } - - public override float GetData() - { - return Target.fillAmount; + get => Target.fillAmount; + set => Target.fillAmount = value; } } ``` @@ -381,6 +377,4 @@ DataConverter.Register((sourceType, targetType), new CustomDataConverter()); *** ## 8. Extension -- TextMeshPro -* TMP Text Binder -* TMP Text UI Binder -* TMP Input Field Binder \ No newline at end of file +* TMP Text Binder \ No newline at end of file diff --git a/.github/README_CN.md b/.github/README_CN.md index 6acec44..f2db78a 100644 --- a/.github/README_CN.md +++ b/.github/README_CN.md @@ -315,14 +315,10 @@ DataConvert.Register(Type sourceType, Type targetType, DataConverter dataConvert ``` cs public class RuntimeImageFillAmountBinder : DataBinder { - public override void SetData(float data) + public override float Value { - Target.fillAmount = data; - } - - public override float GetData() - { - return Target.fillAmount; + get => Target.fillAmount; + set => Target.fillAmount = value; } } ``` @@ -382,6 +378,4 @@ DataConverter.Register((sourceType, targetType), new CustomDataConverter()); *** ## 8. 扩展 -- TextMeshPro -* TMP Text Binder -* TMP Text UI Binder -* TMP Input Field Binder \ No newline at end of file +* TMP Text Binder \ No newline at end of file diff --git a/Extension/TextMeshPro/Script/TMPInputFieldBinder.cs b/Extension/TextMeshPro/Script/TMPInputFieldBinder.cs deleted file mode 100644 index 97bca23..0000000 --- a/Extension/TextMeshPro/Script/TMPInputFieldBinder.cs +++ /dev/null @@ -1,37 +0,0 @@ -#if UBIND_TEXTMESHPRO -using TMPro; -using UnityEngine; - -namespace Aya.DataBinding -{ - [AddComponentMenu("Data Binding/TMP Input Field Binder")] - public class TMPInputFieldBinder : ComponentBinder - { - } - - public class RuntimeTMPInputFieldBinder : DataBinder - { - public override void AddListener() => Target.onValueChanged.AddListener(OnValueChangedCallback); - public override void RemoveListener() => Target.onValueChanged.RemoveListener(OnValueChangedCallback); - - public override void SetData(string data) - { - Target.text = data; - } - - public override string GetData() - { - return Target.text; - } - } - -#if UNITY_EDITOR - - [UnityEditor.CustomEditor(typeof(TMPInputFieldBinder)), UnityEditor.CanEditMultipleObjects] - public class TMPInputFieldBinderEditor : ComponentBinderEditor - { - } - -#endif -} -#endif \ No newline at end of file diff --git a/Extension/TextMeshPro/Script/TMPInputFieldBinder.cs.meta b/Extension/TextMeshPro/Script/TMPInputFieldBinder.cs.meta deleted file mode 100644 index ddbf8dc..0000000 --- a/Extension/TextMeshPro/Script/TMPInputFieldBinder.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 1c7e63201aa6fea4f80a800a9f3c153b -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {fileID: 2800000, guid: b07aafae171734f4b837582f4db8e75d, type: 3} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Extension/TextMeshPro/Script/TMPTextBinder.cs b/Extension/TextMeshPro/Script/TMPTextBinder.cs index 199f0ef..5b292a1 100644 --- a/Extension/TextMeshPro/Script/TMPTextBinder.cs +++ b/Extension/TextMeshPro/Script/TMPTextBinder.cs @@ -5,30 +5,26 @@ namespace Aya.DataBinding { [AddComponentMenu("Data Binding/TMP Text Binder")] - public class TMPTextBinder : ComponentBinder + public class TMPTextBinder : ComponentBinder { public override bool NeedUpdate => true; } - public class RuntimeTMPTextBinder : DataBinder + public class RuntimeTMPTextBinder : DataBinder { public override bool NeedUpdate => true; - public override void SetData(string data) + public override string Value { - Target.text = data; - } - - public override string GetData() - { - return Target.text; + get => Target.text; + set => Target.text = value; } } #if UNITY_EDITOR [UnityEditor.CustomEditor(typeof(TMPTextBinder)), UnityEditor.CanEditMultipleObjects] - public class TMPTextBinderEditor : ComponentBinderEditor + public class TMPTextBinderEditor : ComponentBinderEditor { } diff --git a/Extension/TextMeshPro/Script/TMPTextUIBinder.cs b/Extension/TextMeshPro/Script/TMPTextUIBinder.cs deleted file mode 100644 index c135c3e..0000000 --- a/Extension/TextMeshPro/Script/TMPTextUIBinder.cs +++ /dev/null @@ -1,37 +0,0 @@ -#if UBIND_TEXTMESHPRO -using TMPro; -using UnityEngine; - -namespace Aya.DataBinding -{ - [AddComponentMenu("Data Binding/TMP Text Binder (UI)")] - public class TMPTextUIBinder : ComponentBinder - { - public override bool NeedUpdate => true; - } - - public class RuntimeTMPTextUIBinder : DataBinder - { - public override bool NeedUpdate => true; - - public override void SetData(string data) - { - Target.text = data; - } - - public override string GetData() - { - return Target.text; - } - } - -#if UNITY_EDITOR - - [UnityEditor.CustomEditor(typeof(TMPTextUIBinder)), UnityEditor.CanEditMultipleObjects] - public class TMPTextUIBinderEditor : ComponentBinderEditor - { - } - -#endif -} -#endif \ No newline at end of file diff --git a/Extension/TextMeshPro/Script/TMPTextUIBinder.cs.meta b/Extension/TextMeshPro/Script/TMPTextUIBinder.cs.meta deleted file mode 100644 index eae7fcb..0000000 --- a/Extension/TextMeshPro/Script/TMPTextUIBinder.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 9a9c9a0d0a14e1b42b8378b828b81a5c -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {fileID: 2800000, guid: b07aafae171734f4b837582f4db8e75d, type: 3} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Runtime/Script/Binder/RuntimeTypeBinder.cs b/Runtime/Script/Binder/RuntimeTypeBinder.cs index abe79ff..f60751b 100644 --- a/Runtime/Script/Binder/RuntimeTypeBinder.cs +++ b/Runtime/Script/Binder/RuntimeTypeBinder.cs @@ -54,15 +54,6 @@ public override void UnBind() } } - public override void SetData(object data) - { - // Not use - } - - public override object GetData() - { - // Not use - return null; - } + public override object Value { get; set; } } } diff --git a/Runtime/Script/Binder/RuntimeValueBinder.cs b/Runtime/Script/Binder/RuntimeValueBinder.cs index b1b6cf7..4b6ea81 100644 --- a/Runtime/Script/Binder/RuntimeValueBinder.cs +++ b/Runtime/Script/Binder/RuntimeValueBinder.cs @@ -26,14 +26,10 @@ public RuntimeValueBinder(string context, string key, DataDirection direction, F Setter = setter; } - public override void SetData(T data) + public override T Value { - Setter(data); - } - - public override T GetData() - { - return Getter(); + get => Getter(); + set => Setter(value); } } } diff --git a/Runtime/Script/Component/PropertyBinder.cs b/Runtime/Script/Component/PropertyBinder.cs index c00d2fa..5ed6369 100644 --- a/Runtime/Script/Component/PropertyBinder.cs +++ b/Runtime/Script/Component/PropertyBinder.cs @@ -39,6 +39,56 @@ public class RuntimePropertyBinder : DataBinder public override bool NeedUpdate => true; + public override object Value + { + get + { + if (FiledInfo != null) + { + var data = FiledInfo.GetValue(Target); + return data; + } + + if (PropertyInfo != null) + { + var data = PropertyInfo.GetValue(Target, null); + return data; + } + + return default; + } + set + { + var data = value; + var dataType = data != null ? data.GetType() : typeof(object); + if (FiledInfo != null) + { + if (data != null && dataType != FiledInfo.FieldType) + { + var convertData = Convert.ChangeType(data, FiledInfo.FieldType, CultureInfo.InvariantCulture); + FiledInfo.SetValue(Target, convertData); + } + else + { + FiledInfo.SetValue(Target, data); + } + } + + if (PropertyInfo != null) + { + if (data != null && dataType != PropertyInfo.PropertyType) + { + var convertData = Convert.ChangeType(data, PropertyInfo.PropertyType, CultureInfo.InvariantCulture); + PropertyInfo.SetValue(Target, convertData, null); + } + else + { + PropertyInfo.SetValue(Target, data, null); + } + } + } + } + public FieldInfo FiledInfo { get @@ -74,53 +124,5 @@ public PropertyInfo PropertyInfo } private PropertyInfo _propertyInfo; - - public override object GetData() - { - if (FiledInfo != null) - { - var data = FiledInfo.GetValue(Target); - return data; - } - - if (PropertyInfo != null) - { - var data = PropertyInfo.GetValue(Target, null); - return data; - } - - return default; - } - - - public override void SetData(object data) - { - var dataType = data != null ? data.GetType() : typeof(object); - if (FiledInfo != null) - { - if (data != null && dataType != FiledInfo.FieldType) - { - var convertData = Convert.ChangeType(data, FiledInfo.FieldType, CultureInfo.InvariantCulture); - FiledInfo.SetValue(Target, convertData); - } - else - { - FiledInfo.SetValue(Target, data); - } - } - - if (PropertyInfo != null) - { - if (data != null && dataType != PropertyInfo.PropertyType) - { - var convertData = Convert.ChangeType(data, PropertyInfo.PropertyType, CultureInfo.InvariantCulture); - PropertyInfo.SetValue(Target, convertData, null); - } - else - { - PropertyInfo.SetValue(Target, data, null); - } - } - } } } diff --git a/Runtime/Script/Component/UI/CanvasGroupBinder.cs b/Runtime/Script/Component/UI/CanvasGroupBinder.cs index f7f37b6..21ff204 100644 --- a/Runtime/Script/Component/UI/CanvasGroupBinder.cs +++ b/Runtime/Script/Component/UI/CanvasGroupBinder.cs @@ -12,14 +12,10 @@ public class RuntimeCanvasGroupBinder : DataBinder { public override bool NeedUpdate => true; - public override void SetData(float data) + public override float Value { - Target.alpha = data; - } - - public override float GetData() - { - return Target.alpha; + get => Target.alpha; + set => Target.alpha = value; } } diff --git a/Runtime/Script/Component/UI/ColorBinder.cs b/Runtime/Script/Component/UI/ColorBinder.cs index 0687160..3e78936 100644 --- a/Runtime/Script/Component/UI/ColorBinder.cs +++ b/Runtime/Script/Component/UI/ColorBinder.cs @@ -13,14 +13,10 @@ public class RuntimeColorBinder : DataBinder { public override bool NeedUpdate => true; - public override void SetData(Color data) + public override Color Value { - Target.color = data; - } - - public override Color GetData() - { - return Target.color; + get => Target.color; + set => Target.color = value; } } diff --git a/Runtime/Script/Component/UI/DropdownBinder.cs b/Runtime/Script/Component/UI/DropdownBinder.cs index 58a213a..799b630 100644 --- a/Runtime/Script/Component/UI/DropdownBinder.cs +++ b/Runtime/Script/Component/UI/DropdownBinder.cs @@ -14,14 +14,10 @@ public class RuntimeDropdownBinder : DataBinder public override void AddListener() => Target.onValueChanged.AddListener(OnValueChangedCallback); public override void RemoveListener() => Target.onValueChanged.RemoveListener(OnValueChangedCallback); - public override void SetData(int data) + public override int Value { - Target.value = data; - } - - public override int GetData() - { - return Target.value; + get => Target.value; + set => Target.value = value; } } diff --git a/Runtime/Script/Component/UI/DropdownListBinder.cs b/Runtime/Script/Component/UI/DropdownListBinder.cs index 1259ef3..74d138e 100644 --- a/Runtime/Script/Component/UI/DropdownListBinder.cs +++ b/Runtime/Script/Component/UI/DropdownListBinder.cs @@ -14,14 +14,10 @@ public class RuntimeDropdownListBinder : DataBinderList true; - public override void SetData(List data) + public override List Value { - Target.options = data; - } - - public override List GetData() - { - return Target.options; + get => Target.options; + set => Target.options = value; } } diff --git a/Runtime/Script/Component/UI/ImageBinder.cs b/Runtime/Script/Component/UI/ImageBinder.cs index 975f710..6fce4b7 100644 --- a/Runtime/Script/Component/UI/ImageBinder.cs +++ b/Runtime/Script/Component/UI/ImageBinder.cs @@ -13,14 +13,10 @@ public class RuntimeImageBinder : DataBinder { public override bool NeedUpdate => true; - public override void SetData(Sprite data) + public override Sprite Value { - Target.sprite = data; - } - - public override Sprite GetData() - { - return Target.sprite; + get => Target.sprite; + set => Target.sprite = value; } } diff --git a/Runtime/Script/Component/UI/ImageFillAmountBinder.cs b/Runtime/Script/Component/UI/ImageFillAmountBinder.cs index 8136514..951f960 100644 --- a/Runtime/Script/Component/UI/ImageFillAmountBinder.cs +++ b/Runtime/Script/Component/UI/ImageFillAmountBinder.cs @@ -13,14 +13,10 @@ public class RuntimeImageFillAmountBinder : DataBinder { public override bool NeedUpdate => true; - public override void SetData(float data) + public override float Value { - Target.fillAmount = data; - } - - public override float GetData() - { - return Target.fillAmount; + get => Target.fillAmount; + set => Target.fillAmount = value; } } diff --git a/Runtime/Script/Component/UI/InputFieldBinder.cs b/Runtime/Script/Component/UI/InputFieldBinder.cs index 3369297..7588f20 100644 --- a/Runtime/Script/Component/UI/InputFieldBinder.cs +++ b/Runtime/Script/Component/UI/InputFieldBinder.cs @@ -14,14 +14,10 @@ public class RuntimeInputDataBinder : DataBinder public override void AddListener() => Target.onValueChanged.AddListener(OnValueChangedCallback); public override void RemoveListener() => Target.onValueChanged.RemoveListener(OnValueChangedCallback); - public override void SetData(string data) + public override string Value { - Target.text = data; - } - - public override string GetData() - { - return Target.text; + get => Target.text; + set => Target.text = value; } } diff --git a/Runtime/Script/Component/UI/RawImageBinder.cs b/Runtime/Script/Component/UI/RawImageBinder.cs index c01caaa..857c76e 100644 --- a/Runtime/Script/Component/UI/RawImageBinder.cs +++ b/Runtime/Script/Component/UI/RawImageBinder.cs @@ -13,14 +13,10 @@ public class RuntimeRawImageBinder : DataBinder { public override bool NeedUpdate => true; - public override void SetData(Texture data) + public override Texture Value { - Target.texture = data; - } - - public override Texture GetData() - { - return Target.texture; + get => Target.texture; + set => Target.texture = value; } } diff --git a/Runtime/Script/Component/UI/ScrollbarBinder.cs b/Runtime/Script/Component/UI/ScrollbarBinder.cs index 999ff61..e949e5b 100644 --- a/Runtime/Script/Component/UI/ScrollbarBinder.cs +++ b/Runtime/Script/Component/UI/ScrollbarBinder.cs @@ -14,14 +14,10 @@ public class RuntimeScrollbarBinder : DataBinder public override void AddListener() => Target.onValueChanged.AddListener(OnValueChangedCallback); public override void RemoveListener() => Target.onValueChanged.RemoveListener(OnValueChangedCallback); - public override void SetData(float data) + public override float Value { - Target.value = data; - } - - public override float GetData() - { - return Target.value; + get => Target.value; + set => Target.value = value; } } diff --git a/Runtime/Script/Component/UI/SliderBinder.cs b/Runtime/Script/Component/UI/SliderBinder.cs index e0d8275..7906fa0 100644 --- a/Runtime/Script/Component/UI/SliderBinder.cs +++ b/Runtime/Script/Component/UI/SliderBinder.cs @@ -14,14 +14,10 @@ public class RuntimeSliderBinder : DataBinder public override void AddListener() => Target.onValueChanged.AddListener(OnValueChangedCallback); public override void RemoveListener() => Target.onValueChanged.RemoveListener(OnValueChangedCallback); - public override void SetData(float data) + public override float Value { - Target.value = data; - } - - public override float GetData() - { - return Target.value; + get => Target.value; + set => Target.value = value; } } diff --git a/Runtime/Script/Component/UI/TextBinder.cs b/Runtime/Script/Component/UI/TextBinder.cs index 78ae0f9..927ae3c 100644 --- a/Runtime/Script/Component/UI/TextBinder.cs +++ b/Runtime/Script/Component/UI/TextBinder.cs @@ -13,14 +13,10 @@ public class RuntimeTextBinder : DataBinder { public override bool NeedUpdate => true; - public override void SetData(string data) + public override string Value { - Target.text = data; - } - - public override string GetData() - { - return Target.text; + get => Target.text; + set => Target.text = value; } } diff --git a/Runtime/Script/Component/UI/TextFontSizeBinder.cs b/Runtime/Script/Component/UI/TextFontSizeBinder.cs index b6b88d7..35c1939 100644 --- a/Runtime/Script/Component/UI/TextFontSizeBinder.cs +++ b/Runtime/Script/Component/UI/TextFontSizeBinder.cs @@ -13,14 +13,10 @@ public class RuntimeTextFontSizeBinder : DataBinder { public override bool NeedUpdate => true; - public override void SetData(int data) + public override int Value { - Target.fontSize = data; - } - - public override int GetData() - { - return Target.fontSize; + get => Target.fontSize; + set => Target.fontSize = value; } } diff --git a/Runtime/Script/Component/UI/ToggleBinder.cs b/Runtime/Script/Component/UI/ToggleBinder.cs index a1f7ad1..a442c2e 100644 --- a/Runtime/Script/Component/UI/ToggleBinder.cs +++ b/Runtime/Script/Component/UI/ToggleBinder.cs @@ -14,14 +14,10 @@ public class ToggleDataBinder : DataBinder public override void AddListener() => Target.onValueChanged.AddListener(OnValueChangedCallback); public override void RemoveListener() => Target.onValueChanged.RemoveListener(OnValueChangedCallback); - public override void SetData(bool data) + public override bool Value { - Target.isOn = data; - } - - public override bool GetData() - { - return Target.isOn; + get => Target.isOn; + set => Target.isOn = value; } } diff --git a/Runtime/Script/Core/DataBinder(T).cs b/Runtime/Script/Core/DataBinder(T).cs index 05524ba..297c2bf 100644 --- a/Runtime/Script/Core/DataBinder(T).cs +++ b/Runtime/Script/Core/DataBinder(T).cs @@ -43,9 +43,7 @@ public override Type DataType #endregion - public abstract void SetData(T data); - - public abstract T GetData(); + public abstract T Value { get; set; } public virtual void OnValueChangedCallback(T data) { @@ -56,7 +54,7 @@ public override void Broadcast() { if (!IsSource) return; var dataBinders = DataContext.GetDestinations(Key); - var data = GetData(); + var data = Value; PreviousData = data; for (var i = 0; i < dataBinders.Count; i++) { @@ -68,7 +66,7 @@ public override void Broadcast() public override void UpdateSource() { if (!IsSource) return; - var currentData = GetData(); + var currentData = Value; if (CheckEquals(currentData, PreviousData)) return; Broadcast(); OnValueChanged?.Invoke(currentData); @@ -78,7 +76,7 @@ public override void UpdateTarget() { if (!IsDestination) return; var latestData = DataContext.GetData(Context, Key); - SetData(latestData); + Value = latestData; } public virtual bool CheckEquals(T data1, T data2) diff --git a/Runtime/Script/Core/DataBinder.cs b/Runtime/Script/Core/DataBinder.cs index 0ea9c07..77c6965 100644 --- a/Runtime/Script/Core/DataBinder.cs +++ b/Runtime/Script/Core/DataBinder.cs @@ -33,35 +33,20 @@ public abstract class DataBinder public virtual Type BinderType { get; internal set; } - public virtual MethodInfo GetMethod + public virtual PropertyInfo ValuePropertyInfo { get { - if (_getMethod == null) + if (_valuePropertyInfo == null) { - _getMethod = BinderType.GetMethod("GetData"); + _valuePropertyInfo = BinderType.GetProperty("Value"); } - return _getMethod; + return _valuePropertyInfo; } } - private MethodInfo _getMethod; - - public virtual MethodInfo SetMethod - { - get - { - if (_setMethod == null) - { - _setMethod = BinderType.GetMethod("SetData"); - } - - return _setMethod; - } - } - - private MethodInfo _setMethod; + private PropertyInfo _valuePropertyInfo; #endregion @@ -123,18 +108,18 @@ internal virtual void SetDataInternal(object data) { if (data == null) { - SetMethod.Invoke(this, null); + ValuePropertyInfo.SetValue(this, null); return; } var converter = DataConverter.GetConverter(data.GetType(), DataType); var convertData = converter.To(data, DataType); - SetMethod.Invoke(this, new object[] { convertData }); + ValuePropertyInfo.SetValue(this, convertData); } internal virtual object GetDataInternal(Type convertType) { - var data = GetMethod.Invoke(this, null); + var data = ValuePropertyInfo.GetValue(this); if (data == null) return default; var converter = DataConverter.GetConverter(DataType, convertType); diff --git a/Runtime/Script/Core/DataContext.cs b/Runtime/Script/Core/DataContext.cs index 54ebd7a..9132faa 100644 --- a/Runtime/Script/Core/DataContext.cs +++ b/Runtime/Script/Core/DataContext.cs @@ -133,7 +133,7 @@ public T GetData(string key) { var sourceDataBinder = dataBinder as DataBinder; if (sourceDataBinder == null) continue; - var result = sourceDataBinder.GetData(); + var result = sourceDataBinder.Value; return result; }