赞
踩
前期准备材料
1、已经导出成功的unity项目,导出的unity项目内部结构见下图
2、新建一个或者使用已有项目
打开安卓项目,导入unity的module,找到unity项目中的unityLibrary,选中此module,点击finish后稍等片刻。
a、在项目的gradle.properties中添加如下代码
unityStreamingAssets=.unity3d, google-services-desktop.json, google-services.json, GoogleService-Info.plist
b、在主module中的build.gradle中添加如下代码
configurations.all {
// 重点大问题:一次性解决support库版本不一致,直接改了所有的依赖项目
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
def requested = details.requested
if (requested.group == 'com.android.support') {
if (!requested.name.startsWith("multidex")) {
details.useVersion '28.0.0'
}
}
}
}
public class MyUnityPlayer extends UnityPlayer {
public MyUnityPlayer(Context context) {
super(context);
}
protected void kill() {
}
}
在build.gradle中引用aar:
implementation(name: 'unityLibrary-debug', ext: 'aar')
如下是unityLibrary中so文件的位置:
如下是主module中位置,如果没有jniLibs文件夹新建即可。
a、在主module中新建UnityActivity,布局文件中只有一个LinearLayout即可:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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=".unity.SecondActivity"> <LinearLayout android:id="@+id/unityLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="8dp" android:layout_weight="10" android:orientation="horizontal" app:layout_constraintTop_toTopOf="parent" tools:layout_editor_absoluteX="104dp"> </LinearLayout> </LinearLayout>
b、UnityActivity中初始化LinearLayout和MyUnityPlayer,并重写unityplayer生命周期方法
private LinearLayout unityLayout; private MyUnityPlayer mUnityPlayer; // 在onCreate中初始化 unityLayout = findViewById(R.id.unityLayout); getWindow().setFormat(PixelFormat.RGBX_8888); // <--- This makes xperia play happy // 创建Unity视图 mUnityPlayer = new MyUnityPlayer(this); // 添加Unity视图 unityLayout.addView(mUnityPlayer.getView()); mUnityPlayer.requestFocus(); // 以下是生命周期方法---必须要写,否则黑屏 @Override protected void onNewIntent(Intent intent) { // To support deep linking, we need to make sure that the client can get access to // the last sent intent. The clients access this through a JNI api that allows them // to get the intent set on launch. To update that after launch we have to manually // replace the intent with the one caught here. super.onNewIntent(intent); setIntent(intent); } // Quit Unity @Override protected void onDestroy () { mUnityPlayer.quit(); super.onDestroy(); } // Pause Unity @Override protected void onPause() { super.onPause(); mUnityPlayer.pause(); } // Resume Unity @Override protected void onResume() { super.onResume(); mUnityPlayer.resume(); } // Low Memory Unity @Override public void onLowMemory() { super.onLowMemory(); mUnityPlayer.lowMemory(); } // Trim Memory Unity @Override public void onTrimMemory(int level) { super.onTrimMemory(level); if (level == TRIM_MEMORY_RUNNING_CRITICAL) { mUnityPlayer.lowMemory(); } } // This ensures the layout will be correct. @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); mUnityPlayer.configurationChanged(newConfig); } // Notify Unity of the focus change. @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); mUnityPlayer.windowFocusChanged(hasFocus); } // For some reason the multiple keyevent type is not supported by the ndk. // Force event injection by overriding dispatchKeyEvent(). @Override public boolean dispatchKeyEvent(KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_MULTIPLE) return mUnityPlayer.injectEvent(event); return super.dispatchKeyEvent(event); } // Pass any events not handled by (unfocused) views straight to UnityPlayer @Override public boolean onKeyUp(int keyCode, KeyEvent event) { return mUnityPlayer.injectEvent(event); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { return mUnityPlayer.injectEvent(event); } @Override public boolean onTouchEvent(MotionEvent event) { return mUnityPlayer.injectEvent(event); } /*API12*/ public boolean onGenericMotionEvent(MotionEvent event) { return mUnityPlayer.injectEvent(event); }
如果想要成为架构师或想突破20~30K薪资范畴,那就不要局限在编码,业务,要会选型、扩展,提升编程思维。此外,良好的职业规划也很重要,学习的习惯很重要,但是最重要的还是要能持之以恒,任何不能坚持落实的计划都是空谈。
如果你没有方向,这里给大家分享一套由阿里高级架构师编写的《Android八大模块进阶笔记》,帮大家将杂乱、零散、碎片化的知识进行体系化的整理,让大家系统而高效地掌握Android开发的各个知识点。
相对于我们平时看的碎片化内容,这份笔记的知识点更系统化,更容易理解和记忆,是严格按照知识体系编排的。
一、面试合集
二、源码解析合集
三、开源框架合集
欢迎大家一键三连支持,若需要文中资料,直接扫描文末CSDN官方认证微信卡片免费领取↓↓↓
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。