Unity嵌入AndroidStudio(二) IL2CPP打包

首先建立Unity工程,话不多说直接上图:

导出Android工程: 

 得到如下文件,备用:

接下来创建安卓项目:

 注意包名要和unity里面的一致,sdk版本也要一致

 等待编译完成:

打开setting.grade添加依赖:

include ':unityLibrary'

打开gradle.properties添加StreamingAsset;

unityStreamingAssets=.unity3d, google-services-desktop.json, google-services.json, GoogleService-Info.plist

安卓要启动Unity工程,所以在主入口build.gradle添加unity的路标(草,我也不知道咋说了)

implementation project(path: ':unityLibrary')

接下来添加ndk(记得换成自己的路径)

ndk.dir=C\:\\Program Files\\Unity\\Hub\\Editor\\2020.3.40f1\\Editor\\Data\\PlaybackEngines\\AndroidPlayer\\NDK

然后再主app添加ndk:

 ndk {
            abiFilters "armeabi", "armeabi-v7a", "x86", "mips","x86_64"
        }

还有最后一个部分在string里面添加

<string name="game_view_content_description">Game view</string>

 至此安卓环境配置部分全部结束。

  将刚才导出的安卓工程里面的unityLibrary文件夹直接放入安卓工程中,等待编译完成。

 

在安卓里面启动Unity

 添加启动按钮:

 代码如下:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="进入Unity场景"
        android:textAllCaps="false"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

 直接粘贴过来可能会报错如图:

这会只需要搞一下jar包就行了: 

 右键有一个as a library,点一下就行了。

然后如图:

 然后打包,发现一致报错,提示Failed to load ‘libmain.so’

回头把Unity里面的设置改了一下就行了

也可以这样:

 

对了打包后发现有两个图标,这个时候只需要打开unityLibrary里面的 安卓清单文件,删除以下代码即可:

如果只是在安卓启动unity工程以上就可以了,如果有交互部分可以修改Unity场景为:

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

public class Test : MonoBehaviour
{
    private AndroidJavaClass androidJavaClass;
    private AndroidJavaObject androidJavaObject;
    private Text msg;
    private Button btn;
    private void Start()
    {
        msg = GameObject.Find("Text").GetComponent<Text>();
        btn = GameObject.Find("Button").GetComponent<Button>();
        androidJavaClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        androidJavaObject = androidJavaClass.GetStatic<AndroidJavaObject>("currentActivity");
        btn.onClick.AddListener(CallAndroid);
    }
    public void CallAndroid()
    {
        msg.text = androidJavaObject.Call<string>("BackToUnity");
        Debug.Log("unity调用安卓函数");
    }
}

 然后再安卓里面找到unityLibrary添加java代码:

 private int num;

    //在unity里面调用并返回消息
    public String BackToUnity() {
        num++;
        return "安卓收到Unity消息:"+num;
    }

 打包。

Unity嵌入安卓

结束。 

猜你喜欢

转载自blog.csdn.net/qq_38513810/article/details/130942385