'How can I get Time.time as a fixed integer after an event happens in Unity
I want to display on the screen that the player has achieved their goals in this much time after playing the game. But when I display Time.time it keeps changing. I want to record the time elapsed when the event happens and display it as a fixed number string.
Solution 1:[1]
When the player achieves the goal, store Time.time in a class variable (i.e. declared inside the class but outside of any function). Then display that variable rather than Time.time.
Solution 2:[2]
Do you want to record the result of the game? If so, you can try to create a new canvas in the scene, and create a new image in the canvas as the background image. You can set the relevant properties of the image through the Inspector interface.
After setting the background, add a Button to the scene, and set the different states of the Button when it is pressed, lifted, and disabled according to personal needs. And add a C# script file under canvas - StartMenuController
The start interface code is:
public void OnStartGameButtonClick()
{
//When the button is pressed, jump to the game interface
SceneManager.LoadScene(1);
//The parameter in the LoadScene() function can be the number of interfaces in builtsettings or the scene name
}
The end interface code is:
public void OnExitButtonClick()
{
Application.Quit();
}
Point each function in the canvas to the onClick of the respective button.Hope it helps you thank you
Solution 3:[3]
Access the data generated by the player, and then read it out. Does this idea help you? Create a new C# code in Unity, copy the following code into it and mount it on an empty object. The following code is divided into two methods: SaveGame (save data) and LodeGame (read data). You can create two Buttons in the game and create click events respectively.
public Inventory myInventory;//Data to be saved
public Item HpMedicine;//Data to be saved
public void SaveGame() //Save game method
{
Debug.Log(Application.persistentDataPath);
if (!Directory.Exists(Application.persistentDataPath + "/game_SaveGame"))
{
Directory.CreateDirectory(Application.persistentDataPath + "/game_SaveGame");
}
BinaryFormatter formatter = new BinaryFormatter();//Binary conversion
FileStream file = File.Create(Application.persistentDataPath + "/game_SaveGame/Inventory.txt");
var json0 = JsonUtility.ToJson(myInventory);//Data Json conversion to be saved
var json1 = JsonUtility.ToJson(HpMedicine);//Data Json conversion to be saved
var json2 = JsonUtility.ToJson(MpMedicine);//Data Json conversion to be saved
formatter.Serialize(file, json0);//Saved data
formatter.Serialize(file, json1);//Saved data
formatter.Serialize(file, json2);//Saved data
file.Close();
}
public void LoadGame()//Read the game method
{
BinaryFormatter bf = new BinaryFormatter();
if (File.Exists(Application.persistentDataPath + "/game_SaveGame/Inventory.txt"))
{
FileStream file = File.Open(Application.persistentDataPath + "/game_SaveGame/Inventory.txt", FileMode.Open);
JsonUtility.FromJsonOverwrite((string)bf.Deserialize(file), myInventory);//Saved data
JsonUtility.FromJsonOverwrite((string)bf.Deserialize(file), HpMedicine);//Saved data
JsonUtility.FromJsonOverwrite((string)bf.Deserialize(file), MpMedicine);//Saved data
file.Close();
}
}
Solution 4:[4]
I hope I misunderstood your question because as I understand it, this is the answer.
just save Time.time into some varaible on event.
// for example..
public float attackTime;
public void OnAttack() => attackTime = Time.time; // now use attackTime...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Sven Viking |
Solution 2 | Housheng-MSFT |
Solution 3 | Housheng-MSFT |
Solution 4 | KiynL |