在使用Unity開發時,有些時候想要測試MonoBehaviour中的一些方法,但是又不想去單獨寫用例調用它,(想根據自己的需要在任何你覺得舒服的時候調用它)
這個時候就可以使用這個工具,首先需要 FunctionCallerAttribute.cs 和AbstractFunctionCallerEditor.cs ,AbstractFunctionCallerEditor.cs是支持MonoBehaviour編輯器的
一些代碼,不用在意那些細節。bstractFunctionCallerEditor.cs就是關聯你的方法和按鈕的類,一看便知~
使用過程:
1、編寫一個對應monoBehaviour的editor類這個類不是繼承於Editor 而是AbstractFunctionCallerEditor ,下例:
[CustomEditor(typeof(LightManager))]
public class LightManagerEditor : AbstractFunctionCallerEditor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
}
}
2、在你需要測試的方法的上一行加上一句話:
[FunctionCallerAttribute("TurnOffTheLight")]
public void TurnOffTheLight(){}
此時在編輯器中就會發現一個按鈕:
這樣在任何需要的時候都可以調用這個方法了~
8月 13日