简体   繁体   English

C#UnityEvents-覆盖

[英]C# UnityEvents - Override

I had been searching so much to solve my problem but I found nothing. 我一直在努力寻找解决问题的方法,但没有发现任何问题。

So, what is my problem? 那我怎么了 I have a class that extends from UnityEvent and it has 2 virtual methods and it has public identifier in another file that should call those methods. 我有一个从UnityEvent扩展的类,它有2个虚拟方法,并且在另一个应调用这些方法的文件中具有公共标识符。 But when I put my script in inspector nothing happens (except root virtual methods). 但是,当我将脚本放入检查器时,什么也没发生(root虚拟方法除外)。

The class: 班级:

using UnityEngine;
using UnityEngine.Events;
using System.Collections;

namespace Namespace {
    [System.Serializable]
    public class SomeClass : UnityEvent {
        public virtual void OnSomething(GameObject objectSender, GameObject objectAction) {

        }
        public virtual void OnSomethingElse(GameObject objectSender, GameObject objectAction) {
        }
    }
}

I overrided those methods in a file and but in variable "script" shown below but they are not overrided. 我在文件中覆盖了这些方法,但是在下面显示的变量“ script”中覆盖了这些方法,但是它们没有被覆盖。 And using of SomeClass: 并使用SomeClass:

using UnityEngine;
using UnityEngine.Events;
using System.Collections;

namespace Namespace {
    public class ActionListener : MonoBehaviour {
        public SomeClass script;

        // When I lose last hope
        // private void Start() {
        //  script.Invoke();
        //}
        public void OnHover(GameObject sender, GameObject action) {
            script.OnHoverListener(sender, action);
        }
        public void OnClick(GameObject sender, GameObject action) {
            script.OnClickListener(sender, action);
        }
    }
}

I have solved this problem using MonoBehaviour instead UnityEvent. 我已经使用MonoBehaviour代替UnityEvent解决了这个问题。

And not it looks like this 而且看起来不是这样

using UnityEngine;
using UnityEngine.Events;
using System.Collections;

namespace Namespace {
    public class SomeClass : MonoBehaviour {
        public virtual void OnSomething(GameObject objectSender, GameObject objectAction) {

        }
        public virtual void OnSomethingElse(GameObject objectSender, GameObject objectAction) {
        }
    }
}

And in use 并在使用中

using UnityEngine;
using UnityEngine.Events;
using System.Collections;

namespace Namespace {
    public class ActionListener : MonoBehaviour {
        public SomeClass script;

        public void OnHover(GameObject sender, GameObject action) {
            script.OnSomething(sender, action);
        }
        public void OnClick(GameObject sender, GameObject action) {
            script.OnSomethingElse(sender, action);
        }
    }
}

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

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