赞
踩
嵌入项目的AndroidStudio版本和Unity版本
Unity2019
AndroidStudio2021
01 新建一个新的安卓项目
项目里新建一个button
实现button的点击事件进入游戏
package com.example.project_android; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.widget.Button; import android.view.View; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button button1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button1 = findViewById(R.id.button1); //注册监听器 button1.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.button1: onClickButton1(view); break; default: break; } } private void onClickButton1(View view) { System.out.println("触发点击进入游戏的按钮事件"); } }
unity导出android工程
导出的工程文件夹放入原生的安卓项目
放入如下代码
include ':unityLibrary'
project(':unityLibrary').projectDir=new File('.\\UnityAndroidProject\\unityLibrary')
放入如下代码
implementation project(':unityLibrary')
implementation fileTree(dir: project(':unityLibrary').getProjectDir().toString() + ('\\libs'), include: ['*.jar'])
放入如下代码
flatDir {
dirs "${project(':unityLibrary').projectDir}/libs"
}
android:process=":UnityActivity"
项目里添加UnityGameActivity.java
package com.example.project_android; import android.os.Bundle; import android.util.Log; import android.view.KeyEvent; import com.unity3d.player.UnityPlayer; import com.unity3d.player.UnityPlayerActivity; //进入到这个类就进入了unity游戏画面 public class UnityGameActivity extends UnityPlayerActivity { private static final String TAG = "UnityGameActivity"; private boolean isFinish = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); SendMessageToUnity("AndroidToUnityMessage"); } public void finishUnityGame(String str) { Log.i(TAG, "unity传给Android的信息: "+ str ); isFinish = true; //关闭unity游戏返回app finish(); } public void SendMessageToUnity(String str) { Log.i(TAG, "Android传给unity的信息: " + str); //其中GameData是unity生成的gameobject,脚本要挂在上面。AndroidToUnity是unity里实现的方法。str是传过去的值。 UnityPlayer.UnitySendMessage("GameData", "AndroidToUnity", str); } @Override protected void onResume() { Log.e(TAG, "onResume: " ); super.onResume(); } @Override protected void onPause() { Log.e(TAG, "onPause: " ); super.onPause(); if (!isFinish){ mUnityPlayer.pause(); } } @Override protected void onStop() { Log.e(TAG, "onStop: " ); super.onStop(); } @Override protected void onDestroy() { Log.e(TAG, "onDestroy: " ); super.onDestroy(); } }
需要配置,才能找到这个类
<activity android:name="com.example.project_android.UnityGameActivity" android:theme="@style/UnityThemeSelector" android:screenOrientation="landscape" android:launchMode="singleTask" android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale|layoutDirection|density" android:hardwareAccelerated="false">
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
<meta-data android:name="android.notch_support" android:value="true" />
</activity>
unity的脚本加上可以接收信息的方法,脚本需要挂在GameData上
public void AndroidToUnity(string str)
{
Debug.Log("Unity接收Android信息 = " + str);
//通过Newtonsoft.Json插件的JsonConvert.DeserializeObject方法,将json格式的string数据转换为model数据
//UnityDataModel unityDataModel = JsonConvert.DeserializeObject<UnityDataModel>(str);
//Debug.Log("unityDataModel.key1 = " + unityDataModel.key1);
//Debug.Log("unityDataModel.key2 = " + unityDataModel.key2);
}
完整代码
using System.Collections; using System.Collections.Generic; using UnityEngine; using Newtonsoft.Json; public class GameData : MonoBehaviour { public void IOSToUnity(string str) { Debug.Log("Unity接收IOS信息 = " + str); //通过Newtonsoft.Json插件的JsonConvert.DeserializeObject方法,将json格式的string数据转换为model数据 UnityDataModel unityDataModel = JsonConvert.DeserializeObject<UnityDataModel>(str); Debug.Log("unityDataModel.key1 = " + unityDataModel.key1); Debug.Log("unityDataModel.key2 = " + unityDataModel.key2); } public void AndroidToUnity(string str) { Debug.Log("Unity接收Android信息 = " + str); //通过Newtonsoft.Json插件的JsonConvert.DeserializeObject方法,将json格式的string数据转换为model数据 //UnityDataModel unityDataModel = JsonConvert.DeserializeObject<UnityDataModel>(str); //Debug.Log("unityDataModel.key1 = " + unityDataModel.key1); //Debug.Log("unityDataModel.key2 = " + unityDataModel.key2); } private void Awake() { DontDestroyOnLoad(this); } }
安卓端在button的点击事件里,加上启动unity游戏的方法
private void onClickButton1(View view) {
System.out.println("触发点击进入游戏的按钮事件");
startActivity(new Intent(view.getContext(), UnityGameActivity.class));
}
解决安装以上双图标问题
unity导出的android sdk 和 android studio导出的android sdk版本要一致
解决:E/Unity: Failed to load 'libmain.so’问题,需要在unity做如下配置:E/Unity: Failed to load 'libmain.so’问题,需要在unity做如下配置
游戏启动闪退问题解决
<string name="game_view_content_description">Game view</string>
Unity端结束游戏时发信息给安卓端
核心代码
#if UNITY_ANDROID
try
{
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject overrideActivity = jc.GetStatic<AndroidJavaObject>("currentActivity");
//AndroidToUnityMessage是发送给Android的信息,finishUnityGame是安卓端接收的方法
overrideActivity.Call("finishUnityGame", "AndroidToUnityMessage");
}
catch (Exception e)
{
Debug.Log(e.Message);
}
#endif
安卓可以横屏且可以旋转的设置
unity里的设置
AndroidStudio里的设置
<activity android:name="com.unity3d.player.UnityPlayerActivity" android:theme="@style/UnityThemeSelector" android:screenOrientation="sensorLandscape" android:launchMode="singleTask" android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale|layoutDirection|density" android:hardwareAccelerated="false">
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。