无法通过脚本添加OnManipulationEnded事件(MRTK)
解决MRTK OnManipulationEnded事件绑定的CS1593错误
问题出在OnManipulationEnded事件对应的委托要求必须接收一个ManipulationEventData类型的参数——哪怕你用不到这个参数,也得在绑定时声明它。你原来写的无参lambda表达式不符合委托的参数要求,所以触发了CS1593错误。
修改后的代码方案
方案1:不需要使用事件数据时
直接在lambda里接收参数,再调用你的无参new_position()函数:
mainobject.AddComponent<ObjectManipulator>(); mainobject.GetComponent<ObjectManipulator>().OnManipulationEnded.AddListener((eventData) => new_position());
方案2:需要用到操纵相关信息时
如果之后想获取操纵的目标物体、最终位置等信息,可以把事件数据传给自定义函数:
// 先修改new_position的定义,添加参数 void new_position(ManipulationEventData eventData) { // 示例:获取操纵后的物体位置 Vector3 finalPosition = eventData.transform.position; // 你的位置计算逻辑 } // 绑定事件时直接传入函数 mainobject.AddComponent<ObjectManipulator>(); mainobject.GetComponent<ObjectManipulator>().OnManipulationEnded.AddListener(new_position);
这样就能正常完成事件绑定,在物体操纵结束后触发你的位置计算逻辑。
内容的提问来源于stack exchange,提问作者MAYANK PANDE
相关产品推荐
相关产品推荐

