Comments (3)
可以自定义一个【可绑定数据类型】,如下:
using HT.Framework;
using TMPro;
using UnityEngine.Events;
using UnityEngine.EventSystems;
/// <summary>
/// 专用于TMP组件的可绑定string类型
/// </summary>
public class BindableString_TMP : BindableType<string>
{
private UnityAction<string> _callback;
/// <summary>
/// 数据值
/// </summary>
public override string Value
{
get
{
return base.Value;
}
set
{
if (_value == value)
return;
_value = value;
_onValueChanged?.Invoke(_value);
}
}
public BindableString_TMP()
{
_callback = (v) => { Value = v; };
Value = null;
}
public BindableString_TMP(string value)
{
_callback = (v) => { Value = v; };
Value = value;
}
/// <summary>
/// 绑定控件
/// </summary>
/// <param name="control">绑定的目标控件</param>
protected override void Binding(UIBehaviour control)
{
base.Binding(control);
if (_bindedControls.Contains(control))
return;
if (control is TMP_InputField)
{
TMP_InputField inputField = control as TMP_InputField;
inputField.text = Value;
inputField.onValueChanged.AddListener(_callback);
_onValueChanged += (value) => { if (inputField) inputField.text = value; };
_bindedControls.Add(control);
}
else if (control is TMP_Text)
{
TMP_Text text = control as TMP_Text;
text.text = Value;
_onValueChanged += (value) => { if (text) text.text = value; };
_bindedControls.Add(control);
}
else
{
Log.Warning(string.Format("自动化任务:数据绑定失败,当前不支持控件 {0} 与 BindableString_TMP 类型的数据绑定!", control.GetType().FullName));
}
}
/// <summary>
/// 解除所有控件的绑定
/// </summary>
protected override void Unbind()
{
base.Unbind();
foreach (var control in _bindedControls)
{
if (control == null)
continue;
if (control is TMP_InputField)
{
TMP_InputField inputField = control as TMP_InputField;
inputField.onValueChanged.RemoveListener(_callback);
}
}
_onValueChanged = null;
_bindedControls.Clear();
}
}
from htframework.
然后绑定组件:
using HT.Framework;
using TMPro;
public class Test_TMP : HTBehaviour, IDataDriver<TestData>
{
/// <summary>
/// 公共数据
/// </summary>
public TestData Data { get; set; }
protected override bool IsAutomate => true;
/// <summary>
/// 绑定至TMP_Text组件
/// </summary>
[DataBinding(nameof(TestData.Name))] public TMP_Text T_Name;
/// <summary>
/// 绑定至TMP_InputField组件
/// </summary>
[DataBinding(nameof(TestData.Name))] public TMP_InputField IF_Name;
}
public class TestData
{
public BindableString_TMP Name = new BindableString_TMP("jack");
}
from htframework.
Cool~ 感谢答复
from htframework.
Related Issues (20)
- 你好,我想问下资源加载模块那边,为啥没做引用计数的功能? HOT 1
- 这跟Ellan的Gameframework有什么区别... HOT 1
- AsseterMaster模块在资源多的情况下会卡死 HOT 1
- 我在打ab包的时候勾选AppendHash,但是UI加载失败,因为ab包名加了hash值 HOT 3
- 我如果修改了打包方法,做了AB包加密,是否只能自己实现DefaultResourceHelper,无法实现加载进度条了? HOT 5
- 新建了一个MyDefaultResourceHelper,无法切换回DefaultResourceHelper了 HOT 2
- UI Entity 会重复创建 HOT 1
- DefaultResourceHelper 能否将单线加载 改为并行资源加载 HOT 7
- ProtocolChannelBase HOT 5
- UI管理器,数据驱动功能绑定TMP的组件,未同步数据的问题 HOT 4
- 整合资源打包支持 HOT 1
- gamecomponent超链接失效 HOT 1
- 是否可以将UI元素注入UIRegion? HOT 3
- 是否会支持 Addressables 或者 yooasset HOT 2
- Builder.cs 85-88有点问题 HOT 3
- Are we support iOS platform yet? HOT 4
- 缺少命名空间 HOT 2
- 加载AssetBundle好像有问题 HOT 2
- LoadSceneAsync add an AsyncOperation callback HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from htframework.