Android上的Unity High得分保存

时间:2016-06-27 15:09:05

标签: c# android unity3d unityscript unity5

我的当前和高分并没有出现在Android设备

As you can see in picture it shows Score and HighScore in DeathGUI , but when i build it to android it shows 0 and 0

DeathGUI菜单控制器

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class MenuController : MonoBehaviour {

    public Text scoreText;

    // Use this for initialization
    void Start () {
    }

    // Update is called once per frame
    void Update () {

    }

    public void StartGame()
    {
        SceneManager.LoadScene("Name is Hidden but it is Correct");
    }

    public void RestartGame()
    {
        if (GameStateManager.GameState == GameState.Dead)
        {
            GameStateManager.GameState = GameState.Intro;
            SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
        }
    }

    public void ToggleEndMenu(float score)
    {
        gameObject.SetActive(true);
        scoreText.text = ((int)score).ToString("0");
    }
}

球员得分管理器

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class ScoreManager : MonoBehaviour
{


    private float score = 0f;
    public Text Scoretext;
    public MenuController deathmenu;
    void Start()
    {

    }




    void Update()
        {
            score += Time.deltaTime;
            Scoretext.text = ((int)score).ToString();
        }

       public void onDeath()
        {

           if(PlayerPrefs.GetFloat("Highscore") < score)
            PlayerPrefs.SetFloat("Highscore", score);
            deathmenu.ToggleEndMenu(score);
        }

    }


Player Script 



    void Update()
        {
            //handle back key in Windows Phone
            if (Input.GetKeyDown(KeyCode.Escape))
                Application.Quit();

            if (GameStateManager.GameState == GameState.Intro)
            {
                MovePlayerOnYAxis();
                if (WasTouchedOrClicked())
                {
                    IntroGUI.SetActive(false);
                    GameStateManager.GameState = GameState.Playing;
                }
            }

            else if (GameStateManager.GameState == GameState.Playing)
            {
                MovePlayerOnYAxis();
                Canvas.SetActive(true);
                DeathGUI.SetActive(false);


            }
            score += Time.deltaTime;
            Scoretext.text = ((int)score).ToString();
        }


 void PlayerDies()
    {


        GameStateManager.GameState = GameState.Dead;
        DeathGUI.SetActive(true);
        Canvas.SetActive(false);
        GetComponent<AudioSource>().PlayOneShot(DeathAudioClip);

        if (PlayerPrefs.GetFloat("Highscore") < score)
            PlayerPrefs.SetFloat("Highscore", score);
        deathmenu.ToggleEndMenu(score);
    }
}

HighScore Script

HighScore GUI for script

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class HighScoreText : MonoBehaviour {

    public Text HighscoreText;

    // Use this for initialization
    void Start () {
        HighscoreText.text = ((int)PlayerPrefs.GetFloat("Highscore")).ToString();
    }

    // Update is called once per frame
    void Update () {

    }
}

1 个答案:

答案 0 :(得分:0)

如果您在检查器中查看PlayerSettings,您是否将写入权限设置为外部(SDCard)或仅限内部?

还安装位置?

我制作了一个本地高分的游戏,我看到了带有这些值的球员配置的配置部分: 安装位置=首选外部 写访问=外部(SD卡)

这是我的LocalScore.js:

#pragma strict
#pragma implicit

var Score1 : int;
var Name1 : String;


var Score2 : int;
var Name2 : String;


var Score3 : int;
var Name3 : String;


var Score4 : int;
var Name4 : String;


var Score5 : int;
var Name5 : String;

var Score6 : int;
var Name6 : String;


var Score7 : int;
var Name7 : String;


var Score8 : int;
var Name8 : String;


var Score9 : int;
var Name9 : String;


var Score10 : int;
var Name10 : String;





function Start () {

}

function Update () {

}
function OnGUI() {

    if(GUI.Button(Rect(Screen.width / 2 - 150,  Screen.height / 2.5 + 100 , 250 ,50), "Back to Global Scoreboard") ) {
//      Application.Quit();
        Application.LoadLevel("Highscore");

    }

//  UpdateHScore();

//  GUI.Textfield();

}

function UpdateLocalScores(){
//  Score1 = PlayerPrefs.GetInt(HScore0);
//  Name1 = PlayerPrefs.GetString(HScoreName0); 
//  
//  Score2 = PlayerPrefs.GetInt(1HScore);
//  Name2 = PlayerPrefs.GetString(1HScoreName);     
//
//  Score3 = PlayerPrefs.GetInt(2HScore);
//  Name3 = PlayerPrefs.GetString(2HScoreName);     
//
//  Score4 = PlayerPrefs.GetInt(3HScore);
//  Name4 = PlayerPrefs.GetString(3HScoreName);     
//
//  Score5 = PlayerPrefs.GetInt(4HScore);
//  Name5 = PlayerPrefs.GetString(4HScoreName);     
//
//  Score6 = PlayerPrefs.GetInt(5HScore);
//  Name6 = PlayerPrefs.GetString(5HScoreName);     
//
//  Score7 = PlayerPrefs.GetInt(6HScore);
//  Name7 = PlayerPrefs.GetString(6HScoreName);     
//
//  Score8 = PlayerPrefs.GetInt(7HScore);
//  Name8 = PlayerPrefs.GetString(7HScoreName);     
//
//  Score9 = PlayerPrefs.GetInt(8HScore);
//  Name9 = PlayerPrefs.GetString(8HScoreName);     
//
//  Score10 = PlayerPrefs.GetInt(9HScore);
//  Name10 = PlayerPrefs.GetString(9HScoreName);        



}