Unity模块嵌入到Android中
阅读原文时间:2023年07月09日阅读:5

嗨,大家好,小黑在沉寂了6个月之后,终于要继续写一篇博客了。

先吐槽一波上家公司PHD&&OMS,不吐不快。上家公司的小黑,每天不是在弄UIWidgets,就是再弄UIWidgets,不然就是和它的开发者沟通反馈。。相关的使用项目更是推了重来、推了重来!扯淡的是没有项目经理,没有产品经理,要啥没啥,就让UI出图,那UI只是负责美啊,她们能管得了逻辑么?逻辑有冲突了我不得更改么,然后就让让人作呕的领导喷了,后来学聪明了,逻辑不对去找领导询问怎么更改或者是商量,又让喷了。。啊这?就这? 呵呵呵呵,tui~~

小黑见看不到希望了,所以小黑就开始学习,因为想进游戏公司,就开始学数据结构、学lua(上家公司也是用lua,但是从来没让小黑碰过!),通过学习数据结构了解到了什么是代码严谨性,lua不说了,做为轻量级语言,两天带着元表搞完。然后开始了面试,在五一前后面了不到10家吧,面试官对小黑的态度总结下来就是:打击、激励、安慰、嘲讽、赞赏。。。嘲讽的这个真的是小黑心态崩了,面试官问了很多,但是第一道很简单:问点是否在圆内。面完之后小黑本人还觉得面的不错,但最后接到的反馈:勾股定理都不会??。。。。。FUCK,那我回答的时候,面试官想知道勾股定理倒是点我一下啊,我给你说不就好了??   问题是驴唇,要的答案是马嘴,啥也不说了,也怪小黑自己菜,回答解题思路时应该把能想到的都说一遍!!!

吐槽完了,言归正传,我们开始学习。

目录

今天学习:Unity模块嵌入到Android中

版本:

1、在Unity准备自己相应的工程文件

1、搭建简单场景

2、创建并且编写脚本

3、挂载脚本

4、打包发布Unity为Andoird工程

5、查看发布文件

2、准备在Android端嵌入Unity工程文件。

1、你是一个Andoird工程师

2、非Andoird工程师

唠家常

今日无推荐


今天学习:Unity模块嵌入到Android中

1、Unity 2020.3.10f1

2、Android Studio 4.2.1

1、搭建简单场景

2、创建并且编写脚本

代码很简单,有错误的地方大家指出,我来更改。

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

public class RoutationTest : MonoBehaviour
{
    public Toggle _toggle;
    public Button _button;
    void Start()
    {
        _toggle.onValueChanged.AddListener(delegate
        {
            if (_value == 0000.1f)
                _value = -0000.1f;
            else
                _value = 0000.1f;
        });
        _button.onClick.AddListener(()=>
        {
            Debug.Log("输出:  返回按钮被触发.");
            backNavtive();

        });
    }

    float _value = 0000.1f;
    void Update()
    {
        this.transform.RotateAround(Vector3.up, _value);      
    }

    //用来调用Java端的函数
    private void backNavtive()
    {
#if UNITY_ANDROID && !UNITY_EDITOR
      AndroidJavaClass __javaClass = new AndroidJavaClass("  包名  .   java类名");
     
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //AndroidJavaObject __jo = __javaClass.Get<AndroidJavaObject>("currentActivity");
&nbsp; &nbsp; &nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; __javaClass.CallStatic("BackToNative", "输出:给原生传递的返回参数");
#endif
&nbsp;&nbsp;&nbsp; }

&nbsp; &nbsp; &nbsp; //用来给Java端调用的函数
&nbsp;&nbsp;&nbsp; public void ChangeRotation(string msg)
&nbsp;&nbsp;&nbsp; {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Debug.LogWarning("Unity端输出:" + msg);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _toggle.isOn = !_toggle.isOn;
&nbsp;&nbsp;&nbsp; }
}

3、挂载脚本

注意事项:

