简体   繁体   English

Unity3d中的多态

[英]polymorphism in Unity3d

i need some help. 我需要协助。 I dont know if this possible: 我不知道这是否可能:

I have 2 Scripts, the interactBase and the interactRock script. 我有2个脚本,interactBase和interactRock脚本。

The interactRock script extends the interactBase Script. interactRock脚本扩展了interactBase脚本。

interactRock overwrites the function "interact()" interactRock覆盖函数“ interact()”

So i try to get the reference of the Object and call the function of the child. 因此,我尝试获取对象的引用并调用子级的函数。

But it dosent seems to work. 但是它似乎起作用。 Maybe u can help me? 也许你可以帮我吗?

Code: 码:

interactBase: interactBase:

    using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class interactBase : MonoBehaviour {

    public interactBase(){

    }

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }

    public void interact(){

    }
}

rockInteract: rockInteract:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class rockInteract : interactBase {
    public bool triggered;

    public rockInteract(){
    }

    // Use this for initialization
    void Start () {
        triggered = false;
    }

    // Update is called once per frame
    void Update () {

    }

    public new void interact(){
        triggered = true;
        print ("working");
    }
}

The Calling Script: 调用脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class triggerScript : MonoBehaviour {
    Animator anim;
    SpriteRenderer sprite;
    public bool triggered;
    public GameObject toTrigger;
    interactBase baseFunc;

    // Use this for initialization
    void Start () {
        anim = GetComponent<Animator> ();
        sprite = GetComponent<SpriteRenderer> ();
        triggered = false;

        baseFunc = toTrigger.GetComponent<interactBase>(); 
        print (baseFunc);
    }

    // Update is called once per frame
    void Update () {
        transform.position += Vector3.zero;
        if (triggered == true) {
            //print (baseFunc.interact ());
            baseFunc.interact ();
            triggered = false;
        }
    }
}

Thank you 谢谢

The issue is that you need to define the base interact as 问题是您需要将基本交互定义为

virtual void interact()

and the child as 和孩子一样

override void interact

for the child interact to be called through a base class pointer. 通过基类指针调用子交互。 Using new does not accomplish that. 使用new并不能做到这一点。

new really means "child has its very own method, which just happens to have the same name, as an entirely different method in the base class. This new method should be called when you use a child pointer but not when you use a base pointer, since base has its own method with that name". new的真正含义是“ child具有自己的方法,恰好具有相同的名称,这与基类中的完全不同的方法相同。当使用child指针时应调用此新方法,而在使用base指针时不应调用此新方法。 ,因为base拥有使用该名称的自己的方法”。 Virtual and override means "child has a different version of the same method and that is the one to call regardless of what type of pointer you use to this class". 虚拟和重写意味着“子级具有相同方法的不同版本,并且无论您使用何种指针指向此类,都必须调用该子级”。

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

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