블로그 이미지
Every unexpected event is a path to learning for you.

카테고리

분류 전체보기 (2731)
Unity3D (814)
Programming (474)
Server (33)
Unreal (4)
Gamebryo (56)
Tip & Tech (228)
협업 (57)
3DS Max (3)
Game (12)
Utility (136)
Etc (96)
Link (32)
Portfolio (19)
Subject (90)
iOS,OSX (51)
Android (14)
Linux (5)
잉여 프로젝트 (2)
게임이야기 (3)
Memories (20)
Interest (38)
Thinking (38)
한글 (30)
PaperCraft (5)
Animation (408)
Wallpaper (2)
재테크 (18)
Exercise (3)
나만의 맛집 (3)
냥이 (10)
육아 (16)
Total
Today
Yesterday
03-30 00:00

[참조] https://mentum.tistory.com/150

 

유니티 퍼미션 체크 적용기. (Unity Permission Check) 2019.11.14 재작성.

2019.11.14 재작성. 유니티 안드로이드에서 스크린샷을 저장하기 위해 권한 부분을 다시 작성해야해서 하는 김에 이 글도 재작성 하였다. 2019/11/14 - [Unity/프로그래밍] - 유니티 안드로이드 스크린샷

mentum.tistory.com

 

 

참조 사이트에 포스팅 된 내용을 참고해서 나한테 맞게 만듬.

(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();
    }
}

 

반응형
Posted by blueasa
, |

[링크]

https://mentum.tistory.com/150

 

유니티 퍼미션 체크 적용기. (Unity Permission Check)

2019.02.12 다른방식으로 포스트 재 작성 [주의] OBB를 사용하는 Split 빌드의 경우 반드시 저장소 권한을 획득해야함. [주의] 유니티 2018.3 부터 퍼미션체크가 내장되었습니다. 2018.3부터는 플러그인 필요없습..

mentum.tistory.com

 

반응형
Posted by blueasa
, |

Using this method you can see if you are able to view the page you wanted or not by accessing the html of that page you can read it to see if you got redirected or not. this example uses www.google.com as it's check

  1. using System.Net;
  2. public string GetHtmlFromUri(string resource)
  3. {
  4. string html = string.Empty;
  5. HttpWebRequest req = (HttpWebRequest)WebRequest.Create(resource);
  6. try
  7. {
  8. using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
  9. {
  10. bool isSuccess = (int)resp.StatusCode < 299 && (int)resp.StatusCode >= 200;
  11. if (isSuccess)
  12. {
  13. using (StreamReader reader = new StreamReader(resp.GetResponseStream()))
  14. {
  15. //We are limiting the array to 80 so we don't have
  16. //to parse the entire html document feel free to
  17. //adjust (probably stay under 300)
  18. char[] cs = new char[80];
  19. reader.Read(cs, 0, cs.Length);
  20. foreach(char ch in cs)
  21. {
  22. html +=ch;
  23. }
  24. }
  25. }
  26. }
  27. }
  28. catch
  29. {
  30. return "";
  31. }
  32. return html;
  33. }

This is to call the function

  1. using System.Net;
  2. void Start()
  3. {
  4. string HtmlText = GetHtmlFromUri("http://google.com");
  5. if(HtmlText == "")
  6. {
  7. //No connection
  8. }
  9. else if(!HtmlText.Contains("schema.org/WebPage"))
  10. {
  11. //Redirecting since the beginning of googles html contains that
  12. //phrase and it was not found
  13. }
  14. else
  15. {
  16. //success
  17. }
  18. }



[출처] https://answers.unity.com/questions/567497/how-to-100-check-internet-availability.html

반응형
Posted by blueasa
, |