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注冊表編輯器中進行刪除...