开发流程:
Unity导出透明背景的宠物组件
AndroidStudio导入组件后二次开发
Unity宠物组件设置
1.将宠物设置好后调整宠物大小占满整个屏幕,效果如图
2.调整Camera配置,Clear Flags->Solid Color,Background->RGBA0000,设置如图:
3.Unity设置如导出Android传统项目即可,最后勾选Export Project,如图所示
4.点击Export,等待导出成功即可
Android导入组件并进行二次开发
1.以Module的形式导入,如图
上图中的launcher是Unity自带的微启动器,并不需要导入
2.在ProjectStructure中为app主程序添加unityLibrary依赖(Module Dependence)
3.将unityLibrary的sdk配置设置为与app主程序相同,在Project Structure的Modules中配置即可
4.进入项目文件列表,将unityLibrary->libs->unity-classes.jar复制到app主程序的libs文件夹下并添加库依赖(需要使用其中的类进行开发,否则可能提示找不到类)
5.打开unityLibrary的AndroidManifest.xml文件,进行如下修改
android:process=":raadidcard"
其中添加部分修改启动模式,注释部分作用是创建安装图标,我们在app主程序使用即可
6.打开app主程序的AndroidManifest.xml文件添加代码,如下图
xmlns:tools="http://schemas.android.com/tools"
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW" />
tools:replace="android:icon,android:theme,android:allowBackup"
7.打开app主程序的build.gradle文件添加代码段如下图
ndk {
abiFilters 'armeabi-v7a'
}
splits {
abi {
enable true
reset()
include 'armeabi-v7a'
universalApk true
}
}
8.打开app主程序的values文件夹下的strings.xml文件添加代码
<string name="game_view_content_description">Game view</string>
9.新建一个布局文件,主布局要包含一个子布局,代码如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/rl_pet"
android:layout_width="200dp"
android:layout_height="300dp" />
</LinearLayout>
10.打开unityLibrary中的UnityPlayerActivity.java文件,注释一些代码,如下图
类似注释代码段2,将onStop/onStart/onPause/onResume内的mUnityPlayer操作均注释掉
11.新建MUnityActivity.java,代码如下
package com.jk.yuan;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.widget.Toast;
import com.unity3d.player.UnityPlayer;
import com.unity3d.player.UnityPlayerActivity;
public class MUnityActivity extends UnityPlayerActivity {
public static UnityPlayer mup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(mup == null) {
mup = mUnityPlayer;
}
if (!Settings.canDrawOverlays(this)) {
Toast.makeText(this, "当前无权限,请授权", Toast.LENGTH_SHORT).show();
startActivityForResult(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName())), 1);
} else {
Toast.makeText(this, "启动!!!", Toast.LENGTH_SHORT).show();
startService(new Intent(MUnityActivity.this, UnityService.class));
moveTaskToBack(true);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (!Settings.canDrawOverlays(this)) {
Toast.makeText(this, "授权失败", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "授权成功", Toast.LENGTH_SHORT).show();
startService(new Intent(MUnityActivity.this, UnityService.class));
moveTaskToBack(true);
}
}
}
}
12.新建UnityService.java文件,代码如下
package com.jk.yuan;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.os.Build;
import android.os.IBinder;
import android.provider.Settings;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import androidx.annotation.Nullable;
import com.unity3d.player.UnityPlayer;
public class UnityService extends Service{
//要引用的布局文件.
LinearLayout touchLayout;
//布局参数
WindowManager.LayoutParams params;
//实例化的WindowManager.
WindowManager windowManager;
protected UnityPlayer mUnityPlayer;
@Override
public void onCreate() {
super.onCreate();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
private void createToucher(UnityPlayer mup) {
//赋值WindowManager&LayoutParam.
params = new WindowManager.LayoutParams();
windowManager = (WindowManager) getApplication().getSystemService(Context.WINDOW_SERVICE);
//根据版本设置type
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
params.type = WindowManager.LayoutParams.TYPE_PHONE;
}
//设置效果为背景透明.
params.format = PixelFormat.RGBA_8888;
//设置透明度
params.alpha = 1.0f;
//设置flags.不可聚焦及不可使用按钮对悬浮窗进行操控.
params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
//设置窗口初始停靠位置.
params.gravity = Gravity.RIGHT | Gravity.BOTTOM;
params.x = 0;
params.y = 0;
Point point = new Point();
windowManager.getDefaultDisplay().getRealSize(point);
DisplayMetrics dm = new DisplayMetrics();
windowManager.getDefaultDisplay().getMetrics(dm);
int dpi = (int) (dm.density*160);
//设置悬浮窗口长宽数据.
params.width = 200*(dpi/160);
params.height = 300*(dpi/160);
LayoutInflater inflater = LayoutInflater.from(getApplication());
//获取浮动窗口视图所在布局.
touchLayout = (LinearLayout) inflater.inflate(R.layout.pet_window, null);
if (Settings.canDrawOverlays(this)) {
//添加toucherlayout
windowManager.addView(touchLayout, params);
}
mUnityPlayer = mup;
((RelativeLayout) touchLayout.findViewById(R.id.rl_pet)).addView(mUnityPlayer);
//mUnityPlayer.start();
mUnityPlayer.resume();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
UnityPlayer upa = MUnityActivity.mup;
createToucher(upa);
mUnityPlayer.windowFocusChanged(true);
return super.onStartCommand(intent, flags, startId);
}
// Quit Unity
@Override public void onDestroy ()
{
mUnityPlayer.pause();
//mUnityPlayer.stop();
mUnityPlayer.quit();
super.onDestroy();
}
}
13.在app主程序的AndroidManifest.xml文件中配置MUnityActivity.java为主Acitivity,并添加UnityService的声明,代码如下
<activity
android:name=".MUnityActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:exported="true"
android:launchMode="singleInstance"
android:theme="@style/UnityThemeSelector.Translucent">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".UnityService"></service>
14.在运行设置中设置运行选项为MUnityAcitivty,如图所示
14.连接安卓测试机运行即可
原理分析
笔者使用Service启动悬浮窗进行开发,但由于屡次尝试失败,只有将官方导出的代码中的UnityPlayer获取使用,将其部分生命周期在Service中执行