在非MonoBehaviour类中使用协程 [英] Use coroutine inside a non MonoBehaviour class

查看:549
本文介绍了在非MonoBehaviour类中使用协程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在非Monobehaviour类的实例内传递Monobehaviour?我发现TonyLi提到的链接您可以传递Monobehaviour来启动和停止类实例中的协程,但是他没有显示如何做到这一点.他这样做theEvent.StartEvent(myMonoBehaviour);但他没有显示他从哪里获得我的单身行为.我在互联网上四处张望,但似乎找不到方法.

How can you pass a Monobehaviour inside an instance of a non Monobehaviour class? I found this link where TonyLi mentions that you can pass a Monobehaviour to start and stop coroutines inside a instance of a class, but he does not show how you can do that. He does this theEvent.StartEvent(myMonoBehaviour); but he does not show where he gets myMonobehaviour from. I looked around on the internet but I cannot seem to find how.

  • 编辑

这就是我想要做的.我想在一个类的实例中运行一个协程.我还希望能够在类实例中停止协程.我想这样做,这样我的场景中就不会有任何具有大型管理器的对象,而且我可以将代码重用于要以此方式进行乒乓操作的任何对象.代码将Gameobject朝一个方向移动,然后中断并朝另一个方向移动,然后再次中断,依此类推.但是我无法从类外部启动协程.

Here is what I am trying to do. I want to run a coroutine inside an instance of a class. I also want to be able to stop the coroutine inside the instance of the class. I want to do it this way so that I don't have any objects in my scene that have large managers and also so that I can reuse the code for any object that I want to pingpong in this way. The code moves a Gameobject in one direction then takes a break and moves it in the other direction and takes a break again etc. But I cannot start the coroutine from outside the class.

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

[RequireComponent (typeof(Image))]
public class SpecialBar : MonoBehaviour {

    public float rangeX;
    public float breakTime;
    public float step;
    float startProgress = 0.5f;
    PingPongGameObject pingPonger;

    Color[] teamColors = new Color[]{new Color(255,136,0),new Color(0,170,255)};

    void Start()
    {

        for(int i = 0; i < teamColors.Length; ++i)
        {
            teamColors[i] = StaticFunctions.NormalizeColor (teamColors[i]);
        }

        pingPonger = new PingPongGameObject (gameObject.transform.position,
            new Vector3(rangeX,0.0f,0.0f),
            gameObject,
            startProgress,
            breakTime,
            step
            );
    }
}

第二堂课是我的协程所在的地方.

The second class is where my coroutine is in.

public class PingPongGameObject
{
    float step;
    Vector3 center;
    Vector3 range;
    GameObject ball;
    float progress;
    float breakTime;
    Vector3 endPos;
    Vector3 oppositePosition;


    public PingPongGameObject(Vector3 _center, Vector3 _range, GameObject _ball, float _startProgress, float _breakTime, float _step)
    {
        center = _center;
        range = _range;
        ball = _ball;
        progress = _startProgress;
        breakTime = _breakTime;
        step = _step;
        endPos = center - range;
        oppositePosition = center + range;
        // This is where I want to start the coroutine
    }

    public IEnumerator PingPong()
    {


        while (progress < 1) {
            progress += Time.deltaTime * step;
            Vector3 newPos = Vector3.Lerp (oppositePosition, endPos, progress);
            ball.transform.position = newPos;
            yield return null;
        }
        Vector3 temp = endPos;
        endPos = oppositePosition;
        oppositePosition = temp;
        progress = 0;
        yield return new WaitForSeconds (breakTime);
        yield return null;
    }

    public float Step
    {
        set{step = value;}
    }

    public void StopCoroutine()
    {
        // This is where I want to stop the coroutine
    }
}

推荐答案

TonyLi提到您可以通过Monobehaviour来启动和停止 在一个类的实例中的协程,但是他没有显示你如何 可以做到的.他做到了

TonyLi mentions that you can pass a Monobehaviour to start and stop coroutines inside a instance of a class, but he does not show how you can do that. He does this

您可以使用this关键字来做到这一点.此关键字将获取MonoBehaviour的当前实例.

You are can do that with the this keyword. The this keyword will get the current instance of MonoBehaviour.

在此示例中,有一棵树,恰好有一个组件MonoScript:

In this example there's a tree, which happens to have a component MonoScript:

MonoScript的特定实例可以(如果是c#程序)想要实例化通用c#类,NonMonoScript:

That particular instance of MonoScript can if it wants (since it's a c# program) instantiate a general c# class, NonMonoScript:

要通过以下类别传递MonoBehaviour的类:

Class to pass MonoBehaviour from:

public class MonoScript : MonoBehaviour
{
    void Start()
    {
        NonMonoScript  nonMonoScript = new NonMonoScript();
        //Pass MonoBehaviour to non MonoBehaviour class
        nonMonoScript.monoParser(this);
    }
}

接收通行证MonoBehaviour实例的类:

public class NonMonoScript 
{
    public void monoParser(MonoBehaviour mono)
    {
        //We can now use StartCoroutine from MonoBehaviour in a non MonoBehaviour script
        mono.StartCoroutine(testFunction());

       //And also use StopCoroutine function
        mono.StopCoroutine(testFunction());
    }

    IEnumerator testFunction()
    {
        yield return new WaitForSeconds(3f);
        Debug.Log("Test!");
    }
}

您还可以将monoParser函数中的mono引用存储在要重用的本地变量中.

You can also store the mono reference from the monoParser function in a local variable to be re-used.

这篇关于在非MonoBehaviour类中使用协程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