[英]'Does not implement Interface Member' error
我是C#的新手,所以我仍然很习惯。
我已经将这段代码转到了要使用虚拟操纵杆控制角色的位置。 我正在测试操纵杆是响应还是现在,但是每次尝试播放场景时,都会出现3个CS0535
错误:
'VirtualJoystick'未实现接口成员UnityEngine.EventSystems.IPointerUpHandler.OnPointerUp(UnityEngine.EventSystems.PointerEventData)'
'VirtualJoystick'未实现接口成员UnityEngine.EventSystems.IDragHandler.OnDrag(UnityEngine.EventSystems.PointerEventData)'
'VirtualJoystick'未实现接口成员'UnityEngine.EventSystems.IPointerDownHandler.OnPointerDown(UnityEngine.EventSystems.PointerEventData)'
这是代码。 很短
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;
public class VirtualJoystick : MonoBehaviour, IDragHandler, IPointerUpHandler, IPointerDownHandler
{
private Image bgImg;
private Image joystickImg;
private Vector3 inputVector;
private void start()
{
bgImg = GetComponent<Image>();
joystickImg = transform.GetChild(0).GetComponent<Image>();
}
public virtual void onDrag(PointerEventData ped)
{
Vector2 pos;
if(RectTransformUtility.ScreenPointToLocalPointInRectangle(bgImg.rectTransform, ped.position, ped.pressEventCamera, out pos))
{
Debug.Log("Test");
}
}
public virtual void onPointerDown(PointerEventData ped)
{
onDrag(ped);
}
public virtual void onPointerUp(PointerEventData ped)
{
}
}
我希望你们能帮助我。
编辑:
我已经纠正了语法,这就是为什么场景不播放的原因。 但是OnDrag代码上的Debug.Log无法正常工作。 我收到此NullReferenceException:对象引用未设置为对象错误的实例。 同样,我只是按照视频中的内容进行操作,他的效果似乎还不错。
您已声明的方法virtual
,因为基类声明它们virtual
不已,你有一个声明它们override
。
所以:
public override void OnPointerUp(PointerEventData ped) { }
另请注意,使用C#时,您的命名应完全匹配,因此应使用OnPointerUp
而不是onPointerUp
。
更正您的拼写,即onPointerDown
> OnPointerDown
。 C#区分大小写。
当您的类从接口继承时,它应实现在此类接口上声明的所有方法和属性。
在您的情况下,您必须在类中声明编译错误消息中提及的方法,并尊重它们的参数类型和返回值, 所有方法均区分大小写 。
接口旨在确保从其继承的所有类都实现此类方法和属性。 因此,您可以在不了解类本身的情况下访问这些成员。
接口不声明方法/属性修饰符(私有,公共等)。 您可以根据需要指定它们。
idraghandler,ipointeruphandler,ipointerdownhandler工具按此顺序执行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.