新建Unity工程
unity泛型单例脚本
SingletonUnity.cs
using UnityEngine;
public class SingletonUnity<T> : MonoBehaviour where T : MonoBehaviour
{
public static T instance = null;
public virtual void Awake()
{
if (instance == null)
{
DontDestroyOnLoad(gameObject);
instance = (T)FindObjectOfType(typeof(T));
}
else if (instance != null)
{
Destroy(gameObject);
}
}
}
调用Java脚本
MobPlugin .cs
using UnityEngine;
public class MobPlugin : SingletonUnity<MobPlugin>
{
#elif UNITY_ANDROID && !UNITY_EDITOR
AndroidJavaClass jc;
AndroidJavaObject activity;
#endif
public void Start()
{
#if UNITY_ANDROID && !UNITY_EDITOR
jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
activity = jc.GetStatic<AndroidJavaObject>("currentActivity");
activity.Call("SetSplashGone");
#endif
}
public void UnityToast(string msg)
{
#if UNITY_ANDROID && !UNITY_EDITOR
activity.Call("unityToast", msg);
#endif
Debug.Log("UnityToast:" + msg);
}
public void AndroidToast(string msg)
{
TestAndroid testAndroid = GetComponent<TestAndroid>();
if (testAndroid!=null)
{
testAndroid.Toast(msg);
}
}
}
测试脚本
TestAndroid.cs
using UnityEngine;
using UnityEngine.UI;
public class TestAndroid : MonoBehaviour {
public Text text;
public void UnityToast() {
MobPlugin.instance.UnityToast("Unity中点击了按钮");
}
public void Toast(string msg)
{
if (text)
{
text.text = msg;
}
}
}
简易unity界面搭建
unity导出设置
包名可以随意命名之后用不到该包名
导出后的Android工程中间件
新建Android工程
这里使用Android 8.0
因为Google play的要求
新建空Activity
新建一个模块作为unity模块供主模块调用,让代码更简洁
settings.gradle中要包含unity模块
在app模块添加依赖
implementation project(':unitylib')
添加assets文件夹
添加jniLibs文件夹
同步代码混淆规则配置文件
同步unitylib模块的 gradlew
放置jar包
放置assets下的文件
放置.so文件
修改UnityPlayerActivity.java
package com.apen.unitylib;//package com.apen.uni;将之前的包名修改为库模块的包名
import com.unity3d.player.*;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;
public class UnityPlayerActivity extends Activity
{
protected UnityPlayer mUnityPlayer;
/**
* 设置父视图
*/
public void setView(RelativeLayout mParent){
this.mUnityPlayer = new UnityPlayer(this);
View mView = this.mUnityPlayer.getView();
mParent.addView(mView);
this.mUnityPlayer.requestFocus();
}
....//打包后生成的代码保留,仅删除protected void onCreate(Bundle savedInstanceState) {}方法,为了之后继承该activity不出错
}
在app模块中继承UnityPlayerActivity并设置视图
package com.apen.unity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.unity3d.player.UnityPlayer;
import com.apen.unitylib.UnityPlayerActivity;
public class UnityActivity extends WebActivity {
RelativeLayout mParent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_unity);
mParent=findViewById(R.id.UnityView);
setView(mParent);
findViewById(R.id.androidclick).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
UnityPlayer.UnitySendMessage("TestAndroid", "Toast", "Android中点击了按钮");
}
});
}
public void unityToast(final String msg) {
new Thread(new Runnable() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(UnityActivity.this, msg, Toast.LENGTH_LONG).show();
}
});
}
}).start();
}
}
视图xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".UnityActivity">
<RelativeLayout
android:id="@+id/UnityView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView
android:id="@+id/androidclick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TestAndroid"/>
</RelativeLayout>
配置Android 清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.apen.unity"
android:installLocation="preferExternal">
<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"
android:anyDensity="true" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-feature android:glEsVersion="0x00020000" />
<uses-feature android:name="android.software.leanback" android:required="false" /><!--因为该工程不适配Android TV,所以要设置这项-->
<uses-feature android:name="android.hardware.touchscreen" android:required="false" />
<uses-feature android:name="android.hardware.touchscreen.multitouch" android:required="false" />
<uses-feature android:name="android.hardware.touchscreen.multitouch.distinct" android:required="false" />
<application
android:allowBackup="true"
android:name=".App"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:isGame="true"
android:banner="@drawable/app_banner">
<activity android:label="@string/app_name"
android:screenOrientation="fullSensor"
android:launchMode="singleTask"
android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale|layoutDirection|density"
android:name=".UnityActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
</intent-filter>
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
</activity>
<meta-data android:name="unity.build-id" android:value="64375dc2-7bb1-4490-ad40-f03e6d678f1c" />
<meta-data android:name="unity.splash-mode" android:value="0" />
<meta-data android:name="unity.splash-enable" android:value="True" />
<meta-data android:name="android.max_aspect" android:value="2.1" />
</application>
</manifest>
git:https://github.com/appppppen/Unity4Android-Project.git,欢迎star