①、既然是要嵌入到Android中并且进行交互,那包名最好一样(当然也可以不一样,不过就需要多做些事情);

②、Android再与Unity交互中,是通过UnityPlayer.UnitySendMessage(物体名 , 函数名 , 参数 );所以场景中的名字要记住,本文中交互代码挂载到了Cube上,其下边交互函数为ChangeRotation

③、构建框架要选择ARMv7

4、打包发布Unity为Andoird工程

5、查看发布文件

下边的文件夹中,缺一不可(当然,成功打包也是不会缺少的,除非自己删除掉、或者打包失败)。


目前就小黑自己摸索来说有两个办法:

1你是一个Andoird工程师

你是一个Andoird工程师,会使用Android Studio,那么到了这里你也知道知道怎么与你的工程相融合了。

Ⅰ、直接File > New > Import Moudle > 选择路径 > 导入

Ⅱ、开始解决各种报错(目前小黑遇到的是一些SDK版本不一致问题,直接改掉就好)

2、非Andoird工程师

非Andoird工程师,和小黑一样只会一些基础,直接用Android Studio打开我们刚才发布出来的工程。

1、导入编译

经过一点时间编译后,系统会在AS某个角落(通常是右下角)提示更新Gradle,直接更新。

2、解决报错

然后看我们的结构目录:里边launcher下的build.gradleunityLibrary下的build.gradle可能会出现SDK版本问题。这时候改为自己的SDK版本号,然后同步Sync Now就ok了。

3、Make Project and Run

①、创建AS自带的虚拟机,然后打包至虚拟机运行,看我们的交互逻辑是否能被正常触发。

②、为了保险起见,我们打开自己的Android测试机,然后打开开发者开启调试,安装至真机测试。

好了,伙伴们都没有问题了吧?      (什么?不行?   emmmmm,从上面再来一遍)。没问题的我们接着往下走啊。

4、完成双端交互

①、Unity 给 Android 发送消息

我们在测试的时候,发现我们Unity端的按钮点击后会报错,不要慌张,我们现在开始解决!他报错内容无非就是在com.unity3d.player这个包体下边,缺少UnityPlayerActivityTest这个类,那我们直接创建一个就好了。

那我们创建好了之后,发现还是报错,简单不是?接着创建,别忘了是静态函数啊,我们在Unity端写的是CallStatic

好了,至此unity给android发送消息成功了。

②、Android 给 Unity 发送消息

如果我们没有原生交换按钮,那为了方便测试,我们就直接可以在AS里被Unity调用的函数中,再次调用给Unity发送消息。

然后我们Run起来,点击程序中左上角按钮:返回Navtive。

这时候我们看Log,发现成功了。是不是nice~~

那么,至此, Unity与Android之间的交互算是彻底完成。我们接下来要进行在Android中嵌入Unity了。


5、在Android中嵌入Unity

上面我们只是简单的创建了一个Java类,那我们启动时,其实是启动的Unity的Activity,而这个东西小黑暂时不会获取,那如何嵌入?怎么办? 没办法,所以只能自己把上边的类增加了一个Activity,这个类继承Activity就好了,如果继承了AppCompatActivity,就需要实现AppCompat了。

接着我们发现res的目录结构中,多了一个layout,下边的xml文件就是我们可以自己操作的UI面板,右上角上边有三个不同的展示面板,我们做一个很简单的事情,例如增加一个文本?

正事儿开始:

①、增加一个LinerLayoutunityPanel),用来呈现Unity端内容

②、增加一个LinerLayoutnavtivePanel),用来呈现Unity端内容

③、增加一个按钮(btn_CallUnity),用来给Unity发送消息。

④、增加一个按钮(btn_Activity)用来控制原生页面显隐。

⑤、开始写代码,不要介意小黑的代码风格啊。。。。。

package com.unity3d.player;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

/**
 * 用来和Unity交互并且展示原生按钮
 */
