2016-03-27 29 views
0

我有一堂課,我想讓serializable(查看檢查器中的一些公共變量),但我也需要在該課程中使用Coroutines。要在我的課程中使用Coroutines,我必須從MonoBehaviour繼承它。但是,我不能使用serializable類的功能。帶有協程的可序列化類?

public class Act1HomeAwake : MonoBehaviour 
{ 
    public Act1_1HomeAwake act1_1HomeAwake; 

    public void StartAct1(int subActNumber) 
    { 
     switch(subActNumber) 
     { 
      case 1: act1_1HomeAwake.StartSubAct1_1(); break; 
     }      
    } 
} 

[System.Serializable] 
public class Act1_1HomeAwake // : MonoBehaviour 
{ 
    // don't see this 2 variables in the inspector WITH inheriting from MonoBehaviour 
    public OpenCloseAnimation openCloseEyesScript; 
    public Text textTipsTasksComponent; 

    // WITHOUT inheriting from MonoBehaviour compiler don't understand this construction 
    StartCoroutine("OpenCloseEyesAnimation"); 
} 
+0

您的代碼無效C#?你正嘗試從任何類成員之外調用一個方法。 –

回答

1

你需要你想要的類序列化到顯示:

[Serializable] // this is needed to show the object in Inspector 
public class OpenCloseAnimation {} 

[Serializable] 
public class Act1_1HomeAwake 
{ 
    public OpenCloseAnimation openCloseEyesScript; 
    public void CallCoroutine(MonoBehaviour mb) 
    { 
     mb.StartCoroutine(OpenCloseEyesAnimation()); 
    } 
    public IEnumerator OpenCloseEyesAnimation(){ yield return null;} 
} 

但認爲,也許你正在做的是錯誤的。如果你需要在你的班級中使用協程,那麼也許它應該是一個MonoBehaviour。其他方法是從包含您的對象的MonoBehaviour啓動協程。

public class MbClass : MonoBehaviour 
{ 
    public Act1_1HomeAwake homeAwake; 
    void Start(){ 
     // Considering you don't pass the MB in ctor anymore. 
     this.homeAwake = new Act1_1HomeAwake(); 
     StartCoroutine(this.homeAwake.OpenCloseEyesAnimation()); 
    } 
} 
相關問題