unity3D 关于重新载入场景后Easytouch不能使用的问题

来源:互联网 发布:十本法术工厂升级数据 编辑:程序博客网 时间:2024/06/11 08:10

关于重新载入场景后Easytouch不能使用的问题


使用easytouch的手柄(Joystick)来控制主角移动。当第一次进入场景时,手柄是能够正常控制主角移动的,但是当再次进入相同的场景时,或者载入其他需要使用手柄来控制主角时,会发生一个错误。

MissingReferenceException: The object of type 'MoveController' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.

这种错误的解决办法是在move.cs脚本中,添加以下代码:

void OnDestroy(){EasyJoystick.On_JoystickMove -= OnJoystickMove;EasyJoystick.On_JoystickMoveEnd -= OnJoystickMoveEnd;}


注意:1. move.cs是放在主角上面的。

    2.  -=是解除掉手柄与主角的关系,使得它在下一个场景可以重新绑定其他其他的物体。

    3. OnJoystickMove和OnJoystickMoveEnd是函数的名字

    4.destroy不要拼写错误。


原理:easytouch的调用方法顺序为:

程序启动执行OnEnable();

触碰到手柄的时候执行OnJoystickMove();

离开手柄时执行OnJoystickMoveEnd();

程序执行结束时执行OnDisable()和OnDestroy();


在程序结束前,用OnDestroy()方法,解除掉手柄与主角的关系。



原创粉丝点击