【问题标题】:Unity/C# Trouble with virtual and overrideUnity/C# 虚拟和覆盖问题
【发布时间】:2015-12-31 22:56:58
【问题描述】:

所以我正在尝试创建自己的方法(如更新、启动等) 基本上,可以检测输入但使用 bool 方法的东西。更像是“OnKeyDown”。

我没有收到任何错误,但什么也没发生,这是我的课程:

 using UnityEngine;
 using System.Collections;

 public class BeastScript : MonoBehaviour
 {



     void Update()
     {
         #region INPUT
         //KEYS
         if (Input.anyKeyDown)
         {
             OnKeyDown(Input.inputString);
         }

         if (Input.anyKey)
         {
             OnKeyHold(Input.inputString);
         }
         //MOUSE
         if (Input.GetMouseButtonDown(0))
         {
             OnMouseClick(0);
         }

         if (Input.GetMouseButtonDown(1))
         {
             OnMouseClick(1);
         }

         if (Input.GetMouseButtonDown(2))
         {
             OnMouseClick(2);
         }

         if (Input.GetMouseButton(0))
         {
             OnMouseClickHold(0);
         }

         if (Input.GetMouseButton(1))
         {
             OnMouseClickHold(1);
         }

         if (Input.GetMouseButton(2))
         {
             OnMouseClickHold(2);
         }

         if (Input.GetMouseButtonUp(0))
         {
             OnMouseRelease(0);
         }

         if (Input.GetMouseButtonUp(1))
         {
             OnMouseRelease(1);
         }

         if (Input.GetMouseButtonUp(2))
         {
             OnMouseRelease(2);
         }
         #endregion INPUT



     }


     public virtual void OnKeyDown(string key) { }
     public virtual void OnKeyHold(string key) { }
     public virtual void OnMouseClick(int button) { }
     public virtual void OnMouseClickHold(int button) { }
     public virtual void OnMouseRelease(int button) { }

 }

这是派生的

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 using Beast;
 using Beast.BUI;
 using Beast.Extensions;
 using System;
 using UnityEngine.Events;

 using Beast.Web;

 public class Tester : BeastScript {

     void Start()
     {

     }

     void OnGUI ()
     {

     }

     void Update ()
     {

     }

     public override void OnMouseClick(int button)
     {
         base.OnMouseClick(button);

         if (button == 0)
         {
             print("Works");
         }
     }
 }

【问题讨论】:

  • 如果任何答案对您有用,如果您接受该答案,对其他人会有所帮助。如果没有,请随时评论给出的答案,或更新您的问题。

标签: c# unity3d overriding virtual


【解决方案1】:

如果您在基类方法Update 中运行脚本,请确保调用base.Update()

void Update ()
{
    base.Update();

    // special Update code
}

【讨论】:

    【解决方案2】:

    当继承一个类时要重写的方法,你必须将这些方法声明为虚拟的。在您的示例中,两个类都有一个 private void Update() (当不声明访问修饰符时,它是隐式私有的)。 这意味着,继承类 (Tester) 甚至不知道它的基类已经有一个 Update() 方法。因此它不会覆盖它,而是隐藏它。一旦 Unity 调用 Tester 的 Update() 方法,它就完全没有效果,因为它是空的。

    您想要的是让 Tester 的 Update() 方法包含 BeastScript 的 Update() 方法的所有内容,并可选择扩展它。可以这样完成:

    public class BeastScript : MonoBehaviour
    {
        protected virtual void Update() // "protected" so subclasses can see it
        {                               // and "virtual" so subclasses can override it
            if (Input.anyKeyDown)
                ...
        }
    }
    
    public class Tester : BeastScript
    {
        protected override Update() // "protected" because overriding methods must not be less accessible than the method they override
        {                           // and "override" to specify that this method overrides a virtual method
            // optional: do stuff before calling BeastScript.Update()
            base.Update(); // this calls BeastScript.Update()
            // optional: do stuff after calling BeastScript.Update()
        }
    }
    

    【讨论】:

      【解决方案3】:

      您创建了一个空的 Update 方法来替换基本的 Update 方法。在 Tester 类中删除它

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-07-26
        • 1970-01-01
        • 1970-01-01
        • 2011-02-25
        • 2011-05-21
        • 2013-01-15
        • 2021-03-26
        • 1970-01-01
        相关资源
        最近更新 更多