unity3d 是否从Button启动异步Monobhavior类?

dbf7pr2w  于 2022-11-15  发布在  其他
关注(0)|答案(2)|浏览(144)

我在Unity & C#方面完全是个菜鸟,现在我有一个任务,我觉得有点势不可挡,所以任何输入都将非常感谢...
我得到了一个类,看起来像这样:

//using directives

public class PipelineExample : MonoBehaviour
{
    private CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
    private bool res = false;

    // Start is called before the first frame update
    void Start()

    {
        StartPipeline();
        while (!res) ;
    }

    async void StartPipeline()
    {
        // create models, pipeline, pipeline step
        IModel fbxModel = new FbxModel();
        Pipeline<IModel> pipeline = new Pipeline<IModel>();
        IAction<IModel> pipelineStep = new GenericPipelineStep<IModel>();

        // add steps to pipeline
        pipeline.AddPipeLineStep(pipelineStep);


        try
        {
            res = await pipeline.Execute(fbxModel, cancellationTokenSource.Token);
            Console.Write("Result of pipeline: " + res);
        }
        catch (OperationCanceledException)
        {
            Console.WriteLine("Canceled successfully!");
        }
    }

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

现在,我想在单击“开始”按钮时,以某种方式从另一个类调用此startmethod(?):

private void OnStartButtonClicked(MouseUpEvent evt)
{
    //What goes in here?
}

我不能只是流水线示例.开始很明显,
公共管道示例;启动也不起作用。
基本上我的问题是,如何从另一个类启动这个PipeLineExample类?

ctzwtxfj

ctzwtxfj1#

如果您对PipelineExample类进行了一些修改,您应该能够调用您正在寻找的方法。
下面是一个例子,你可以通过很多方法来实现这一点:

public class PipelineExample : MonoBehaviour
{
    private CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();

    public static PipelineExample instance { get; private set; }
    public static bool initialised { get; private set; }

    public IModel fbxModel { get; private set; }
    public Pipeline<IModel> pipeline { get; private set; }

    // Unity will create an instance of this class. We then use it as a 'singleton'.
    private void Awake ( )
    {
        if ( instance != null )
            return;

        instance = this;
    }

    public async Task<bool> StartPipeline ( )
    {
        if ( initialised )
            return true;

        // create models, pipeline, pipeline step
        fbxModel = new FbxModel();
        pipeline = new Pipeline<IModel>();
        IAction<IModel> pipelineStep = new GenericPipelineStep<IModel>();

        // add steps to pipeline
        pipeline.AddPipeLineStep ( pipelineStep );

        try
        {
            initialised = await pipeline.Execute ( fbxModel, cancellationTokenSource.Token );
            Console.Write ( "Result of pipeline: " + initialised );
        }
        catch ( OperationCanceledException )
        {
            Console.WriteLine ( "Canceled successfully!" );
        }

        return initialised;
    }

    // You might want to include a method to cancel the pipeline setup using cancellationTokenSource?
}

因为它是一个MonoBehaviour,Unity将处理创建该类的示例。如果你不想将该类作为组件附加到场景中的GameObject,你也可以将其实现为ScriptableObject(可能是我的首选)。但是你可能希望场景特定的值在应用程序中驱动这个组件。
然后,您可以如下调用设置:

private async void OnStartButtonClicked ( MouseUpEvent evt )
{
    if ( !PipelineExample.initialised )
    {
        // The pipeline isn't yet initialised. Alert user for a potential setup time.
        if ( !await PipelineExample.instance.StartPipeline ( ) )
        {
            // Something went wrong ... alert user?
            return;
        }
    }

    // PipelineExample is initialised.
    // PipelineExample.instance.fbxModel and PipelineExample.instance.pipeline are now set up.
}

请注意,这将在单击按钮时设置您的管道。如果加载时间很长,您可能会受益于某种用户通知,让用户知道存在潜在的延迟。

lf3rwulv

lf3rwulv2#

所以我找到了一个简单可行的解决方案。
我只是将PipelineExample中的方法设置为静态,并在按钮中调用start:管道示例.Start();

相关问题