シューティングチュートリアルをUnity5で(第10回)

10.1 タイトルの表示

GUITextはGameObjectではなく、Componentになった模様。ゆえにGameObjectからではなくComponent>Rendering>GUI Textで作成する(もしくはInspectorのAdd ComponentからRendering>GUI Text)。 f:id:shady:20150524174934j:plain

GUI Textコンポーネントは一オブジェクトには複数登録できないので、チュートリアル通りTitleの下に二つのGUI Textコンポーネント、とはいかない。 f:id:shady:20150524174958j:plain

なので一つのGUI Textに二行テキストを書き、AlignmentをCenter(中央揃え)、Line Spacing(改行高さ)を1.5に設定する事でチュートリアル通りの表示になるようにした。

f:id:shady:20150524175009j:plain f:id:shady:20150524175023j:plain

10.2 タイトル -> ゲームスタート ( -> 死んだら ) -> タイトル のマネージャークラスを作る

  • 特記事項なし
using UnityEngine;

public class Manager : MonoBehaviour {
    public GameObject player;
    private GameObject title;

    void Start () {
        title = GameObject.Find ("Title");
    }
    
    void Update () {
        if (IsPlaying () == false && Input.GetKeyDown (KeyCode.X)) {
            GameStart ();
        }
    }

    void GameStart()
    {
        title.SetActive (false);
        Instantiate (player, player.transform.position, player.transform.rotation);
    }

    public void GameOver()
    {
        title.SetActive (true);
    }

    public bool IsPlaying()
    {
        return title.activeSelf == false;
    }
}
  • 特記事項なし
using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {
    Spaceship spaceship;
    private Transform tran;
    private AudioSource audioSource;

    void Start() {
        spaceship = GetComponent<Spaceship>();
        tran = GetComponent<Transform>();
        audioSource = GetComponent<AudioSource> ();
        StartCoroutine("IE");
    }

    IEnumerator IE()
    {
        while (true) {
            spaceship.Shot(tran);
            audioSource.Play ();
            yield return new WaitForSeconds(spaceship.shotDelay);
        }
    }

    void Update () {
        float x = Input.GetAxisRaw("Horizontal");
        float y = Input.GetAxisRaw("Vertical");
        Vector2 direction = new Vector2(x, y).normalized;
        Move(direction);
    }

    void Move(Vector2 direction) {
        Vector2 min = Camera.main.ViewportToWorldPoint (new Vector2 (0, 0));
        Vector2 max = Camera.main.ViewportToWorldPoint (new Vector2 (1, 1));
        Vector2 pos = transform.position;

        pos += direction * spaceship.speed * Time.deltaTime;

        pos.x = Mathf.Clamp (pos.x, min.x, max.x);
        pos.y = Mathf.Clamp (pos.y, min.y, max.y);

        transform.position = pos;
    }

    void OnTriggerEnter2D(Collider2D c)
    {
        string layerName = LayerMask.LayerToName(c.gameObject.layer);
        if (layerName == "Bullet(Enemy)") {
            Destroy (c.gameObject);
        }

        if (layerName == "Bullet(Enemy)" || layerName == "Enemy") {
            FindObjectOfType<Manager> ().GameOver (); //追加箇所
            spaceship.Explosion ();
            Destroy (gameObject);
        }
    }
}
  • 特記事項なし
using UnityEngine;
using System.Collections;

public class Emitter : MonoBehaviour {
    public GameObject[] waves;
    private int currentWave;
    private Manager manager;

    void Start () {
        manager = FindObjectOfType<Manager> ();
        StartCoroutine ("IE");
    }
    
    IEnumerator IE() {
        while (true) {
            while (manager.IsPlaying () == false) {
                yield return new WaitForEndOfFrame ();
            }

            GameObject g = (GameObject)Instantiate (waves [currentWave], transform.position, Quaternion.identity);

            g.transform.parent = transform;

            while (g.transform.childCount != 0) {
                yield return new WaitForEndOfFrame ();
            }

            Destroy (g);

            if (waves.Length <= ++currentWave) {
                currentWave = 0;
            }
        }

    }
}