Unity游戏开发移动端 ,麻烦的就是与Android交互,现在就教如何在unity中 传入文件路径调用android选择打开方式来查看一个文件:

首先创建一个android项目

Android 选择文件系统自带 安卓选择文件打开方式_Unity与Android交互


Android 选择文件系统自带 安卓选择文件打开方式_Android 选择文件系统自带_02


Android 选择文件系统自带 安卓选择文件打开方式_Android 选择文件系统自带_03


Android 选择文件系统自带 安卓选择文件打开方式_android_04

进入Android studio 画面如下:

Android 选择文件系统自带 安卓选择文件打开方式_Android_05

首先我们新建一个模块:

Android 选择文件系统自带 安卓选择文件打开方式_Unity与Android交互_06

记得选择 android library

Android 选择文件系统自带 安卓选择文件打开方式_Android 选择文件系统自带_07

Android 选择文件系统自带 安卓选择文件打开方式_ide_08


创建完如下:

Android 选择文件系统自带 安卓选择文件打开方式_android_09

首先我们将unity的jar包导入: 路径位于

G:\unity2017\Editor\Data\PlaybackEngines\AndroidPlayer\Variations\il2cpp\Release\Classes

Android 选择文件系统自带 安卓选择文件打开方式_android_10


这时候虽然jar包放进去 但是 还没加入在这个模块中,我们需要右键点击Add as library添加到模块中

Android 选择文件系统自带 安卓选择文件打开方式_ide_11


添加完 如下:

Android 选择文件系统自带 安卓选择文件打开方式_ide_12


为了确定该模块是否与jar包 产生依赖 我们去查看模块的依赖 右键open modules setting

Android 选择文件系统自带 安卓选择文件打开方式_ide_13


打开看到如下,确认模块已经与jar产生依赖关系了

Android 选择文件系统自带 安卓选择文件打开方式_Android_14

切换一下项目的显示 好进行下一步操作

Android 选择文件系统自带 安卓选择文件打开方式_Unity与Android交互_15


我们现在就创建一个空的activity:

Android 选择文件系统自带 安卓选择文件打开方式_Android_16


点击创建后 直接点finish

Android 选择文件系统自带 安卓选择文件打开方式_android_17


生成后我们要修改一些配置:先将layout下的xml删除掉 因为我们不需要布局

Android 选择文件系统自带 安卓选择文件打开方式_ide_18


删除掉后 我们需要把setContentView(R.layout.activity_main);这行代码也删掉 不然有错误

Android 选择文件系统自带 安卓选择文件打开方式_android_19

接着我们就开始写代码了

最后代码如下:

package com.study.mylibrary;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.unity3d.player.UnityPlayerActivity;

import java.io.File;

