Unity 请求安卓权限并拿到回调

最近室友想要在Unity中获取安卓权限相关的回调,比如摄像头权限是否允许等等。点击完之后返回允许,禁止等等。我来帮忙实现一下。
经过调研发现,在2020版本的Unity是自带回调功能的。所以在2020大家可以直接调用Unity相关的API得到权限回调不用打jar包,直接用C#脚本即可实现,实现代码如下

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestPremission : MonoBehaviour
{
    public void CallAndroid()
    {
        AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPermissions");

        AndroidJavaClass jc1 = new AndroidJavaClass("com.unity3d.player.UnityPlayer");

        AndroidJavaObject jo = jc1.GetStatic<AndroidJavaObject>("currentActivity");

        jc.CallStatic("requestUserPermissions", jo , new string[] { "android.permission.CAMERA" }, new IPermissionRequestCallbacks());
    }
}

public class IPermissionRequestCallbacks : AndroidJavaProxy
{
    public IPermissionRequestCallbacks() : base("com.unity3d.player.IPermissionRequestCallbacks")
    {
/*  */
    }

    public virtual void onPermissionGranted(string var1) 
    {
        Debug.Log("111111111"+var1);
    }

    public virtual void onPermissionDenied(string var1)
    {
        Debug.Log("22222222222" + var1);
    }

    public virtual void onPermissionDeniedAndDontAskAgain(string var1)
    {
        Debug.Log("333333333333"+var1);
    }
}

然后可惜室友用的版本是Unity2019,所以我打开了2020的jar包,看了权限相关的代码,将其拷贝出来单独打了个jar包。
这里由于讲的不是打jar包问题,关于打jar包相关的问题请自行了解一下。

我这边代码是从unity的jar包里复制的,Unity的jar包代码经过混淆,可能变量命名比较混乱,由于我做的只是demo,所以就不做整理。你们要使用的话变量名请自己整理下。

代码如下:

安卓相关代码:

先封装一个fragment获取来获取unity activity的回调

package com.device.Permission;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;


public final class g extends Fragment {

    private final IPermissionRequestCallbacks a;
    private final Activity b;
    private final Looper c;

    public g() {
        this.a = null;
        this.b = null;
        this.c = null;
    }

    public g(Activity var1, IPermissionRequestCallbacks var2) {
        this.a = var2;
        this.b = var1;
        this.c = Looper.myLooper();
    }

    public final void onCreate(Bundle var1) {
        super.onCreate(var1);
        String[] var2 = this.getArguments().getStringArray("PermissionNames");
        this.requestPermissions(var2, 96489);
    }

    public final void onRequestPermissionsResult(int var1, String[] var2, int[] var3) {
        if (var1 == 96489) {
            if (var2.length == 0) {
                String[] var6 = this.getArguments().getStringArray("PermissionNames");
                this.requestPermissions(var6, 96489);
            } else {
                for(var1 = 0; var1 < var2.length && var1 < var3.length; ++var1) {
                    if (this.a != null && this.b != null && this.c != null) {
                        String var4 = var2[var1] == null ? "<null>" : var2[var1];
                        (new Handler(this.c)).post(new com.device.Permission.g.a(this.a, var4, var3[var1], this.b.shouldShowRequestPermissionRationale(var4)));
                    }
                }

                FragmentTransaction var5;
                (var5 = this.getActivity().getFragmentManager().beginTransaction()).remove(this);
                var5.commit();
            }
        }
    }

    class a implements Runnable {
        private IPermissionRequestCallbacks b;
        private String c;
        private int d;
        private boolean e;

        a(IPermissionRequestCallbacks var2, String var3, int var4, boolean var5) {
            this.b = var2;
            this.c = var3;
            this.d = var4;
            this.e = var5;
        }

        public final void run() {
            if (this.d == -1) {
                if (this.e) {
                    this.b.onPermissionDenied(this.c);
                } else {
                    this.b.onPermissionDeniedAndDontAskAgain(this.c);
                }
            } else {
                if (this.d == 0) {
                    this.b.onPermissionGranted(this.c);
                }

            }
        }
    }
}

请求权限方法供Unity调用

package com.device.Permission;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;

public class AndroidPermission {

    public static void requestUserPermissions(Activity var0, String[] var1, com.device.Permission.IPermissionRequestCallbacks var2) {

        if (var0 != null && var1 != null) {
            FragmentManager var3 = var0.getFragmentManager();
            String var4 = "96489";
            if (var3.findFragmentByTag(var4) == null) {
                com.device.Permission.g var5 = new com.device.Permission.g(var0, var2);
                Bundle var7;
                (var7 = new Bundle()).putStringArray("PermissionNames", var1);
                var5.setArguments(var7);
                FragmentTransaction var6;
                (var6 = var3.beginTransaction()).add(0, var5, var4);
                var6.commit();
            }

        }
    }
}

权限回调接口

package com.device.Permission;

public interface IPermissionRequestCallbacks {

    void onPermissionGranted(String var1);

    void onPermissionDenied(String var1);

    void onPermissionDeniedAndDontAskAgain(String var1);
}

然后打jar包,导入至Unity的Plugin目录下,加上一个权限相关的AndroidMainfset.xml文件即可

Unity相关代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestPremission : MonoBehaviour
{
    public void CallAndroid()
    {
        //获取名为UnityPlayer的类
        AndroidJavaClass jc = new AndroidJavaClass("com.device.Permission.AndroidPermission");

        AndroidJavaClass jc1 = new AndroidJavaClass("com.unity3d.player.UnityPlayer");

        AndroidJavaObject jo = jc1.GetStatic<AndroidJavaObject>("currentActivity");

        jc.CallStatic("requestUserPermissions", jo , new string[] { "android.permission.CAMERA" }, new IPermissionRequestCallbacks());
    }
}

public class IPermissionRequestCallbacks : AndroidJavaProxy
{
    public IPermissionRequestCallbacks() : base("com.device.Permission.IPermissionRequestCallbacks")
    {

    }

    public virtual void onPermissionGranted(string var1) 
    {
        Debug.Log("111111111"+var1);
    }

    public virtual void onPermissionDenied(string var1)
    {
        Debug.Log("22222222222" + var1);
    }

    public virtual void onPermissionDeniedAndDontAskAgain(string var1)
    {
        Debug.Log("333333333333"+var1);
    }
}

注意
在这里插入图片描述
这里的必须和jar包路径一致,否则会报参数错误的问题

打包Apk测试

将TestPremission 脚本的CallAndroid方法绑定至按钮调用即可

工程地址: Github

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值