public class UnityPlayerActivityTest extends UnityPlayerActivity {

&nbsp; &nbsp; private static UnityPlayerActivityTest _Instance;
&nbsp; &nbsp; @Override
&nbsp; &nbsp; protected void onCreate(Bundle savedInstanceState) {
&nbsp; &nbsp; &nbsp; &nbsp; _Instance = this;
&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);
&nbsp; &nbsp; &nbsp; &nbsp; setContentView(R.layout.activity_unity_player_test);

&nbsp; &nbsp; &nbsp; &nbsp; _btnCallUnity = findViewById(R.id.btn_CallUnity);
&nbsp; &nbsp; &nbsp; &nbsp; _btnActivity = findViewById(R.id.btn_Activity);
&nbsp; &nbsp; &nbsp; &nbsp; _AndroidLiner = findViewById(R.id.navtivePanel);
&nbsp; &nbsp; &nbsp; &nbsp; _unityLiner = findViewById(R.id.unityPanel);
&nbsp; &nbsp; &nbsp; &nbsp; _unityLiner.addView(mUnityPlayer.getView());
&nbsp; &nbsp; &nbsp; &nbsp; _btnActivity.setOnClickListener((vb)->
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; openActivity(true);
&nbsp; &nbsp; &nbsp; &nbsp; });
&nbsp; &nbsp; &nbsp; &nbsp; _btnCallUnity.setOnClickListener((view)->
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CallUnityMessage("我来自Android端,要改变Unity中Cube的旋转方向");
&nbsp; &nbsp; &nbsp; &nbsp; });
&nbsp; &nbsp; }

&nbsp; &nbsp; Button _btnCallUnity, _btnActivity;
&nbsp; &nbsp; //ConstraintLayout ;
&nbsp; &nbsp; static LinearLayout _unityLiner,_AndroidLiner;

&nbsp; &nbsp; /**
&nbsp; &nbsp; * 用来控制原生的面板显隐
&nbsp; &nbsp; * @param isopen:开关选项
&nbsp; &nbsp; */
&nbsp; &nbsp; private static void openActivity(boolean isopen){

&nbsp; &nbsp; &nbsp; &nbsp; if (!isopen){
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _AndroidLiner.setVisibility(View.INVISIBLE);
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; else{
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _AndroidLiner.setVisibility(View.VISIBLE);
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; }

&nbsp; &nbsp; /**
&nbsp; &nbsp;  * 用来给Unity发送消息
&nbsp; &nbsp;  * @param msg:要给Unity发送的消息
&nbsp; &nbsp;  */
&nbsp; &nbsp; private void CallUnityMessage(String msg){
&nbsp; &nbsp; &nbsp; &nbsp; //因为我们直接用的Unity的包名,所以我们不需要增加引用
&nbsp; &nbsp; &nbsp; &nbsp; UnityPlayer.UnitySendMessage("Cube","ChangeRotation",msg);
&nbsp; &nbsp; }

&nbsp; &nbsp; /**
&nbsp; &nbsp;  * 用来接收Unity端口发来的消息
&nbsp; &nbsp;  * @param msg :Unity发的消息
&nbsp; &nbsp;  */
&nbsp; &nbsp; public static void BackToNative(String msg){
&nbsp; &nbsp; &nbsp; &nbsp; Log.w("BackToNative: ", "Android端接收到内容为:" + msg);
&nbsp; &nbsp; &nbsp; &nbsp; openActivity(false);
&nbsp; &nbsp; }
}

6、Make And Run

随便点击开始吧,不同的按钮我们都能区分出来是Andorid原生按钮还是UnityUI按钮。

至此,我们在Android中嵌入Unity已经全部完成了

别忘了打包真机进行测试,我们做程序的一定要严谨,一定要真机测试!

  • 小黑的今日分享结束啦,小伙伴们你们get到了么,你们有没有更好的办法呢,可以评论区留言分享,也可以加小黑的QQ:841298494,大家一起进步。

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章