【发布时间】:2018-01-10 02:15:36
【问题描述】:
Unity 共享不同的资产/脚本以使用 VR。我尝试开发一种简单的体验来提高我对不同 Unity 功能的了解,但我在调用事件时遇到了麻烦。
在脚本 MenuButton.cs 中,您可以订阅 OnButtonSelected 事件,但我不知道如何:
菜单按钮.cs
public class MenuButton : MonoBehaviour
{
public event Action<MenuButton> OnButtonSelected; // This event is triggered when the selection of the button has finished.
...
private IEnumerator ActivateButton()
{
// If anything is subscribed to the OnButtonSelected event, call it.
if (OnButtonSelected != null)
OnButtonSelected(this);
}
}
我尝试了多种不成功的方式从另一个脚本订阅此事件,例如:
namespace VRStandardAssets.Menu
{
public class GetDiscover : MonoBehaviour
{
[SerializeField] private MenuButton m_MenuButton; // This controls when the selection is complete.
void OnEnable()
{
m_MenuButton.OnButtonSelected += Teleport;
}
void OnDisable()
{
m_MenuButton.OnButtonSelected -= Teleport;
}
void Teleport()
{
Debug.Log("Hello");
}
}
}
但我有错误:“错误 CS0123:方法或委托VRStandardAssets.Menu.GetDiscover.Teleport()' parameters do not match delegateSystem.Action(VRStandardAssets.Menu.MenuButton)' 参数”。
这是什么意思?我只是在寻找调用事件的最简单方法...
我也尝试使用委托方法,但它也不起作用...... 也许我不太了解 Unity 事件系统的功能,即使我已经遵循了一些教程,也欢迎一些清晰的解释:
https://unity3d.com/fr/learn/tutorials/topics/scripting/events-creating-simple-messaging-system
https://unity3d.com/fr/learn/tutorials/topics/scripting/events
【问题讨论】: