android studio 集成 unity 导出工程
最近需要将 unity 导出工程 嵌入到 android 工程
环境
1,android studio 2021.1.1.1 2, unity 2021.2.4
新建android 工程
1,
2,
导出 unity 工程
1, 2,导出结果 unityLibrary 就是我们想要的结果 launcher 暂时用不上
3, 在android 工程中 4,选择unitylibarary 目录 5, 6,更改 app gradle配置文件:
implementation project(path:':unityLibrary')
implementation fileTree(dir: project(':unityLibrary').getProjectDir().toString()+('\\libs'), include: ['*.jar', '*.aar'])
ndk {
abiFilters 'armeabi-v7a', 'arm64-v8a'
}
7,删除manifest配置8, 在 gradle.property 添加配置 不加这个会报错
unityStreamingAssets=.unity3d, google-services-desktop.json, google-services.json, GoogleService-Info.plist
9,添加 value值 不加也会挂掉
<string name="game_view_content_description">Game view</string>
10,调用
Button myBtn= findViewById(R.id.button);
myBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,UnityPlayerActivity.class);
startActivity(intent);
}
});
android c# 互相调用
android 调用 c#
package com.xingame.unitycallandroid;
import android.os.Bundle;
import android.widget.Toast;
import com.unity3d.player.UnityPlayer;
public class MainActivity extends UnityPlayerActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void UnityCallAndroid () {
Toast.makeText(this,"unity调用android成功", Toast.LENGTH_LONG).show();
AndroidCallUnity();
}
public void AndroidCallUnity () {
UnityPlayer.UnitySendMessage("receiveObj", "UnityMethod", "This is args.");
}
}
c# 调用 java
private void OnBtnClickHandler()
{
using (jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer")){
using (jo = jc.GetStatic<AndroidJavaObject>("currentActivity")){
jo.Call("UnityCallAndroid");
}
}
}
引用
官方 git https://article.itxueyuan.com/9qO5pW
|