Skip to content

Instantly share code, notes, and snippets.

@Buravo46
Last active October 20, 2016 02:35
Show Gist options
  • Save Buravo46/8332962 to your computer and use it in GitHub Desktop.
Save Buravo46/8332962 to your computer and use it in GitHub Desktop.
【Unity】シーンを跨いでもスコアなどのデータを保持することができるスクリプト。 参考URL : http://goodscientist.tumblr.com/post/25617673512/dontdestroyonload
using UnityEngine;
using System.Collections;
public class GameDataStorageScript : MonoBehaviour {
private static bool created = false;
int score = 0;
void Awake(){
if(!created){
// シーンを切り替えても指定のオブジェクトを破棄せずに残す
DontDestroyOnLoad(this.gameObject);
// 生成した
created = true;
} else {
Destroy(this.gameObject);
}
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnGUI(){
GUI.Label(new Rect(10,20,120,20),"SCORE : " + score);
}
}
#pragma strict
// スコア
var score : int = 0;
// 生成しているかどうか
private static var created:boolean = false;
function Awake(){
// シーンを切り替えても指定のオブジェクトを破棄せずに残す
if(!created){
DontDestroyOnLoad(this.gameObject);
created = true;
} else {
Destroy(this.gameObject);
}
}
function Start () {
}
function Update () {
}
function OnGUI(){
GUI.Label(Rect(10,20,120,20),"SCORE : " + score);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment