简体   繁体   English

为从Unity中的抽象类继承的抽象类/脚本创建对象字段

[英]Creating an objectfield for an abstract class/scripts that inherit from that abstract class in Unity

I'm trying to make a cutscene system in Unity (I hate using the timeline and it doesn't work for my purpose without a tonne of custom playables which I also hate making) so I decided to start making my own that uses a node based system. 我试图在Unity中制作一个过场动画系统(我讨厌使用时间轴,没有大量我也讨厌制作的自定义可玩对象,它就无法达到我的目的),所以我决定开始制作自己的使用节点的基于系统。

My idea was that each node holds a class that inherits from an abstract class. 我的想法是每个节点都拥有一个从抽象类继承的类。 This will be the actual code that runs for that node but my issue comes with the inheritance part: 这将是为该节点运行的实际代码,但是我的问题来自继承部分:

It seems it's impossible to create an objectfield that only accepts scripts that inherit an abstract class. 似乎不可能创建仅接受继承抽象类的脚本的objectfield。 If I create an objectfield that only accepts CutsceneEvent (the name of the abstract class) I can't add any script to it whether it's the abstract class itself or any script that inherits from it. 如果我创建一个仅接受CutsceneEvent(抽象类的名称)的对象字段,则无论是抽象类本身还是从其继承的任何脚本,都无法向其添加任何脚本。

My only solution was to set the field as Monoscript and then check the baseclass and remove/keep the script depending on if it inherits from CutsceneEvent but that comes with it's own issues (trying to then get specific functions and variables from that script which is only a Monoscript type and can't be converted to CutsceneEvent). 我唯一的解决方案是将字段设置为Monoscript,然后检查基类并根据脚本是否继承自CutsceneEvent来删除/保留脚本,但这是它自己的问题(试图从该脚本中获取特定的函数和变量只是Monoscript类型,无法将其转换为CutsceneEvent)。

I'm completely stumped. 我完全迷住了。 Everywhere I've asked I've gotten basically ignored seemingly because no one knew how to solve it. 我问过的所有地方似乎都基本被忽略了,因为没人知道如何解决。

Here's some code. 这是一些代码。

The abstact class: 表象思维班:

//CutsceneEvent 
public abstract class CutsceneEvent : MonoBehaviour
{
    public System.Action after;
    public abstract IEnumerator Function();
}

How I'm defining the variables(I'm using the xNode framework): 我如何定义变量(我正在使用xNode框架):

public CutsceneEvent function;
[Output] public CutsceneEventNode nextEvent;
[Input] public CutsceneEventNode lastEvent;
protected override void Init()
{

}




// Return the correct value of an output port when requested
public override object GetValue(NodePort port)
{

    return null;
}

U can use an Interface instead of an abstract class for your fields: U可以为您的字段使用接口而不是抽象类:

public interface ICutsceneEvent {}

//CutsceneEvent 
public abstract class CutsceneEvent : MonoBehaviour, ICutsceneEvent
{
    public System.Action after;
    public abstract IEnumerator Function();
}

Then instead of having a list of CutsceneEvent's, you can have a list of ICutsceneEvent's. 然后,您可以拥有ICutsceneEvent的列表,而不是拥有CutsceneEvent的列表。 You should put the abstract methods in the interface and implement them in the abstract class :D. 您应该将抽象方法放在接口中,并在抽象类:D中实现它们。

Also, check out Cinemachine if you haven't ^^ 另外,如果您还没有^^,请查看Cinemachine

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

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