Unityピンポンゲーム学習中 5Aから5C 動画
今回は、シーン間の変数を保持するPlayerPrefsとスコアです。
Unity tutorial – Pong Game – How To Make Mobile Games – part 5A
—
Unity tutorial – Pong Game – How To Make Mobile Games – part 5B
scoreの表示、PlayerPrefs 変数保持プロパティ
MainGame.js
CreateEmptyで作ったオブジェクトに設定します。
Scoreの点数を増加させます。
var score3DText:TextMesh; private var score:int=0; function Awake(){ InvokeRepeating("UpdateScore",0.05,0.05); } function Start () { } function UpdateScore () { score+=1; score3DText.text="Score:"+score.ToString(); } function GameOver(){ //ball.jsで、使用する関数になります。 if(score>PlayerPrefs.GetInt("highScore")){ PlayerPrefs.SetInt("highScore",score); } Application.LoadLevel("menu"); }
—
Unity tutorial – Pong Game – How To Make Mobile Games – part 5C
ゲーム画面でハイスコアになったときに、最初の画面にハイスコアを表示させる。
つまり異なるシーン間で変数をやりとりする。
ballオブジェクトにGeneralScriptをかぶせる
ball.jsとMainGame.jsは相互関係になります。
ここがみそです。
menuシーンに、highScore3DTextを作成します。
ball.js var mainGameScript:MainGame; function Awake(){ rigidbody.AddForce(4,4,0,ForceMode.Impulse); //4c InvokeRepeating("IncreaseBallVelocity",2,2);//2sec,2sec= start time, repeat time } function Update(){ if(transform.position.y<-5){ //Application.LoadLevel("menu"); mainGameScript.GameOver(); } } //4c function IncreaseBallVelocity(){ rigidbody.velocity*=1.05; Debug.Log("velocity:"+rigidbody.velocity); }
MenuScene.jsも書き換えます。そして、MenuシーンのGeneralScriptのhigh Scrore 3DTextに3DTextであるhighScore3DTextを選択します。
ここがみそです。
private var ray:Ray; private var rayCastHit:RaycastHit; var highScore3DText:TextMesh; function Awake(){ highScore3DText.text="High Score: "+ PlayerPrefs.GetInt("highScore").ToString(); } function Start () { } function Update () { if(Input.GetMouseButtonDown(0)){ ray=Camera.main.ScreenPointToRay(Input.mousePosition); if(Physics.Raycast(ray,rayCastHit)){ if(rayCastHit.transform.name=="playButton"){ Application.LoadLevel("pon2"); } } } }