第一句子网 - 唯美句子、句子迷、好句子大全
第一句子网 > 【Unity】游戏开发过程中的前后台切换技术

【Unity】游戏开发过程中的前后台切换技术

时间:2019-09-08 21:17:03

相关推荐

【Unity】游戏开发过程中的前后台切换技术

在有些场景中游戏是需要从前台切换到后台运行的,那么在开发过程中需要如何去实现这个功能呢,为了帮助大家,下面就给大家介绍下退后台的方法,不会的就一起来看看吧。

//simulateSwitchToBackground.csusingUnityEngine;usingSystem.Collections;usingSystem.Collections.Generic;publicclass simulateSwitchToBackground : MonoBehaviour {voidsendApplicationPauseMessage(boolisPause){Transform[] transList= GameObject.FindObjectsOfType(); for(inti = 0; i < transList.Length; i++) {Transform trans = transList [i];//Note that messages will not be sent to inactive objectstrans.SendMessage ("OnApplicationPause",isPause,SendMessageOptions.DontRequireReceiver);}}voidsendApplicationFocusMessage(boolisFocus){Transform[] transList= GameObject.FindObjectsOfType(); for(inti = 0; i < transList.Length; i++) {Transform trans = transList [i];//Note that messages will not be sent to inactive objectstrans.SendMessage ("OnApplicationFocus",isFocus,SendMessageOptions.DontRequireReceiver);}}publicvoid sendEnterBackgroundMessage(){sendApplicationPauseMessage (true);sendApplicationFocusMessage (false);}publicvoid sendEnterFoegroundMessage(){sendApplicationFocusMessage (true);sendApplicationPauseMessage (false);}}//simulateSwitchToBackgroundEditor.csusingUnityEngine;usingSystem.Collections;usingUnityEditor;[CustomEditor(typeof(simulateSwitchToBackground))]publicclass simulateSwitchToBackgroundEditor : Editor{voidOnEnable(){}publicoverride void OnInspectorGUI(){DrawDefaultInspector();serializedObject.Update ();serializedObject.ApplyModifiedProperties ();//now varibles in script have been updatedif(GUILayout.Button ("send enter background message")) {if(Application.isPlaying) {((simulateSwitchToBackground)target).sendEnterBackgroundMessage ();}}if(GUILayout.Button ("send enter foeground message")) {if(Application.isPlaying) {((simulateSwitchToBackground)target).sendEnterFoegroundMessage ();}}}}

把simulateSwitchToBackground.cs挂到场景中的一个gameObject上,其inspector面板如下:

在游戏运行过程中点“send endter background message”按钮,即模拟游戏退到后台。再点"send enter foeground message"按钮,模拟游戏从后台切回到前台。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。