Giter Site home page Giter Site logo

Comments (3)

SaiTingHu avatar SaiTingHu commented on September 17, 2024

可以自定义一个【可绑定数据类型】,如下:

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.

SaiTingHu avatar SaiTingHu commented on September 17, 2024

然后绑定组件:

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.

nilan256 avatar nilan256 commented on September 17, 2024

Cool~ 感谢答复

from htframework.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.