简体   繁体   English

在运行时创建的哪个切换已在Unity中调用?

[英]Which Toggle created at runtime has been called in Unity?

I need to create a set of Toggle at run-time and set a listener when one of those change. 我需要在运行时创建一组Toggle ,并在其中一个发生更改时设置一个侦听器。 So this implies to know who changed. 因此,这意味着知道谁更改了。

I see how to do it statically in the UI but not programmatically and it's hard to find documentation on this subject. 我看到了如何在用户界面中静态地而不是通过编程方式来做到这一点,并且很难找到有关该主题的文档。

The Toggle.onValueChanged event is used to subscribe to the Toggle event to detect when it is toggled. Toggle.onValueChanged事件用于预订Toggle事件,以检测何时Toggle事件。 Toggle.toggle.isOn is used to check the status of the toggle. Toggle.toggle.isOn用于检查切换的状态。 Use AddListener and delegate to help register the toggle so that you will get the name of the toggle when it is toggled: 使用AddListenerdelegate来帮助注册切换,以便在切换时获得切换的名称:

public Toggle toggle1;

void OnEnable()
{
    //Register Toggle Events
    toggle1.onValueChanged.AddListener(delegate
    {
        ToggleCallValueChanged(toggle1);
    });
}

private void ToggleCallValueChanged(Toggle toggle)
{
    Debug.Log("Toggle: " + toggle + " is " + toggle.isOn);
}

void OnDisable()
{
    //Un-Register Toggle Events
    toggle1.onValueChanged.RemoveAllListeners();
}

If using multiple toggles, you can also re-use that one function but use the if statement to determine which one is toggled. 如果使用多个切换,则还可以重用该功能,但可以使用if语句确定切换哪个功能。 You do this if the toggles are related. 如果切换相关,则执行此操作。 This is better than creating a new callback function for each toggle control. 这比为每个切换控件创建一个新的回调函数要好。

Example of 3 toggles detected with one function: 使用一个功能检测到的3个切换的示例:

public Toggle toggle1;
public Toggle toggle2;
public Toggle toggle3;

void OnEnable()
{
    //Register Toggle Events
    toggle1.onValueChanged.AddListener(delegate { ToggleCallBack(toggle1); });
    toggle2.onValueChanged.AddListener(delegate { ToggleCallBack(toggle2); });
    toggle3.onValueChanged.AddListener(delegate { ToggleCallBack(toggle3); });
}

private void ToggleCallBack(Toggle toggle)
{
    if (toggle == toggle1)
    {
        //Your code for Toggle 1
        Debug.Log("Toggled: " + toggle1.name);
    }

    if (toggle == toggle2)
    {
        //Your code for Toggle 2
        Debug.Log("Toggled: " + toggle2.name);
    }

    if (toggle == toggle3)
    {
        //Your code for Toggle 3
        Debug.Log("Toggled: " + toggle3.name);
    }
}

void OnDisable()
{
    //Un-Register Toggle Events
    toggle1.onValueChanged.RemoveAllListeners();
    toggle2.onValueChanged.RemoveAllListeners();
    toggle3.onValueChanged.RemoveAllListeners();
}

You can use what is called an Observer pattern . 您可以使用所谓的观察者模式 That's many systems of events work. 这就是许多事件系统的工作原理。

Basically, each of your toggles created is able to send some event "I was toggled" (with giving some ID, name, value, etc.... to some object that has subscribed to it. 基本上,您创建的每个切换器都可以发送一些事件“我被切换”(给一些已订阅它的对象提供一些ID,名称,值等...)。

It also has a function "subscribe" or "attach", that allows any object to subscribe to it at runtime. 它还具有“订阅”或“附加”功能,该功能允许任何对象在运行时对其进行订阅。

When you subscribe, you provide a callback function (which what could be called and event handler , if you have already heard the term). 订阅时,您将提供一个回调函数(如果您已经听说过该术语,则可以称为和事件处理程序 )。

That function is what will happen when the toggled is used : it will simply look at the list of object that has subscribe to it, and call the callback on each of them. 该函数将在使用切换时发生:它将简单地查看已订阅该对象的对象列表,并在每个对象上调用回调。 That's the way with which the toggle will communicate the event. 这就是切换器传达事件的方式。

Basically, in your case, you probably will have simple one controller which will subscribe to the toggle. 基本上,在您的情况下,您可能将有一个简单的控制器来订阅该切换。 This controller could be (but not necessarily) the class that created the toggles at the first place. 该控制器可以(但不一定)是最初创建切换的类。

The whole pattern is useful because there is absolutely no need for the toggles to be aware of each other or of the controller in their code, (low coupling). 整个模式很有用,因为绝对不需要切换器彼此或在其代码中知道控制器(低耦合)。 And you never have to constantly check if something has changed, the "information" is properly sent only when relevant. 而且您永远不必不断检查是否有任何更改,仅在相关时才正确发送“信息”。

See Programmers's answer for a possible implementation. 请参阅程序员的答案以获取可能的实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM