Last active
October 20, 2016 02:35
-
-
Save Buravo46/8332962 to your computer and use it in GitHub Desktop.
【Unity】シーンを跨いでもスコアなどのデータを保持することができるスクリプト。
参考URL : http://goodscientist.tumblr.com/post/25617673512/dontdestroyonload
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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