kidoOooOoooOOom

ゲーム開発やってます

Unity Tutorial Beginner Space Shooter chapter 15で学んだ事

GUI Textを用いてスコア表示

  • Viewport Space
    • 画面の左下の座標を(0,0), 右上の座標を(1,1)としてtrasnform指定する方式
  • Screen Space
    • 画面のx,y座標を絶対値で指定する方式


GameController側でスコア管理とスコアテキストの更新を行うようにスクリプトを更新。

    public GUIText scoreText;
    private int score;

    void Start()
    {
        score = 0;
        UpdateScore();
        StartCoroutine(SpawnWaves());
    }

    public void AddScore(int addScoreValue)
    {
        score += addScoreValue;
        UpdateScore();
    }

    void UpdateScore()
    {
        scoreText.text = "Score: " + score;
    }


また、破壊されることでスコアが加算されるオブジェクトのスクリプトから
GameControllerへの参照を取得してAddScoreを呼び出す。
GameControllerの参照の取得には、FindWithTagを用いた。
tagを指定してGameObjectを取得し、GetComponentメソッドを用いてGameControllerのオブジェクトを取得している。
このとき、GetComponentではというジェネリック記法で型を指定してオブジェクトを取得している。

    private GameController gameController;

    void Start()
    {
        GameObject gameControllerObject = GameObject.FindWithTag("GameController");
        if (gameControllerObject != null)
        {
            gameController = gameControllerObject.GetComponent <GameController>();
        }
        if (gameController == null)
        {
            Debug.Log("Cannot find GameController script");
        }
    }

Unity: スクリプトからMainCameraにアクセスする方法
http://hamken100.blogspot.jp/2012/05/unity-maincamera.html

GetComponentでプロパティ
http://pukapuka-ahirusan.blogspot.jp/2011/10/getcomponent.html