简体   繁体   English

“未实现接口成员”错误

[英]'Does not implement Interface Member' error

I'm fairly new to C# so I'm still getting used to it. 我是C#的新手,所以我仍然很习惯。

I've had this code going where I want to control a character with a virtual joystick. 我已经将这段代码转到了要使用虚拟操纵杆控制角色的位置。 I was testing whether the joystick would be responsive or now, but every time I try to play the scene, I get 3 CS0535 errors saying: 我正在测试操纵杆是响应还是现在,但是每次尝试播放场景时,都会出现3个CS0535错误:

'VirtualJoystick' does not implement interface member UnityEngine.EventSystems.IPointerUpHandler.OnPointerUp(UnityEngine.EventSystems.PointerEventData)' 'VirtualJoystick'未实现接口成员UnityEngine.EventSystems.IPointerUpHandler.OnPointerUp(UnityEngine.EventSystems.PointerEventData)'

'VirtualJoystick' does not implement interface member UnityEngine.EventSystems.IDragHandler.OnDrag(UnityEngine.EventSystems.PointerEventData)' 'VirtualJoystick'未实现接口成员UnityEngine.EventSystems.IDragHandler.OnDrag(UnityEngine.EventSystems.PointerEventData)'

'VirtualJoystick' does not implement interface member `UnityEngine.EventSystems.IPointerDownHandler.OnPointerDown(UnityEngine.EventSystems.PointerEventData)' 'VirtualJoystick'未实现接口成员'UnityEngine.EventSystems.IPointerDownHandler.OnPointerDown(UnityEngine.EventSystems.PointerEventData)'

Here's the code. 这是代码。 It's fairly short. 很短

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)
{

}

}

I hope you guys could help me through this. 我希望你们能帮助我。

EDIT: 编辑:

I have corrected the syntax, which was cause of why the scene was not playing. 我已经纠正了语法,这就是为什么场景不播放的原因。 But the Debug.Log on OnDrag code is not working. 但是OnDrag代码上的Debug.Log无法正常工作。 I'm getting this NullReferenceException: Object reference not set to an instance of an object error. 我收到此NullReferenceException:对象引用未设置为对象错误的实例。 Again, I'm just following what the video says, and his seems to work just fine. 同样,我只是按照视频中的内容进行操作,他的效果似乎还不错。

You have declared the methods virtual , since the base class declares them virtual already, you have to declare them with override . 您已声明的方法virtual ,因为基类声明它们virtual不已,你有一个声明它们override

So: 所以:

public override void OnPointerUp(PointerEventData ped) { }

Also note that with C# your naming should match exact, so OnPointerUp instead of onPointerUp . 另请注意,使用C#时,您的命名应完全匹配,因此应使用OnPointerUp而不是onPointerUp

Correct your spelling, ie onPointerDown -> OnPointerDown . 更正您的拼写,即onPointerDown > OnPointerDown C# is case sensitive. C#区分大小写。

When your class inherit from interface(s), it should implement all methods and properties declared on such interface(s). 当您的类从接口继承时,它应实现在此类接口上声明的所有方法和属性。

In your case you must declare in your class the methods mentioned in the compiling error messages, respecting their parameters type and returning values, all case sensitive . 在您的情况下,您必须在类中声明编译错误消息中提及的方法,并尊重它们的参数类型和返回值, 所有方法均区分大小写

Interfaces are meant to make sure all classes that inherit from them implement such methods and properties. 接口旨在确保从其继承的所有类都实现此类方法和属性。 Therefore you could access those members without more knowledge about the class itself. 因此,您可以在不了解类本身的情况下访问这些成员。

Interfaces do not declare method/property modifiers (private, public, etc). 接口不声明方法/属性修饰符(私有,公共等)。 You can specify them as you see fit. 您可以根据需要指定它们。

idraghandler,ipointeruphandler,ipointerdownhandler工具按此顺序执行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM