[Android] 유니티 퍼미션 체크(Unity Permission Check)
Unity3D/Android / 2020. 7. 7. 17:48
[참조] https://mentum.tistory.com/150
참조 사이트에 포스팅 된 내용을 참고해서 나한테 맞게 만듬.
(Unity2018 / 저장공간(선택) 권한만 필요)
using System;
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
using UnityEngine.Assertions;
using UnityEngine.Android;
public class UICheckPermissionManagerSGT : MonoBehaviour
{
public GameObject m_panelCheckPermission = null;
public GameObject m_panelDeniedConfirm = null;
private bool m_bOnCheckPermission = false;
void Start()
{
Debug.Log("[Scene] CheckPermission");
InitCheckPermission();
CheckCountryCode_KR();
}
void InitCheckPermission()
{
m_bOnCheckPermission = false;
ActivateCheckPermission(false);
ActivateDeniedConfirm(false);
}
void CheckCountryCode_KR()
{
// GetIP가 한국인지 체크하는 외부 함수
SGT.Global.CheckCountryCode_KR(ConfirmCountryCode_KR);
}
void ConfirmCountryCode_KR(bool _bIsCountryCode_KR)
{
if (true == _bIsCountryCode_KR)
{
// 한국 : Permission 확인
DoCheckPermission();
//CallPermission();
}
else
{
// No 한국 : Next Scene으로 이동
GoToNextScene();
}
}
void DoCheckPermission()
{
// 저장공간(Write) 권한 체크(선택 권한)
if (Permission.HasUserAuthorizedPermission(Permission.ExternalStorageWrite) == false)
{
// 지정된 권한이 없으면 CheckPermission UI 활성화
ActivateCheckPermission(true);
}
else
{
GoToNextScene();
}
}
IEnumerator CheckPermissionCoroutine()
{
m_bOnCheckPermission = true;
yield return new WaitForEndOfFrame();
// 저장공간(Write) 권한 체크(선택 권한)
if (Permission.HasUserAuthorizedPermission(Permission.ExternalStorageWrite) == false)
{
// 권한 요청
Permission.RequestUserPermission(Permission.ExternalStorageWrite);
yield return new WaitForSeconds(0.2f); // 0.2초의 딜레이 후 focus를 체크하자.
yield return new WaitUntil(() => Application.isFocused == true);
if (Permission.HasUserAuthorizedPermission(Permission.ExternalStorageWrite) == false)
{
// 권한 거절하면 안내 팝업
OnEventDenied();
yield break;
}
}
// 권한이 있으면 다음 Scene으로 이동
GoToNextScene();
m_bOnCheckPermission = false;
}
// 해당 앱의 설정창을 호출한다.(권한 설정할 수 있도록 유도)
// https://forum.unity.com/threads/redirect-to-app-settings.461140/
private void OpenAppSetting()
{
try
{
#if UNITY_ANDROID
using (var unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
using (AndroidJavaObject currentActivityObject = unityClass.GetStatic<AndroidJavaObject>("currentActivity"))
{
string packageName = currentActivityObject.Call<string>("getPackageName");
using (var uriClass = new AndroidJavaClass("android.net.Uri"))
using (AndroidJavaObject uriObject = uriClass.CallStatic<AndroidJavaObject>("fromParts", "package", packageName, null))
using (var intentObject = new AndroidJavaObject("android.content.Intent", "android.settings.APPLICATION_DETAILS_SETTINGS", uriObject))
{
intentObject.Call<AndroidJavaObject>("addCategory", "android.intent.category.DEFAULT");
intentObject.Call<AndroidJavaObject>("setFlags", 0x10000000);
currentActivityObject.Call("startActivity", intentObject);
}
}
#endif
}
catch (Exception ex)
{
Debug.LogException(ex);
}
}
void ActivateDeniedConfirm(bool _bActive)
{
GgUtil.Activate(m_panelDeniedConfirm, _bActive);
}
void ActivateCheckPermission(bool _bActive)
{
GgUtil.Activate(m_panelCheckPermission, _bActive);
}
void GoToNextScene()
{
// 패치씬으로 이동
SGT.Scene.RunCheckPermissionToPatch();
}
//////////////////////////////////////////////////////////////////////////
// Event
//////////////////////////////////////////////////////////////////////////
void OnEventCheckPermission()
{
if (true == m_bOnCheckPermission)
return;
StopCoroutine("CheckPermissionCoroutine");
StartCoroutine("CheckPermissionCoroutine");
}
void OnEventDenied()
{
ActivateDeniedConfirm(true);
}
void OnEventDeniedConfirm()
{
// 저장공간은 선택권한이라 권한요청 거절해도 게임 진행
GoToNextScene();
}
//////////////////////////////////////////////////////////////////////////
// UI Event
//////////////////////////////////////////////////////////////////////////
// 권한 동의 버튼
public void OnUIEventCheckPermission()
{
if (SGT.Global.bPreventDoubleClick)
return;
SGT.Global.PreventDoubleClick();
OnEventCheckPermission();
}
// 거절 확인
public void OnUIEventDeniedConfirm()
{
OnEventDeniedConfirm();
}
// 권한 설정 열기
public void OnUIEventOpenAppSetting()
{
OpenAppSetting();
}
}
반응형