Unity5の教科書 4章
WebGLで書き出してみました。
Unity WebGL Player | SwipeCar
ビルド時間かかるわー。macbookproのcorei5のメモリ16G積んでいても30分ぐらいかかる。
ちょっと辛いわー。
教科書のソースの抽象度が一定ではないので一定にした。
関数の抽象度は一定にすべき。ソースの書き方を指南する本ではないのでmain的なupdate関数にまるまる書くのは理解を助けるのには役立つけどプロダクションコードではきちんとメソッド分割すべき。まぁ、メソッド分割とかコードの抽象度の統一とかは業務系システムのセオリーでゲーム畑はどうなのか知らんけど。メソッドコールが速度的に耐えられないとかいう文化があるのかもしれない。
@GameController
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System; public class GameDirector : MonoBehaviour { GameObject car; GameObject flag; GameObject distance; // Use this for initialization void Start () { this.car = GameObject.Find ("car"); this.flag = GameObject.Find ("flag"); this.distance = GameObject.Find ("Distance"); } // Update is called once per frame void Update () { float length = getLength (); if (length >= 0f) { setText ("to Goal," + length.ToString ("F2") + "m"); } else { setText ("GameOver"); } } float getLength () { return this.flag.transform.position.x - this.car.transform.position.x; } void setText (String str) { this.distance.GetComponent<Text> ().text = str; } }
@CarController
using System.Collections; using System.Collections.Generic; using UnityEngine; public class CarController : MonoBehaviour { // 速度 float speed = 0; // 初期場所 Vector2 startPos; // Use this for initialization void Start () { } // Update is called once per frame void Update () { // スワイプの長さを求める detectMouse (); // 移動 move (); // 減衰 decay (); } /** * スワイプの長さを求める */ void detectMouse () { if (Input.GetMouseButtonDown (0)) { this.startPos = Input.mousePosition; } if (Input.GetMouseButtonUp (0)) { Vector2 endPos = Input.mousePosition; float swipeLength = endPos.x - this.startPos.x; this.speed = swipeLength / 500.0f; play (); } } /** * 移動 */ void move () { transform.Translate (this.speed, 0, 0); } /** * 減衰 */ void decay () { this.speed *= 0.98f; } /** * 音声再生 */ void play () { GetComponent<AudioSource> ().Play (); } }
Unity5の教科書 2D&3Dスマートフォンゲーム入門講座
- 作者: 北村愛実
- 出版社/メーカー: SBクリエイティブ
- 発売日: 2016/06/22
- メディア: Kindle版
- この商品を含むブログを見る