2016-11-09 17 views
5

如何在非Monobehaviour類的實例中傳遞Monobehaviour?我發現這個link TonyLi提到你可以通過Monobehaviour在一個類的實例中啓動和停止協程,但是他沒有告訴你如何做到這一點。他這樣做了event.StartEvent(myMonoBehaviour);但他沒有顯示他從哪裏得到我的單子。我在互聯網上四處張望,但我似乎無法找到如何。在非MonoBehaviour類中使用協程

  • 編輯

這裏就是我要做的。我想在一個類的實例中運行一個協程。我也希望能夠在課堂中停止協程。我想這樣做,這樣我的場景中就沒有任何擁有大型管理器的對象,並且我可以重複使用代碼來處理任何想要以這種方式乒乓的對象。該代碼向一個方向移動一個GameObject,然後休息並將其移向另一個方向並再次休息等。但是我無法從課程外部啓動協程。

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 
      ); 
    } 
} 

第二類是我的協程在不在。

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 
    } 
} 
+0

是的,我會在一瞬間對我的問題進行補充。 –

+0

我重新修改了這個問題。 –

+0

查看我離開的答案。你可以使用'this'關鍵字。 – Programmer

回答

7

TonyLi提到,你可以傳遞一個Monobehaviour啓動和停止一個類的實例裏面 協程,但他確實不顯示你如何 可以做到這一點。他這樣做

你是可以做到這一點的this關鍵字。該關鍵字將獲得MonoBehaviour的當前實例。

在這個例子中有一棵樹,它恰巧有一個組件MonoScript

enter image description here

那的MonoScript特定實例可如果它要(因爲它是C#程序)實例化一個普通的C#類, NonMonoScript

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參考:接收通過MonoBehaviour實例

類。

+0

夥計 - 閱讀邁克的描述:*「,這樣我就可以重用任何代碼對象,我想用這種方式乒乓球,代碼向一個方向移動一個GameObject,然後休息並將其移向另一個方向「*所有他正在尋找的是一個平面組件!!!!!!!! ! – Fattie

+1

謝謝你們!程序員這正是我所期待的。喬·布洛我意識到,這可能不是處理我將考慮到的問題和進入你的完美的最聰明的方式。既然這是我正在尋找的aw子,我會拿這個來做一個aw。。 –

+0

@MikeOttink不客氣。我曾遇到過無法使用'StartCoroutine'和'StopCoroutine'功能的問題。這是我解決它的方式。有時,你不需要從'MonoBehaviour'派生你的腳本。其他時候你做。這取決於你的情況,但這個答案只是展示如何將MonoBehaviour傳遞給你問的另一個腳本。喬的回答也應該有效。快樂的編碼! – Programmer