유니티 OnApplicationFocus - yuniti OnApplicationFocus

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Your name Your email Suggestion*

Cancel

Switch to Manual

Parameters

hasFocus True if the GameObjects have focus, else False.

Description

Sent to all GameObjects when the player gets or loses focus.

OnApplicationFocus is called when the application loses or gains focus. Alt-tabbing or Cmd-tabbing can take focus away from the Unity application to another desktop application. This causes the GameObjects to receive an OnApplicationFocus call with the argument set to false. When the user switches back to the Unity application, the GameObjects receive an OnApplicationFocus call with the argument set to true.

OnApplicationFocus can be a co-routine; to do this, use the yield statement in the function. Implemented this way, it is evaluated twice during the initial frame: first as an early notification, and secondly during the normal co-routine update step.

On Android, when the on-screen keyboard is enabled, it causes an OnApplicationFocus( false ) event. If you press Home when the keyboard is enabled, the OnApplicationPause() event is called instead of the OnApplicationFocus() event.

Note: If the Editor is in Play mode, OnApplicationFocus is called when the Game view loses or gains focus. If an external application (meaning an application other than Unity) has focus, and you click a different Editor tab, ::OnApplicationFocus is called twice in one frame. The first time, OnApplicationFocus is called with hasFocus set to true because the Game view regains focus when Unity regains focus. The second time, OnApplicationFocus is called with hasFocus set to false because the Game view loses focus to the Editor tab that was clicked.

To minimize the number of times OnApplicationFocus is called when the Editor is in Play mode, and you are using the rest of the Editor, drag the Game view into a floating window.

using UnityEngine;

public class AppPaused : MonoBehaviour { bool isPaused = false;

void OnGUI() { if (isPaused) GUI.Label(new Rect(100, 100, 50, 30), "Game paused"); }

void OnApplicationFocus(bool hasFocus) { isPaused = !hasFocus; }

void OnApplicationPause(bool pauseStatus) { isPaused = pauseStatus; } }

디바이스에서 게임을 직접적으로 종료하지 않고 이탈/복귀하는 경우 OnApplicationFocus(bool value) OnApplicationPause(bool value) 실행된다. 함수 차이는 다음과 같다.

게임이 처음 실행될

  • OnApplicationFocus(true)가 호출된다. (동일 스크립트 상의 Awake() 실행 이후, Start() 실행 전에 호출) 

플레이 도중 게임을 이탈했을 때(홈버튼을 누르거나 다른 앱을 사용하게 되었을 때)

  • OnApplicationFocus(false)가 호출된다.

  • OnApplicationPause(true)가 호출된다. 

이탈했던 게임에 다시 복귀했을 때

  • OnApplicationFocus(true)가 호출된다.

  • OnApplicationPause(false)가 호출된다.

https://docs.unity3d.com/2022.1/Documentation/ScriptReference/MonoBehaviour.OnApplicationFocus.html

Unity - Scripting API: MonoBehaviour.OnApplicationFocus(bool)

OnApplicationFocus is called when the application loses or gains focus. Alt-tabbing or Cmd-tabbing can take focus away from the Unity application to another desktop application. This causes the GameObjects to receive an OnApplicationFocus call with the arg

docs.unity3d.com

유니티 OnApplicationFocus - yuniti OnApplicationFocus

https://docs.unity3d.com/2022.1/Documentation/ScriptReference/MonoBehaviour.OnApplicationPause.html

Unity - Scripting API: MonoBehaviour.OnApplicationPause(bool)

OnApplicationPause is set to true or false. Normally, false is the value returned by the OnApplicationPause message. This means the game is running normally in the editor. If an editor window such as the Inspector is chosen the game is paused and OnApplica

docs.unity3d.com

유니티 OnApplicationFocus - yuniti OnApplicationFocus

doc 내용 확인

+

https://answers.unity.com/questions/496290/can-somebody-explain-the-onapplicationpausefocus-s.html?childToView=970958#answer-970958 

Can somebody explain the OnApplicationPause/Focus scenarios? - Unity Answers

answers.unity.com

Unity Community Answers에 있는 글 중 친절한 내용

-> 

처음 시작 시 - OnApplicationFocus(true)

이탈 시 - OnApplicationFocus(false), OnApplicationPause(true)

복귀 시 - OnApplicationFocus(true), OnApplicationPause(false)

=>

첫 시작 시 focus가 on이니까 당연히 true로 들어올 것

이탈 시 focus가 out이니까 false로 들어올 것 + pause는 당연히 true 일 것이고.,.,.

아무래도 focus의 경우 시작 시 바로 들어오다 보니 Pause를 더 사용하게 되는 즁

    private void OnApplicationPause(bool pause)
    {
        // 이탈
        if (pause == true)
        {
        	
        }

        // 복귀
        if (pause == false)
        {
        	
        }
    }

대충 이탈, 복귀 시 할 내용 정리해서 사용하는

https://docs.unity3d.com/kr/530/ScriptReference/Application-runInBackground.html

Unity - 스크립팅 API: Application.runInBackground

Success! Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable. 닫기

docs.unity3d.com

runInBackground의 경우 기본 설정이 false인데 OnApplicationPause의 경우 에디터상에서는 요 녀석이 true면 focus가 나가도 계속 플레이하니까 확인할 수 없어서 false로 그대로 두면 되는데 

void 어디든
{
#if UNITY_EDITOR
	Application.runInBackground = 상태;
#endif
}

에이터상에서만 테스트가 필요하면 이런 느낌으로 사용해도 될 듯

* PlayerSettings에서도 변경 가능