public class MainActivity extends UnityPlayerActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
            StrictMode.setVmPolicy(builder.build());
        }
    }


    public void show(String filesPath){
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(Intent.ACTION_VIEW);
        startActivity(openFile(filesPath));
    }

    public static Intent openFile(String filePath){

        File file = new File(filePath);
        if(!file.exists()) return null;
        /* 取得扩展名 */
        String end=file.getName().substring(file.getName().lastIndexOf(".") + 1,file.getName().length()).toLowerCase();
        /* 依扩展名的类型决定MimeType */
        if(end.equals("m4a")||end.equals("mp3")||end.equals("mid")||
                end.equals("xmf")||end.equals("ogg")||end.equals("wav")){
            return getAudioFileIntent(filePath);
        }else if(end.equals("3gp")||end.equals("mp4")){
            return getAudioFileIntent(filePath);
        }else if(end.equals("jpg")||end.equals("gif")||end.equals("png")||
                end.equals("jpeg")||end.equals("bmp")){
            return getImageFileIntent(filePath);
        }else if(end.equals("apk")){
            return getApkFileIntent(filePath);
        }else if(end.equals("ppt")){
            return getPptFileIntent(filePath);
        }else if(end.equals("xls")){
            return getExcelFileIntent(filePath);
        }else if(end.equals("doc")){
            return getWordFileIntent(filePath);
        }else if(end.equals("pdf")){
            return getPdfFileIntent(filePath);
        }else if(end.equals("chm")){
            return getChmFileIntent(filePath);
        }else if(end.equals("txt")){
            return getTextFileIntent(filePath,false);
        }else{
            return getAllIntent(filePath);
        }
    }

    //Android获取一个用于打开APK文件的intent
    public static Intent getAllIntent( String param ) {

        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(android.content.Intent.ACTION_VIEW);
        Uri uri = Uri.fromFile(new File(param ));
        intent.setDataAndType(uri,"*/*");
        return intent;
    }
    //Android获取一个用于打开APK文件的intent
    public static Intent getApkFileIntent( String param ) {

        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(android.content.Intent.ACTION_VIEW);
        Uri uri = Uri.fromFile(new File(param ));
        intent.setDataAndType(uri,"application/vnd.android.package-archive");
        return intent;
    }

    //Android获取一个用于打开VIDEO文件的intent
    public static Intent getVideoFileIntent( String param ) {

        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("oneshot", 0);
        intent.putExtra("configchange", 0);
        Uri uri = Uri.fromFile(new File(param ));
        intent.setDataAndType(uri, "video/*");
        return intent;
    }

    //Android获取一个用于打开AUDIO文件的intent
    public static Intent getAudioFileIntent( String param ){

        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("oneshot", 0);
        intent.putExtra("configchange", 0);
        Uri uri = Uri.fromFile(new File(param ));
        intent.setDataAndType(uri, "audio/*");
        return intent;
    }

    //Android获取一个用于打开Html文件的intent
    public static Intent getHtmlFileIntent( String param ){

        Uri uri = Uri.parse(param ).buildUpon().encodedAuthority("com.android.htmlfileprovider").scheme("content").encodedPath(param ).build();
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.setDataAndType(uri, "text/html");
        return intent;
    }

    //Android获取一个用于打开图片文件的intent
    public static Intent getImageFileIntent( String param ) {

        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri uri = Uri.fromFile(new File(param ));
        intent.setDataAndType(uri, "image/*");
        return intent;
    }

    //Android获取一个用于打开PPT文件的intent
    public static Intent getPptFileIntent( String param ){

        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri uri = Uri.fromFile(new File(param ));
        intent.setDataAndType(uri, "application/vnd.ms-powerpoint");
        return intent;
    }

    //Android获取一个用于打开Excel文件的intent
    public static Intent getExcelFileIntent( String param ){

        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri uri = Uri.fromFile(new File(param ));
        intent.setDataAndType(uri, "application/vnd.ms-excel");
        return intent;
    }

    //Android获取一个用于打开Word文件的intent
    public static Intent getWordFileIntent( String param ){

        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri uri = Uri.fromFile(new File(param ));
        intent.setDataAndType(uri, "application/msword");
        return intent;
    }

    //Android获取一个用于打开CHM文件的intent
    public static Intent getChmFileIntent( String param ){

        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri uri = Uri.fromFile(new File(param ));
        intent.setDataAndType(uri, "application/x-chm");
        return intent;
    }

    //Android获取一个用于打开文本文件的intent
    public static Intent getTextFileIntent( String param, boolean paramBoolean){

        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (paramBoolean){
            Uri uri1 = Uri.parse(param );
            intent.setDataAndType(uri1, "text/plain");
        }else{
            Uri uri2 = Uri.fromFile(new File(param ));
            intent.setDataAndType(uri2, "text/plain");
        }
        return intent;
    }
    //Android获取一个用于打开PDF文件的intent
    public static Intent getPdfFileIntent( String param ){

        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri uri = Uri.fromFile(new File(param ));
        intent.setDataAndType(uri, "application/pdf");
        return intent;
    }

}

代码写完后 ,我们就要修改manifest文件了

