menu 首页 标签 归档 视频 关于
Unity-Unity与Android交互

一言加载中...

前言

用到Unity与Android好多次了,每次都需要重复的找资料,造轮子,这次接了一个小项目后又要用到,完成后下定决心记录AndroidStudio打aar包的过程,本来还想封装起来以后直接用,后来发现这个做法不可行。那以后还是老老实实用一个做一个吧。

过程

  1. Unity端创建项目,切换为Android平台,修改包名,我这里设置为"com.dt.test"

  2. 创建AndroidStudio工程,创建Empty Activity,名称随意,包名随意,最低API也根据自己安装的Android SDK选择。选择Android Library,创建模块。

    au1

    newmodule

  3. 在模块中创建EmptyActivity,勾选Launcher Activity

  4. 从Unity安装路径下找到classes.jar包导入并添加到模块的libs文件夹下

   Windows: ...unity\Editor\Data\PlaybackEngines\AndroidPlayer\Variations\mono\Release\Classes\classes.jar
   Mac:...unity/PlaybackEngines/AndroidPlayer/Variations/mono/Release/Classes/classes.jar
  1. 删除activity_main.xml,在MainActivity.java中删除setContentView(R.layout.activity_main);,并让MainActivity继承UnityPlayerActivity。

6.将app包下的manifest的application标签中的内容覆盖掉Unity_Android_Library的application的标签中内容,删除爆红的三行,在</intent-filter>标签结尾后添加新的标签<meta-data android:name="unityplayer.UnityActivity" android:value="true"/>

  1. 在MainActivity.java 中书写需要用到的方法,举几个常用方法:
//Toast
public void ShowToast(String msg)&#123;
    Toast.makeText(getApplicationContext(),msg,Toast.LENGTH_LONG).show();
&#125;

//离开对话框
public static void ShowDialogSync()&#123;
    UnityPlayer.currentActivity.runOnUiThread(new Runnable() &#123;
        @Override
        public void run() &#123;
            AlertDialog.Builder builder=new AlertDialog.Builder(UnityPlayer.currentActivity);
            builder.setTitle("提示");
            builder.setMessage("你确定要退出吗?");
            //builder.setIcon(R.drawable.iconic);
            builder.setPositiveButton("取消", new DialogInterface.OnClickListener() &#123;
                @Override
                public void onClick(DialogInterface dialogInterface, int i) &#123;
                    dialogInterface.dismiss();
                &#125;
            &#125;);
            builder.setNegativeButton("确定", new DialogInterface.OnClickListener() &#123;
                @Override
                public void onClick(DialogInterface dialogInterface, int i) &#123;
                    System.exit(0);
                &#125;
            &#125;);
            builder.show();
        &#125;
    &#125;);
&#125;

//打开QQ
public void OpenQQSync(final String qq)&#123;
    UnityPlayer.currentActivity.runOnUiThread(new Runnable() &#123;
        @Override
        public void run() &#123;
            String url = "mqqwpa://im/chat?chat_type=wpa&uin="+qq+"&version=1";
            try &#123;
                startActivity(new Intent("android.intent.action.VIEW", parse(url)));
            &#125; catch (Exception e) &#123;
                ShowToast("本机未安装QQ");
            &#125;
        &#125;
    &#125;);

&#125;

//打开URL
public void OpenUrlSync(final String url)&#123;
    UnityPlayer.currentActivity.runOnUiThread(new Runnable() &#123;
        @Override
        public void run() &#123;
            try &#123;
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
            &#125;catch (Exception ex)&#123;
                ShowToast(ex.toString());
            &#125;
        &#125;
    &#125;);
&#125;
  1. 选中模块 ,点击Bulid/Make select Modules,导出aar包,并复制到桌面,将minifest文件也复制到桌面。

  2. 使用压缩工具打开aar包,删除libs\下的classes.jar

  3. 导入Unity Assets\Plugins\Android路径下

  4. 封装了一个类,供以后使用

using UnityEngine;

public class AndroidUtils
&#123;
    static AndroidJavaClass jc;
    static AndroidJavaObject jo;
    static AndroidUtils _instance = null;

    private AndroidUtils()
    &#123;
        jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
    &#125;
    public static AndroidUtils Instance
    &#123;
        get
        &#123;
            if (_instance == null) _instance = new AndroidUtils();

            return _instance;
        &#125;
    &#125;

    public void ShowDialog()
    &#123;
        jo.CallStatic("ShowDialogSync");
    &#125;

    public void ShowToast(string str)
    &#123;
        jo.Call("ShowToast", str);
    &#125;

    public void OpenQQ(string qq)
    &#123;
        jo.Call("OpenQQSync", qq);
    &#125;

    public void OpenUrl(string url)
    &#123;
        jo.Call("OpenUrlSync", url);
    &#125;
&#125;
  1. 调用
Toast.onClick.AddListener(() =>
&#123;
    AndroidUtils.Instance.ShowToast("Toast");
&#125;);

Dialog.onClick.AddListener(() =>
&#123;
    AndroidUtils.Instance.ShowDialog();
&#125;);

OpenQQ.onClick.AddListener(() =>
&#123;
    AndroidUtils.Instance.OpenQQ("xxx");
&#125;);

URL.onClick.AddListener(() =>
&#123;
    AndroidUtils.Instance.OpenUrl("https://www.baidu.com");
&#125;);

效果

result

写博客不易,请我喝杯咖啡?

评论

arrow_upward