Timer 将最佳时间提交到新场景unity3d

Timer 将最佳时间提交到新场景unity3d,timer,unity3d,Timer,Unity3d,计时的是我的计时器。当游戏进入新场景时,是否可能自动插入前一场景的最佳时间?我应该在新场景中插入什么代码以暴露最佳时间 /** * TIMER (JAVASCRIPT) * Copyright (c) gameDev7 * * This is an HH:MM:SS count down/up timer * Includes functions for pausing timer * * TIP: Search where to place your functions * 1

计时的是我的计时器。当游戏进入新场景时,是否可能自动插入前一场景的最佳时间?我应该在新场景中插入什么代码以暴露最佳时间

/**
 * TIMER (JAVASCRIPT)
 * Copyright (c) gameDev7
 *
 * This is an HH:MM:SS count down/up timer
 * Includes functions for pausing timer
 *
 * TIP: Search where to place your functions
 * 1. Press Cntrl/Cmd + F
 * 2a. Type UP for count up timer (CAPS)
 * 2b. Type DOWN for count down timer (CAPS)
 * 3. Check Match whole word only
 * 4. Check Match case
 * 5. Click Find Next
 */

/// INPUT VARIABLES
var timerStyle:GUIStyle;
var countdown:boolean = false; //switches between countdown and countup

var hours:float = 0f;
var minutes:float = 1f;
var seconds:float = 30f;

var printDebug:boolean = false; //for debugging purposes

/// CALCULATION VARIABLES
private var pauseTimer:boolean = false;

private var timer:float = 0f;
private var hrs:float = 0f;
private var min:float = 0f;
private var sec:float = 0f;

/// DISPLAY VARIABLES
private var strHours:String = "00";
private var strMinutes:String = "00";
private var strSeconds:String = "00";

private var strHrs:String = "00";
private var strMin:String = "00";
private var strSec:String = "00";

private var message:String = "Timing...";

// Use this for initialization
//end start

// Update is called once per frame
function Update () {
    if(Input.GetKeyUp("j")) {
        if(pauseTimer) {
            pauseTimer = false;
        } else {
            pauseTimer = true;
        }//end if
        if(printDebug) print("TimerJS - Paused: " + pauseTimer);
    }//end if

    if(pauseTimer) {
        message = "Timer paused";
        Time.timeScale = 0;
    } else {
        //message = "Timer resumed";
        Time.timeScale = 1;
    }//end if

    if(seconds > 59) {
        message = "Seconds must be less than 59!";
        return;
    } else if (minutes > 59) {
        message = "Minutes must be less than 59!";
    } else {
        FindTimer();
    }//end if
}//end update

/* TIMER FUNCTIONS */
//Checks which Timer has been initiated
function FindTimer() {
    if(!countdown) {
        CountUp();
    } else 
    {
        CountDown();
    }//end if
}//end FindTimer

//Timer starts at 00:00:00 and counts up until reaches Time limit
function CountUp() {
    timer += Time.deltaTime;

    if(timer >= 1f) {
        sec++;
        timer = 0f;
    }//end if

    if(sec >= 60) {
        min++;
        sec = 0f;
    }//end if

    if(min >= 60) {
        hrs++;
        min = 0f;
    }//end if

    if(sec >= seconds && min >= minutes && hrs >= hours) {
        sec = seconds;
        min = minutes;
        hrs = hours;
        message = "Time limit reached!";
        if(printDebug) print("TimerJS - Out of time!");
        ///----- TODO: UP -----\\\
    }//end if
}//end countUp

//Timer starts at specified time and counts down until it reaches 00:00:00
function CountDown() {
    timer -= Time.deltaTime;

    if(timer <= -1f) {
        sec--;
        timer = 0f;
    }//end if

    if(hrs <= 0f) {
        hrs = 0f;
    }//end if       

    if(min <= 0f) {
        hrs--;
        min = 59f;
    }//end if

    if(sec <= 0f) {
        min--;
        sec = 59f;
    }//end if

    if(sec <= 0 && min <= 0 && hrs <= 0) {
        sec = 0;
        min = 0;
        hrs = 0;
        message = "Time's Up!";
        if(printDebug) print("TimerJS - Out of time!");
        ///----- TODO: DOWN -----\\\
    }//end if
}//end countDown

function FormatTimer () {
    if(sec < 10) {
        strSec = "0" + sec.ToString();
    } else {
        strSec = sec.ToString();
    }//end if

    if(min < 10) {
        strMin = "0" + min.ToString();
    } else {
        strMin = min.ToString();
    }//end if

    if(hrs < 10) {
        strHrs = "0" + hrs.ToString();
    } else {
        strHrs = hrs.ToString();
    }//end if

    if(seconds < 10) {
        strSeconds = "0" + seconds.ToString();
    } else {
        strSeconds = seconds.ToString();
    }//end if

    if(minutes < 10) {
        strMinutes = "0" + minutes.ToString();
    } else {
        strMinutes = minutes.ToString();
    }//end if

    if(hours < 10) {
        strHours = "0" + hours.ToString();
    } else {
        strHours = hours.ToString();
    }//end if
}//end formatTimer

/* DISPLAY TIMER */
function OnGUI () {

    FormatTimer();
    if(!countdown) {        
        GUI.Label(new Rect(Screen.width/4-155,Screen.height/3-10,200,90), strHrs + ":" + strMin + ":" + strSec + "", timerStyle);
    } //end if
}//end onGui

/* GETTERS & SETTERS */
public function GetPaused() { return pauseTimer; }
public function SetPaused(val:boolean) { pauseTimer = val; }

public function GetSec() { return sec; }
public function GetMin() { return min; }
public function GetHrs() { return hrs; }

public function GetMessage() { return message; }
public function SetMessage(val:String) { message = val; }

将您的最佳时间保存为计时器脚本中的静态变量或您命名的任何变量:static var bestTime=01:32:12


然后,您可以从任何其他脚本(例如在不同场景中使用的脚本)全局访问它,如:Timer.bestTime。

您可以将时间保存为静态变量,使其在场景中可用。或者,您可以使用DontDestroyOnLoadgameObject保留包含该脚本的游戏对象,这意味着该游戏对象及其附加组件的所有值将保留其值。请小心使用第二种方法,因为如果返回到最初具有对象的场景,则会有重复的对象。我认为在这种情况下,最好的解决方案是只使用静态变量,并在所有场景中使用它。

您能将代码缩写为重现错误所需的代码吗?你说的前一场戏的最佳时间是什么意思?