Run the game even when the window is inactive
Verification environment
- Windows
-
- Windows 11
- Unity Editor
-
- 2021.3.3f1
- Input System Packages
-
- 1.3.0
Prerequisites for this tip
The following settings are pre-configured as a prerequisite for the explanation of these tips.
How to make the game work even when the window is inactive
This method does not need to be set programmatically, etc., and can be enabled or disabled in the project settings.
From the Unity Editor menu, select Edit, then select Project Settings.
Make sure that "Player" is selected in the menu on the left, and there is "Run in Background" at the bottom of the page, so you can switch with or without this check.
Check the movement
Display the current time on the screen and see if time advances or stops depending on the settings when you deactivate the window.
Create the UI. It would be nice if you could check if it works, so please make it appropriately.
Create a script. UpdateText
In this case, we'll leave it as .
using System;
using UnityEngine;
using UnityEngine.UI;
public class UpdateText : MonoBehaviour
{
private Text Text;
// Start is called before the first frame update
void Start()
{
Text = GetComponent<Text>();
}
// Update is called once per frame
void Update()
{
Text.text = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
}
}
Attaches the date to the text that you want to update.
Run the game to see if the date and time update successfully.
Verify that activating other windows in this state stops updating the date.
Stop the game and select "Project Settings" from the Edit menu.
Check "Run in background" at the bottom of the "Player" page and close the dialog.
Run the game and see if the date and time update even when other windows are active.