unity文件 PlayerPrefs.SetInt 保存 And PlayerPrefs.GetInt讀取


unity文件保存讀取PlayerPrefs.SetInt   And  PlayerPrefs.GetInt

using UnityEngine;

using System.Collections;

using UnityEngine.UI;

using UnityEngine.SceneManagement;

using Enums;

 

//定義類管理時間靜態變量

public class global_Mgr

{

    //定義初始時間

    public static int GoodTime01 = 366;

    //公共靜態方法、參數string str, int  CurrentTime

    public static void CurrentTimes(string str, int  CurrentTime)

    {       

                  //判斷執行

        switch (str)

        {

            case "Level1":

                {

                    if (CurrentTime < GoodTime01)

                    {

                        GoodTime01 = CurrentTime;

                    }

                                          //保存最佳時間數據

                    PlayerPrefs.SetInt("GoodTimeInt01", GoodTime01);

                }

                break;

            default:  Debug.Log("error");

                break;

        }

    }

}

 

 

using System.Collections;

using UnityEngine;

 

//playercontrol控制到達的時間

public class playercontrol : MonoBehaviour

{

    //場景名稱

    public GameObject Levels=Level01;

    //定義初始時間

    private float times=0;

    //時間累加

    private void Update()

    {

        times += Time.deltaTime;

    }

    //碰撞觸發時結束、記錄時間 

    public void OnTriggerEnter(Collider other)

    {

        //觸發物體

        if (other.name.Equals("Endpoint"))

        {

            int timess = (int)times;

                     //調用global_Mgr中方法

            global_Mgr.CurrentTimes(Levels.name, timess);

        }

    }

}

 

 

using System.Collections;

using UnityEngine.SceneManagement;

using UnityEngine;

 

//主場景

public class MainMenuScene : MonoBehaviour

{

    //場景運行的時候讀取時間

    public void Awake()

    {

         global_Mgr.GoodTime01 = PlayerPrefs.GetInt("GoodTimeInt01");

    }

}

注:unity文件 PlayerPrefs保存 And  PlayerPrefs讀取、用於string、int、float類型,存取數據可在系統regedit注冊表編輯器中進行刪除...


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM