Skip to content

Commit

Permalink
1. Simplify the implementation of custom extensions
Browse files Browse the repository at this point in the history
2. TMP extension and streamlining
  • Loading branch information
ls9512 committed Aug 19, 2021
1 parent 033e312 commit 56377fa
Show file tree
Hide file tree
Showing 26 changed files with 120 additions and 312 deletions.
14 changes: 4 additions & 10 deletions .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,14 +314,10 @@ Implement a runtime data binder for a specific object and property by inheriting
``` cs
public class RuntimeImageFillAmountBinder : DataBinder<Image, float>
{
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;
}
}
```
Expand Down Expand Up @@ -381,6 +377,4 @@ DataConverter.Register((sourceType, targetType), new CustomDataConverter());
***

## 8. <a name='Extension--TextMeshPro'></a>Extension -- TextMeshPro
* TMP Text Binder
* TMP Text UI Binder
* TMP Input Field Binder
* TMP Text Binder
14 changes: 4 additions & 10 deletions .github/README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,14 +315,10 @@ DataConvert.Register(Type sourceType, Type targetType, DataConverter dataConvert
``` cs
public class RuntimeImageFillAmountBinder : DataBinder<Image, float>
{
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;
}
}
```
Expand Down Expand Up @@ -382,6 +378,4 @@ DataConverter.Register((sourceType, targetType), new CustomDataConverter());
***

## 8. <a name='--TextMeshPro'></a>扩展 -- TextMeshPro
* TMP Text Binder
* TMP Text UI Binder
* TMP Input Field Binder
* TMP Text Binder
37 changes: 0 additions & 37 deletions Extension/TextMeshPro/Script/TMPInputFieldBinder.cs

This file was deleted.

11 changes: 0 additions & 11 deletions Extension/TextMeshPro/Script/TMPInputFieldBinder.cs.meta

This file was deleted.

16 changes: 6 additions & 10 deletions Extension/TextMeshPro/Script/TMPTextBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,26 @@
namespace Aya.DataBinding
{
[AddComponentMenu("Data Binding/TMP Text Binder")]
public class TMPTextBinder : ComponentBinder<TextMeshPro, string, RuntimeTMPTextBinder>
public class TMPTextBinder : ComponentBinder<TMP_Text, string, RuntimeTMPTextBinder>
{
public override bool NeedUpdate => true;
}

public class RuntimeTMPTextBinder : DataBinder<TextMeshPro, string>
public class RuntimeTMPTextBinder : DataBinder<TMP_Text, string>
{
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<TextMeshPro, string, RuntimeTMPTextBinder>
public class TMPTextBinderEditor : ComponentBinderEditor<TMP_Text, string, RuntimeTMPTextBinder>
{
}

Expand Down
37 changes: 0 additions & 37 deletions Extension/TextMeshPro/Script/TMPTextUIBinder.cs

This file was deleted.

11 changes: 0 additions & 11 deletions Extension/TextMeshPro/Script/TMPTextUIBinder.cs.meta

This file was deleted.

11 changes: 1 addition & 10 deletions Runtime/Script/Binder/RuntimeTypeBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
}
10 changes: 3 additions & 7 deletions Runtime/Script/Binder/RuntimeValueBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
98 changes: 50 additions & 48 deletions Runtime/Script/Component/PropertyBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,56 @@ public class RuntimePropertyBinder<TTarget> : DataBinder<TTarget, object>

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
Expand Down Expand Up @@ -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);
}
}
}
}
}
10 changes: 3 additions & 7 deletions Runtime/Script/Component/UI/CanvasGroupBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,10 @@ public class RuntimeCanvasGroupBinder : DataBinder<CanvasGroup, float>
{
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;
}
}

Expand Down
10 changes: 3 additions & 7 deletions Runtime/Script/Component/UI/ColorBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,10 @@ public class RuntimeColorBinder : DataBinder<Graphic, Color>
{
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;
}
}

Expand Down
10 changes: 3 additions & 7 deletions Runtime/Script/Component/UI/DropdownBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,10 @@ public class RuntimeDropdownBinder : DataBinder<Dropdown, int>
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;
}
}

Expand Down
Loading

0 comments on commit 56377fa

Please sign in to comment.