ランキングという検索が多かったので うちのストパンSTGでランキングをどうやっているかを
簡単に書いておきます。
だいたいPlayerPrefsを使用するとデータを外部に保持できるということは以前おはなししましたので
どうやって使用するか。
おおまかな流れとしては
◯ランキングの初期化
↓
◯ネームエントリー
↓
◯ランキングのソート
↓
◯ランキングの表示
これらがあればスタンドアローンでの表示は大丈夫だと思います。
はじめは2分木ソートなどの関数を使用していたのですが、ランク値の個数が少ないのと デバッグが大変になったので、 簡単にまとめてみた感じです。
急ごしらえでバグあるかも知れないですけど なおしたらこっそりおしえてくださいw
PHP等を使用したネットランキングの場合は またちょっとややこしいので今回は割愛します。
あと どこかにキーを削除する関数を設けて ランキングの初期化をしたほうがデバッグ時にも重宝しますので
挑戦してみてください。
値を選択して消去する場合は PlayerPrefs.DeleteKey ("キーの名前”);
値をすべて消去する場合 PlayerPrefs.DeleteAll
■ランキングの初期化
private var Score:int[]=[100000,80000,60000,50000,40000,30000,20000];
private var Name:String[]=["AAA","BBB","CCC","DDD","EEE","FFF","GGG"];
private var Class:String[]=["FIGHTER","FIGHTER","FIGHTER","FIGHTER","FIGHTER","FIGHTER","FIGHTER"];
まずはじめにダミーデータを用意してランキングを初期化しておきます。
function Awake(){
if(PlayerPrefs.GetInt("Score1") == null) SetData();
Score1という値が入っているか(初回起動かどうか)をチェックして 値が入っていなければ
初期化の関数『SetData()』を呼びます。
初回起動チェックなので なにかフラグを値として入れておいても構いません
}
function SetData()
{
PlayerPrefsを使ってレジストリにスコアの初期値を書き込みます
ランキングに表示される個数分の値を記述します。
PlayerPrefs.SetInt("Score1",Score[0]);
PlayerPrefs.SetString("Name1", Name[0]);
PlayerPrefs.SetString("Class1", Class[0]);
PlayerPrefs.SetInt("Score2",Score[1]);
PlayerPrefs.SetString("Name2", Name[1]);
PlayerPrefs.SetString("Class2", Class[1]);
・・・・・・
}
■ネームエントリー
function Awake(){
var playerNameInput:String = "AAA";
var ScoreTmp:int = PlayerPrefs.GetInt("Score7");
if(ScoreTmp <= Total_Score )
{
requirePlayerName=true;
}else { Application.LoadLevel("Ranking");}
一番最下位のランキングスコアとゲーム終了時のスコアを比較して条件が合えば
ネームエントリーのテキストフィールドを表示
それ以外の場合『Ranking』のシーンを読み込み
}
function OnGUI(){
if(gSkin)
GUI.skin = gSkin;
else
Debug.Log("StartMenuGUI : GUI Skin object missing!");
if(requirePlayerName){
myWindowRect = GUILayout.Window (9, Rect(Screen.width/2-150,Screen.height/2-100,300,100), EntryNameMenu, "enter name");
}
}
function EntryNameMenu(id : int){
GUILayout.BeginVertical();
GUILayout.Space(10);
GUILayout.BeginHorizontal();
GUILayout.Space(10);
playerNameInput = GUILayout.TextField(playerNameInput);
GUILayout.Space(10);
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Space(10);
if(playerNameInput.length>=1){
if(GUILayout.Button("entry")){
requirePlayerName=false;
PlayerPrefs.SetString("playerName", playerNameInput);
Application.LoadLevel("Ranking");
}
}else{
GUILayout.Label("Enter name to continue");
}
GUILayout.Space(10);
GUILayout.EndHorizontal();
GUILayout.EndVertical();
}
■ランキングのソート
function RankDataSet()
{
var DATA_SIZE:int;
var insertData:int;
Score[0] = PlayerPrefs.GetInt("Score1");
Name[0] = PlayerPrefs.GetString("Name1");
Class[0] = PlayerPrefs.GetString("Class1");
Score[1] = PlayerPrefs.GetInt("Score2");
Name[1] = PlayerPrefs.GetString("Name2");
Class[1] =PlayerPrefs.GetString("Class2");
.........................
スコアの表示数分記述
DATA_SIZE =Score.length;
insertData = Total_Score ;
『Total_Scoreはゲーム終了時のスコアでソートのキーになります』
var min:int;
for(var i= DATA_SIZE-1;i>=0;i--){
if (Score[i] < insertData) min=i;
}
for(var ii= DATA_SIZE-1;ii>min;ii--){
Score[ii]=Score[ii-1];
Name[ii]=Name[ii-1];
Class[ii]=Class[ii-1];
}
Score[min] = insertData;
Name[min] = PlayerPrefs.GetString("playerName");
Class[min] = ClassDataSet();
1:スコアとインサートデータ(今回のゲームの終了時のスコア)を比較して
スコアのデータ数分だけのソートを行います
変数『min』には比較結果のランクが入ります。
2:for文でminの位置までひとつづつランクデータを下にずらします
3:minのランクにインサートデータとネームエントリーで入力された名前をセットします
4:PlayerPlefsをつかってソート終了のデータをレジストリに(スタンドアローンの場合)書き込みます
PlayerPrefs.SetInt("Score1",Score[0]);
PlayerPrefs.SetString("Name1",Name[0]);
PlayerPrefs.SetString("Class1",Class[0]);
PlayerPrefs.SetInt("Score2",Score[1]);
PlayerPrefs.SetString("Name2",Name[1]);
PlayerPrefs.SetString("Class2",Class[1]);
スコア表示数分記述・・・・・・・・・・・・・・・
return ;
}
■ランキングの表示
function OnGUI () {
if(gSkin)GUI.skin = gSkin;
else
Debug.Log("StartMenuGUI : GUI Skin object missing!");
GUI.contentColor = Color (0.75, 0.0 ,0.0);
var backgroundStyle : GUIStyle = new GUIStyle();
backgroundStyle.normal.background = backdrop;
GUILayout.Label ("Press Anykey to Gamestart");
isLoading = true;
if (Input.anyKeyDown) Application.LoadLevel(0);
GUI.Label( Rect( Screen.width/2-340, 240, 128, 32),"RANK" );
GUI.Label( Rect( Screen.width/2-164, 240, 164, 32),"SCORE" );
GUI.Label( Rect( Screen.width/2+60, 240, 164, 32),"NAME" );
GUI.Label( Rect( Screen.width/2+240,240, 164, 32),"CLASS" );
GUI.Label( Rect( Screen.width/2-340, 300, 128, 32),"1ST" );
GUI.Label( Rect( Screen.width/2-340, 360, 128, 32),"2ND");
GUI.Label( Rect( Screen.width/2-340, 420, 128, 32),"3RD" );
GUI.Label( Rect( Screen.width/2-340, 480, 128, 32),"4TH");
GUI.Label( Rect( Screen.width/2-340, 540, 128, 32),"5TH");
GUI.Label( Rect( Screen.width/2-340, 600, 128, 32),"6TH");
GUI.Label( Rect( Screen.width/2-340, 660, 128, 32),"7TH");
GUI.contentColor = Color.white;
GUI.Label( Rect( Screen.width/2-164, 300, 164, 32),""+ Score[0]);
・・・・・・・・・・・・・・・・・・・・・・・・・・
GUI.Label( Rect( Screen.width/2-164, 660, 164, 32),""+ Score[6] );
GUI.contentColor = Color.white;
GUI.Label( Rect( Screen.width/2+60, 300, 164, 32),Name[0] );
・・・・・・・・・・・・・・・・・・・・・・・・・・
GUI.Label( Rect( Screen.width/2+60, 660, 164, 32),Name[6] );
GUI.contentColor = Color.white;
GUI.Label( Rect( Screen.width/2+240, 300, 196, 32),Class[0]);
・・・・・・・・・・・・・・・・・・・・・・・・・・
GUI.Label( Rect( Screen.width/2+240, 660, 196, 32),Class[6]);
}
簡単に書いておきます。
だいたいPlayerPrefsを使用するとデータを外部に保持できるということは以前おはなししましたので
どうやって使用するか。
おおまかな流れとしては
◯ランキングの初期化
↓
◯ネームエントリー
↓
◯ランキングのソート
↓
◯ランキングの表示
これらがあればスタンドアローンでの表示は大丈夫だと思います。
はじめは2分木ソートなどの関数を使用していたのですが、ランク値の個数が少ないのと デバッグが大変になったので、 簡単にまとめてみた感じです。
急ごしらえでバグあるかも知れないですけど なおしたらこっそりおしえてくださいw
PHP等を使用したネットランキングの場合は またちょっとややこしいので今回は割愛します。
あと どこかにキーを削除する関数を設けて ランキングの初期化をしたほうがデバッグ時にも重宝しますので
挑戦してみてください。
値を選択して消去する場合は PlayerPrefs.DeleteKey ("キーの名前”);
値をすべて消去する場合 PlayerPrefs.DeleteAll
■ランキングの初期化
private var Score:int[]=[100000,80000,60000,50000,40000,30000,20000];
private var Name:String[]=["AAA","BBB","CCC","DDD","EEE","FFF","GGG"];
private var Class:String[]=["FIGHTER","FIGHTER","FIGHTER","FIGHTER","FIGHTER","FIGHTER","FIGHTER"];
まずはじめにダミーデータを用意してランキングを初期化しておきます。
function Awake(){
if(PlayerPrefs.GetInt("Score1") == null) SetData();
Score1という値が入っているか(初回起動かどうか)をチェックして 値が入っていなければ
初期化の関数『SetData()』を呼びます。
初回起動チェックなので なにかフラグを値として入れておいても構いません
}
function SetData()
{
PlayerPrefsを使ってレジストリにスコアの初期値を書き込みます
ランキングに表示される個数分の値を記述します。
PlayerPrefs.SetInt("Score1",Score[0]);
PlayerPrefs.SetString("Name1", Name[0]);
PlayerPrefs.SetString("Class1", Class[0]);
PlayerPrefs.SetInt("Score2",Score[1]);
PlayerPrefs.SetString("Name2", Name[1]);
PlayerPrefs.SetString("Class2", Class[1]);
・・・・・・
}
■ネームエントリー
function Awake(){
var playerNameInput:String = "AAA";
var ScoreTmp:int = PlayerPrefs.GetInt("Score7");
if(ScoreTmp <= Total_Score )
{
requirePlayerName=true;
}else { Application.LoadLevel("Ranking");}
一番最下位のランキングスコアとゲーム終了時のスコアを比較して条件が合えば
ネームエントリーのテキストフィールドを表示
それ以外の場合『Ranking』のシーンを読み込み
}
function OnGUI(){
if(gSkin)
GUI.skin = gSkin;
else
Debug.Log("StartMenuGUI : GUI Skin object missing!");
if(requirePlayerName){
myWindowRect = GUILayout.Window (9, Rect(Screen.width/2-150,Screen.height/2-100,300,100), EntryNameMenu, "enter name");
}
}
function EntryNameMenu(id : int){
GUILayout.BeginVertical();
GUILayout.Space(10);
GUILayout.BeginHorizontal();
GUILayout.Space(10);
playerNameInput = GUILayout.TextField(playerNameInput);
GUILayout.Space(10);
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Space(10);
if(playerNameInput.length>=1){
if(GUILayout.Button("entry")){
requirePlayerName=false;
PlayerPrefs.SetString("playerName", playerNameInput);
Application.LoadLevel("Ranking");
}
}else{
GUILayout.Label("Enter name to continue");
}
GUILayout.Space(10);
GUILayout.EndHorizontal();
GUILayout.EndVertical();
}
■ランキングのソート
function RankDataSet()
{
var DATA_SIZE:int;
var insertData:int;
Score[0] = PlayerPrefs.GetInt("Score1");
Name[0] = PlayerPrefs.GetString("Name1");
Class[0] = PlayerPrefs.GetString("Class1");
Score[1] = PlayerPrefs.GetInt("Score2");
Name[1] = PlayerPrefs.GetString("Name2");
Class[1] =PlayerPrefs.GetString("Class2");
.........................
スコアの表示数分記述
DATA_SIZE =Score.length;
insertData = Total_Score ;
『Total_Scoreはゲーム終了時のスコアでソートのキーになります』
var min:int;
for(var i= DATA_SIZE-1;i>=0;i--){
if (Score[i] < insertData) min=i;
}
for(var ii= DATA_SIZE-1;ii>min;ii--){
Score[ii]=Score[ii-1];
Name[ii]=Name[ii-1];
Class[ii]=Class[ii-1];
}
Score[min] = insertData;
Name[min] = PlayerPrefs.GetString("playerName");
Class[min] = ClassDataSet();
1:スコアとインサートデータ(今回のゲームの終了時のスコア)を比較して
スコアのデータ数分だけのソートを行います
変数『min』には比較結果のランクが入ります。
2:for文でminの位置までひとつづつランクデータを下にずらします
3:minのランクにインサートデータとネームエントリーで入力された名前をセットします
4:PlayerPlefsをつかってソート終了のデータをレジストリに(スタンドアローンの場合)書き込みます
PlayerPrefs.SetInt("Score1",Score[0]);
PlayerPrefs.SetString("Name1",Name[0]);
PlayerPrefs.SetString("Class1",Class[0]);
PlayerPrefs.SetInt("Score2",Score[1]);
PlayerPrefs.SetString("Name2",Name[1]);
PlayerPrefs.SetString("Class2",Class[1]);
スコア表示数分記述・・・・・・・・・・・・・・・
return ;
}
■ランキングの表示
function OnGUI () {
if(gSkin)GUI.skin = gSkin;
else
Debug.Log("StartMenuGUI : GUI Skin object missing!");
GUI.contentColor = Color (0.75, 0.0 ,0.0);
var backgroundStyle : GUIStyle = new GUIStyle();
backgroundStyle.normal.background = backdrop;
GUILayout.Label ("Press Anykey to Gamestart");
isLoading = true;
if (Input.anyKeyDown) Application.LoadLevel(0);
GUI.Label( Rect( Screen.width/2-340, 240, 128, 32),"RANK" );
GUI.Label( Rect( Screen.width/2-164, 240, 164, 32),"SCORE" );
GUI.Label( Rect( Screen.width/2+60, 240, 164, 32),"NAME" );
GUI.Label( Rect( Screen.width/2+240,240, 164, 32),"CLASS" );
GUI.Label( Rect( Screen.width/2-340, 300, 128, 32),"1ST" );
GUI.Label( Rect( Screen.width/2-340, 360, 128, 32),"2ND");
GUI.Label( Rect( Screen.width/2-340, 420, 128, 32),"3RD" );
GUI.Label( Rect( Screen.width/2-340, 480, 128, 32),"4TH");
GUI.Label( Rect( Screen.width/2-340, 540, 128, 32),"5TH");
GUI.Label( Rect( Screen.width/2-340, 600, 128, 32),"6TH");
GUI.Label( Rect( Screen.width/2-340, 660, 128, 32),"7TH");
GUI.contentColor = Color.white;
GUI.Label( Rect( Screen.width/2-164, 300, 164, 32),""+ Score[0]);
・・・・・・・・・・・・・・・・・・・・・・・・・・
GUI.Label( Rect( Screen.width/2-164, 660, 164, 32),""+ Score[6] );
GUI.contentColor = Color.white;
GUI.Label( Rect( Screen.width/2+60, 300, 164, 32),Name[0] );
・・・・・・・・・・・・・・・・・・・・・・・・・・
GUI.Label( Rect( Screen.width/2+60, 660, 164, 32),Name[6] );
GUI.contentColor = Color.white;
GUI.Label( Rect( Screen.width/2+240, 300, 196, 32),Class[0]);
・・・・・・・・・・・・・・・・・・・・・・・・・・
GUI.Label( Rect( Screen.width/2+240, 660, 196, 32),Class[6]);
}