はじめに
- 書籍「ゲームの作り方 Unityで覚える遊びのアルゴリズム」と、それについてくるコードを読みながら Unity の使い方やスクリプトの記法などを自分用にまとめ中。
Assets から割り当てられた GameObject を検索する
- Assets にある Script Component などが、どの GameObject に割り当てて使われているかを参照する方法
- Assets の Component などを右クリック > [Find References In Scene] で、Hierarkey に割り当てられた GameObject の一覧が表示される
列挙型の宣言と使用
public enum STEP {
NONE = -1,
START,
GOAL,
GAME_OVER,
};
public STEP step = STEP.NONE;
switch(this.step) {
case STEP.START:
{
}
break;
}
文字列型配列の初期化
public static string[] evaluation_str = {
"okay",
"good",
"great",
"miss",
};
構造体の
public struct Result {
public int oni_defeat_num;
public int[] eval_count;
public int rank;
public float score;
public float score_max;
};
public Result result;
this.result.oni_defeat_num = 0;
this.result.eval_count = new int[(int)EVALUATION.NUM];
this.result.rank = 0;
this.result.score = 0;
this.result.score_max = 0;
for(int i = 0;i < this.result.eval_count.Length;i++) {
this.result.eval_count[i] = 0;
}
GameObject を参照する
public GameObject main_camera = null;
this.main_camera = GameObject.FindGameObjectWithTag("MainCamera");
同じ GameObject の Component を参照する
public ScoreControl score_control = null;
this.score_control = GetComponent<ScoreControl>();
他の GameObject の Component を参照する
public PlayerControl player = null;
this.player = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerControl>();
Script Component を GameObject に割り当てずに使う
- MonoBehaviour を継承したクラスでは使用できない
public ResultControl result_control = null;
this.result_control = new ResultControl();
配列の順次処理
public OniControl[] onis;
foreach(OniControl oni in this.onis) {
}
Prefab から GameObject の作成
GameObject go = Instantiate(this.OniEmitterPrefab) as GameObject;
GameObject の削除
Destroy(this.gameObject);
アニメーションの再生
Animation animation = this.transform.GetComponentInChildren<Animation>();
TODO
animation.CrossFade("P_attack_R", 0.2f);
animation.CrossFadeQueued("P_run");
Chapter1で左クリックから攻撃して敵を倒すまでの処理の流れ
- PlayerControl(Script Component)のUpdateで左クリックを拾う
- AttackColliderControl の衝突判定が有効になる
- 攻撃用オブジェクトが敵と衝突していた場合、攻撃効果の再生や効果音の再生
- OniGroupControl から SceneControl に倒した敵の数を引き渡す
- GUIControl で SceneControl を参照して倒した敵の数を画面に表示
Chapter2のシーン中での処理ステップの制御
enum STEP {
NONE = -1,
WAIT = 0,
PLAY_JINGLE,
NUM,
};
private STEP step = STEP.WAIT;
private STEP next_step = STEP.NONE;
private float step_timer = 0.0f;
void Update () {
this.step_timer += Time.deltaTime;
switch(this.step) {
case STEP.WAIT:
{
if(Input.GetMouseButtonDown(0)) {
this.next_step = STEP.PLAY_JINGLE;
}
}
break;
case STEP.PLAY_JINGLE:
{
if(this.step_timer > this.audio.clip.length + 0.5f) {
Application.LoadLevel("GameScene0");
}
}
break;
}
if(this.next_step != STEP.NONE) {
switch(this.next_step) {
case STEP.PLAY_JINGLE:
{
this.audio.Play();
}
break;
}
this.step = this.next_step;
this.next_step = STEP.NONE;
this.step_timer = 0.0f;
}
switch(this.step) {
case STEP.WAIT:
{
}
break;
}
}