Unity模拟切后台与返回前台

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EditorBackRunTest : MonoBehaviour
{
	void Update()
	{
		if (Input.GetKeyDown(KeyCode.Alpha1))
			SendMessageTest(true);
		else if (Input.GetKeyDown(KeyCode.Alpha2))
			SendMessageTest(false);
	}
	private void OnApplicationForcus(bool isForcus)
	{
		if (isForcus)
		{
			Debug.Log("isForcus:true");
		}
		else
		{
			Debug.Log("isForcus:false");
		}
	}
	private void OnApplicationPause(bool isPause)
	{
		if (isPause)
		{
			Debug.Log("isPause:true");
			Time.timeScale = 0;
			AudioListener.pause = true;

		}
		else
		{
			Debug.Log("isPause:false");
			Time.timeScale = 1;
			AudioListener.pause = false;
		}
	}
	void SendPause(bool isPause)
	{
		transform.SendMessage("OnApplicationPause", isPause, SendMessageOptions.DontRequireReceiver);
	}
	void SendForcus(bool isForcus)
	{
		transform.SendMessage("OnApplicationForcus", isForcus, SendMessageOptions.DontRequireReceiver);
	}
	public void SendMessageTest(bool pause)
	{
		if (pause)
		{
			SendPause(true);
			SendForcus(false);
		}
		else
		{
			SendPause(false);
			SendForcus(true);
		}
	}
}

你可能感兴趣的:(Unity,unity)