我们先拷贝app中的manifest 文件

Android 选择文件系统自带 安卓选择文件打开方式_ide_20

拷贝完如下:

Android 选择文件系统自带 安卓选择文件打开方式_Android 选择文件系统自带_21


将报红的删掉,注意删除第三个的时候别把尖括号删掉,不然会报错

现在我们添加一句配置 添加完如下

Android 选择文件系统自带 安卓选择文件打开方式_Android_22

配置完后我们就要生成aar包,点击 make module

Android 选择文件系统自带 安卓选择文件打开方式_Unity与Android交互_23


make成功后 可以在该路径中看到一个aar包,将他先复制到桌面,我们需要将aar进行修改

Android 选择文件系统自带 安卓选择文件打开方式_Unity与Android交互_24

用压缩软件打开aar 显示如下

Android 选择文件系统自带 安卓选择文件打开方式_android_25


点进libs 我们可以看到一个classes.jar 我们需要将他删掉 然后把外面的classes.jar放进来

Android 选择文件系统自带 安卓选择文件打开方式_android_26

接着我们要改AndroidManifest.xml文件 我们先打开文件 如下:

Android 选择文件系统自带 安卓选择文件打开方式_ide_27


修改后的文件如下:

Android 选择文件系统自带 安卓选择文件打开方式_Android_28


之所以删掉 是因为我们等下需要另外一个manifest文件 如果不删掉 会和之后的文件冲突

改完后记得放回aar包中现在我们将另外一个manifest找到 路径如下:

Android 选择文件系统自带 安卓选择文件打开方式_android_29

暂时先拷贝到桌面 打开如下:

Android 选择文件系统自带 安卓选择文件打开方式_ide_30

如果想更改发布出来安装在手机上apk的名字 则下面@string/app_name 改名成你想要的名字

Android 选择文件系统自带 安卓选择文件打开方式_Unity与Android交互_31

而如果你想在unity的包名打包,将下图com.study.mylibrary改成unity的包名即可

Android 选择文件系统自带 安卓选择文件打开方式_ide_32

最后我的minifest如下:

Android 选择文件系统自带 安卓选择文件打开方式_android_33

android studio就搞定 现在就请出unity了

新建一个unity 项目后 记得将平台更改为android平台 并将aar和manifest导进来,并且新建一个场景作为测试场景 新建一个脚本用来作为测试脚本 如图下:

Android 选择文件系统自带 安卓选择文件打开方式_android_34

脚本如下:

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


public class OpenFile : MonoBehaviour {
    
    public Text pathText;
    public Button openFileBtn;
    public AndroidJavaObject jo;
    public AndroidJavaClass jc;


    private void Awake()
    {
        jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
        openFileBtn.onClick.AddListener(OnOpenFileClick);
    }

    public void OnOpenFileClick()
    {
        //Call 参数第一个 就是android 的函数名 第二个就是你要传的参数 我们要调用android的show的函数 并传string字符串过去
        jo.Call("show", pathText.text);
    }

}

最后场景如下:

Android 选择文件系统自带 安卓选择文件打开方式_android_35

接下来我们就要打包APK

playsetting 配置如下

Android 选择文件系统自带 安卓选择文件打开方式_android_36


Android 选择文件系统自带 安卓选择文件打开方式_ide_37

设置完后直接打包

在手机后运行

Android 选择文件系统自带 安卓选择文件打开方式_android_38

路径是我在手机上随便找个文件测试的 按钮点击后效果如下:

Android 选择文件系统自带 安卓选择文件打开方式_ide_39

点击哪个都可以打开文件 证明是可以的 一下 就是全部过程

补充:

Unity使用Gradle打包的 使用internal 可能有问题 我用unity版本为2017.3.1f

还有注意点就是playersetting

Android 选择文件系统自带 安卓选择文件打开方式_Android 选择文件系统自带_40

这句一定要选成这个 不然应用没有读写权限 会出问题