简体   繁体   English

将游戏对象附加到继承自单行为的脚本

[英]Attaching game objects to scripts that inherit from monobehaviour

I have my game organized by states as follows: BeginState PlayState WonState LostState 我按州对游戏进行了如下组织:BeginState PlayState WonState LostState

and a script to manage them called StateManager.cs: 还有一个用于管理它们的脚本,称为StateManager.cs:

public class StateManager : MonoBehaviour 
{
    private IStateBase activeState;

    void Start () 
    {
        activeState = new BeginState (this);
    }

    void Update () 
    {
        if (activeState != null)
            activeState.StateUpdate();
    }

    void OnGUI()
    {

        if (activeState != null)
            activeState.ShowIt();    }

    public void SwitchState(IStateBase newState)
        {
        activeState = newState;
        }
}

The problem is when I am attaching a script component to a game object this statemanager.cs script is only I use because it is classified as Monobehaviour. 问题是,当我将脚本组件附加到游戏对象时,只能使用此statemanager.cs脚本,因为它被归类为Monobehaviour。

For example I want to use the Playtstate.cs, but I am not able. 例如,我想使用Playtstate.cs,但我不能。

using UnityEngine;
using Assets.Code.Scripts;
namespace Assets.Code.States
{
    public class PlayState : IStateBase
    {
    private StateManager manager;


    public PlayState (StateManager managerRef)
    {
        manager = managerRef;
        Debug.Log ("Constructing PlayState");

    }
    public void StateUpdate()
    {
        if (Input.GetKeyUp(KeyCode.Space))
        {
            manager.SwitchState(new WonState (manager));
        }

        if (Input.GetKeyUp(KeyCode.Return))
        {
            manager.SwitchState(new LostState (manager));
        }
    }

Despite PlayState being an IStateBase instance it is not considered Monobehaviour, so I cannot add a button that is exclusive to this state. 尽管PlayState是IStateBase实例,但它不被视为Monobehaviour,因此我无法添加此状态专有的按钮。 How can I get around this? 我该如何解决?

To be able to attach any Script to GameObject you need to have your script or any class in its inheritance tree to inherit from MonoBehaviour . 为了能够将任何Script附加到GameObject您需要在其继承树中具有您的脚本或任何类以从MonoBehaviour To be able to assign its public fields from Inspector , you need to also mark it as [Serializable] . 为了能够从Inspector分配其公共字段,您还需要将其标记为[Serializable] Please note that your IStateBase is nowhere to be seen as inheriting from MonoBehaviour . 请注意,您的IStateBase在任何地方都不会被视为继承自MonoBehaviour Actually we can't see its implementation at all. 实际上,我们根本看不到它的实现。

Let's see all the general possibilites we have in Unity3D : 让我们看看Unity3D所有的一般可能性:

Inherit from other class that inherits from MonoBehaviour 继承自从M​​onoBehaviour继承的其他类

//Works because it inherits from class inheriting MonoBehaviour
public class TestScript : BaseTest
{
}

//This will also be assignable from Inspector
public class BaseTest : MonoBehaviour
{
}

Inherit from other abstract class that inherits from MonoBehaviour 继承自从M​​onoBehaviour继承的其他抽象类

//This will also work as we still have MonoBehaviour in a root of our inheritance
public class TestScript : BaseTest
{
}

//This will not be assignable from Inspector
public abstract class BaseTest : MonoBehaviour
{
}

Inherit from interface that inherits from MonoBehaviour 从继承自MonoBehaviour的接口继承

Well, interface can't inherit from class or abstract class, only from other interface so it won't work anyway. 好吧,接口不能从类或抽象类继承,只能从其他接口继承,因此无论如何都无法工作。

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

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