Unity共享不同的资源/脚本来使用VR。我试图开发一个简单的体验来提高我对Unity不同功能的了解,但我遇到了调用事件的麻烦。
在script MenuButton.cs中,您可以订阅OnButtonSelected事件,但我不知道如何订阅:
MenuButton.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");
}
}
}但是我有一个错误:"error CS0123: a method or delegate VRStandardAssets.Menu.GetDiscover.Teleport()' parameters do not match delegateSystem.Action(VRStandardAssets.Menu.MenuButton)‘CS0123“。
这是什么意思?我只是在寻找一种最简单的方式来命名事件...
我也尝试了委托方法,但它不太起作用…也许我不太理解事件系统的功能,Unity和一些清晰的解释是受欢迎的,即使我已经学习了一些教程:
https://unity3d.com/fr/learn/tutorials/topics/scripting/events-creating-simple-messaging-system
https://unity3d.com/fr/learn/tutorials/topics/scripting/events
发布于 2018-01-10 02:15:36
这个错误说明,当您订阅"OnButtonSelected“事件时,您的目标方法(在本例中为"Teleport")必须接受VRStandardAssets.Menu.MenuButton类型的参数。
这就是事件系统告诉监听器选择了哪个按钮的方式。
因此,您可以使用类似以下内容:
void Teleport(VRStandardAssets.Menu.MenuButton buttonPressed)
{
// if you care which button, access buttonPressed parameter here..
Debug.Log("Hello");
}(注意:为了良好的编程实践,我建议将其命名为"Teleport“以外的名称--使用类似于"HandleMenuButton”或"MenuButtonPressed“的名称可以使其意图清晰;然后在该方法中可以调用一个单独的"Teleport”函数。将来,如果您需要更改交互,如果您保持这种分离级别,那么更新代码将会更容易。)
https://stackoverflow.com/questions/48174057
复制相似问题