2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

unity > TDD > 動作テスト用gameobjectの作成と使用

Last updated at Posted at 2015-09-12
動作確認
Unity 5.1.3-f1 on MacOS X 10.8.5

UDPで受信したデータを元にグラフ表示をするソフトを実装している中、動作テストにlinuxからUDP送信を実行するスクリプトを走らせるのが面倒になってきた。

ふと、テスト用のGameObjectを作って最初はfalseにしておいて、それをSetActiveにすればいいと思った。

手順

  1. Test_weeklyというGameoOjectを作成して、スクリプトとしてTest_weekl.csを新規作成する
  2. Test_weekly GameObjectは名前の左側のチェックをOFFにして、最初は起動しないようにしておく。
  3. プレイモードで実行後、インスペクタでTest_weeklyをActiveにすると、中のスクリプトが実行される。

Main_unity_-150908-udpTimeGraph-PC__Mac___Linux_Standalone__Personal.jpg

下記がTest_weekly.csの中身。時系列グラフのデータに3点のデータをセットする処理を実装。

Test_weekly.cs
using UnityEngine;
using System.Collections;

public class Test_weekly : MonoBehaviour {

	void Test_addWeeklyData() {

		string [] dts = new string[]{ "09:30", "11:30", "13:30" };
		float [] vals = new float[] { 0.5f, -0.9f, 0.9f };

		System.DateTime curDt;
		float yval;
		int idx = 0;

		foreach(var dt in dts) {
			curDt = System.DateTime.Parse(dt);
			yval = vals[idx];
			timeGraphScript.SetXYVal(curDt, yval);
			idx++;
		}
	}

	void Start () {
		Test_addWeeklyData ();
	}
	
	void Update () {
	
	}
}

使用例

playmode起動直後。グラフには何も描かれていない。

Main_unity_-150908-udpTimeGraph-PC__Mac___Linux_Standalone__Personal.jpg

インスペクタでTest_weeklyをActiveにすると3点のデータをセットするスクリプトが実行され、以下のようになる。

Main_unity_-150908-udpTimeGraph-PC__Mac___Linux_Standalone__Personal.jpg

これでテスト駆動開発ができるようになる。

今はインスペクタでSetActiveにしているが、通信コマンドでActiveにするものを用意してもいいかと思う。

コルーチンを使ってWait付きの処理もできそう。

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?