Skip to content

Commit 4a9f2bc

Browse files
committed
v1.0.5
Updated OpenCVForUnity version to 2.3.4. Updated NatCorder version to 1.5.
1 parent 036109e commit 4a9f2bc

File tree

5 files changed

+176
-64
lines changed

5 files changed

+176
-64
lines changed

Assets/NatCorderWithOpenCVForUnityExample/NatCorderWithOpenCVForUnityExample.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,14 @@ void Awake ()
2020
}
2121

2222
// Use this for initialization
23-
void Start ()
23+
IEnumerator Start ()
2424
{
25+
#if UNITY_ANDROID && !UNITY_EDITOR
26+
yield return RequestAndroidPermission ("android.permission.WRITE_EXTERNAL_STORAGE");
27+
yield return RequestAndroidPermission ("android.permission.CAMERA");
28+
yield return RequestAndroidPermission ("android.permission.RECORD_AUDIO");
29+
#endif
30+
2531
exampleTitle.text = "NatCorderWithOpenCVForUnity Example " + Application.version;
2632

2733
versionInfo.text = OpenCVForUnity.CoreModule.Core.NATIVE_LIBRARY_NAME + " " + OpenCVForUnity.UnityUtils.Utils.getVersion () + " (" + OpenCVForUnity.CoreModule.Core.VERSION + ")";
@@ -47,15 +53,31 @@ void Start ()
4753
#endif
4854
versionInfo.text += " ";
4955
#if ENABLE_MONO
50-
versionInfo.text += "Mono";
56+
versionInfo.text += "Mono";
5157
#elif ENABLE_IL2CPP
5258
versionInfo.text += "IL2CPP";
5359
#elif ENABLE_DOTNET
5460
versionInfo.text += ".NET";
5561
#endif
5662

5763
scrollRect.verticalNormalizedPosition = verticalNormalizedPosition;
64+
65+
yield return null;
66+
}
67+
68+
#if UNITY_ANDROID && !UNITY_EDITOR
69+
private IEnumerator RequestAndroidPermission(string permission)
70+
{
71+
if (!RuntimePermissionHelper.HasPermission(permission))
72+
{
73+
if (RuntimePermissionHelper.ShouldShowRequestPermissionRationale(permission))
74+
{
75+
RuntimePermissionHelper.RequestPermission(new string[] { permission });
76+
}
77+
}
78+
yield return new WaitForSeconds(0.5f);
5879
}
80+
#endif
5981

6082
// Update is called once per frame
6183
void Update ()
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace NatCorderWithOpenCVForUnityExample
6+
{
7+
public class RuntimePermissionHelper
8+
{
9+
private RuntimePermissionHelper ()
10+
{
11+
}
12+
13+
private static AndroidJavaObject GetActivity ()
14+
{
15+
using (var UnityPlayer = new AndroidJavaClass ("com.unity3d.player.UnityPlayer")) {
16+
return UnityPlayer.GetStatic<AndroidJavaObject> ("currentActivity");
17+
}
18+
}
19+
20+
private static bool IsAndroidMOrGreater ()
21+
{
22+
using (var VERSION = new AndroidJavaClass ("android.os.Build$VERSION")) {
23+
return VERSION.GetStatic<int> ("SDK_INT") >= 23;
24+
}
25+
}
26+
27+
public static bool HasPermission (string permission)
28+
{
29+
if (IsAndroidMOrGreater ()) {
30+
using (var activity = GetActivity ()) {
31+
return activity.Call<int> ("checkSelfPermission", permission) == 0;
32+
}
33+
}
34+
35+
return true;
36+
}
37+
38+
public static bool ShouldShowRequestPermissionRationale (string permission)
39+
{
40+
if (IsAndroidMOrGreater ()) {
41+
using (var activity = GetActivity ()) {
42+
return activity.Call<bool> ("shouldShowRequestPermissionRationale", permission);
43+
}
44+
}
45+
46+
return false;
47+
}
48+
49+
public static void RequestPermission (string[] permissiions)
50+
{
51+
if (IsAndroidMOrGreater ()) {
52+
using (var activity = GetActivity ()) {
53+
activity.Call ("requestPermissions", permissiions, 0);
54+
}
55+
}
56+
}
57+
}
58+
}

Assets/NatCorderWithOpenCVForUnityExample/Scripts/Utils/RuntimePermissionHelper.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)