如何从 UnityEngine.UIElements.TextField & Button 获取注册的操作



我正在Unity中创建自动单元测试,并希望测试是否使用了方法TextField.RegisterValueChangedCallback(SomeMethod);使用特定的方法。我还想测试UIElement按钮是否有一个特定的方法注册到:Button.clicked += SomeMethod;

我的问题:有没有办法从这些文本字段和按钮访问所有注册的方法?或者也许是一个(不太混乱(的工作环境?

在我的项目中,我是using UnityEngine.UIElements;

我想测试的方法的简单版本

VisualTreeAsset root; // Assigned in some other method
public void TestedMethod(){
// Get the field from the VisualTreeAsset root
textField = root.Q<TextField>("FieldName");
// Register
// This is the line I want to be tested:
textField.RegisterValueChangedCallback(SomeMethod);
}

用于说明我想要测试的伪代码:

[Test]
public void SomeTest()
{
// Get references from wherever they may be...
Action SomeMethod = [...];
TextField textField = [...];
Button button = [...];
// Assertions
// This is the part I need help with:
Assert.IsTrue( textField.GetRegisteredValueChange().Contains(SomeMethod) );
Assert.IsTrue( button.clicked.Contains(SomeMethod) );
}

我浏览了Unity API,找不到任何有用的东西:https://docs.unity3d.com/ScriptReference/UIElements.TextField.htmlhttps://docs.unity3d.com/ScriptReference/UIElements.Button.html

您可以使用UnityEngine.EventSystemsunityengine.eventsystems

最新更新