1. 程式人生 > >Unity-手機遊戲開發全屏實現

Unity-手機遊戲開發全屏實現

在用untiy開發應用時候發現一個問題,有幾款手機有工具欄,不能實現全屏,


這個手機截圖就是有工具欄的華為手機,我們怎麼才能在自己的app中實現全屏。

public class HideAndroidButtons : MonoBehaviour
{
	#if UNITY_ANDROID && !UNITY_EDITOR
	const int SYSTEM_UI_FLAG_IMMERSIVE_STICKY = 4096;
	const int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 2;
	const int SYSTEM_UI_FLAG_FULLSCREEN = 4;
	
	AndroidJavaObject decorView;
	
	void Start()
	{
		AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
		AndroidJavaObject activity = jc.GetStatic<AndroidJavaObject>("currentActivity");
		AndroidJavaObject window = activity.Call<AndroidJavaObject>("getWindow");
		decorView = window.Call<AndroidJavaObject>("getDecorView");
		
		TurnImmersiveModeOn();
	}
	void OnApplicationFocus(bool focusStatus) {
		if(focusStatus){
			TurnImmersiveModeOn();
		}
	}
	void TurnImmersiveModeOn()
	{
		decorView.Call("setSystemUiVisibility", SYSTEM_UI_FLAG_FULLSCREEN | SYSTEM_UI_FLAG_HIDE_NAVIGATION | SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
	}
	void OnDestroy()
	{
		decorView.Dispose();
	}
	#endif
}



全屏效果