赞
踩
参考文档:
Android内嵌Unity并实现互相跳转的实例代码_Android_脚本之家
Android Fragment中加载,嵌套Unity视图_unity 导入并作为fragment_周灬浩的博客-CSDN博客
本文只实现了Activity整个切换过去,而Fragment,这种像h5似的与app共存,目前还未实现,可参考的第二个链接
然后在节点上添加一个脚本
- using UnityEngine;
- using UnityEngine.SceneManagement;
- using UnityEngine.UI;
- /// <summary>
- /// 安卓 unity交互
- /// </summary>
- public class AndoridAndUnity : MonoBehaviour
- {
- public static AndoridAndUnity instance;
- AndroidJavaObject jo;
- private void Awake()
- {
- Debug.Log("AndoridAndUnity Awake");
- instance = this;
- //RunGameScene("Test");
- DontDestroyOnLoad(this);
- }
-
- private void Start()
- {
- Debug.Log("AndoridAndUnity Start");
- //Debug.Log("AndoridAndUnity 包名:"+ Application.identifier);
- //Debug.Log("AndoridAndUnity 版本号:" + Application.version);
-
- }
-
- //安卓回掉unity
- void fromAndroid(string str)
- {
- Debug.Log("安卓传来信息 : " + str);
- }
-
- //安卓回掉unity
- void RunGameScene(string str)
- {
- Debug.Log("切换场景 : " + str);
- SceneManager.LoadScene(str);
- }
-
- //返回app
- public void GoBack()
- {
- PlayerPrefs.Save();
-
- Debug.Log("AndoridAndUnity GoBack");
- AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
- jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
- jo.Call("goBack");//调用Android中函数
-
- //Application.Quit();
- }
-
- string str;
- private void OnGUI()
- {
- GUI.color = Color.white;
- //GUI.Label(new Rect(10, 100, 200, 40), str);
- if (GUI.Button(new Rect(10, 20, 200, 80), "返回"))
- {
- //str = "点击成功";
- GoBack();
- }
-
- }
-
-
- }
然后在Build Settings中将平台切换到Android
Unity项目目录与(下面新建的)安卓项目目录之间的关系:
选择NoActivity->Next
尽量保持一致,不一致也是以安卓的为准
NativeAndroidApp 中settings.gradle文件添加代码,将导出的unity库导入到安卓项目中(unityLibrary 的路径就是导出放置的路径有关)
- include ':unityLibrary'
- project(':unityLibrary').projectDir = new File('..\\androidBuild\\unityLibrary')
在应用级build.gradle中加入so库和dependencies相应的unity依赖 在应用级build.gradle中加入so库和dependencies相应的unity依赖
- android {
- defaultConfig {
- ***
- ndk {
- // 设置支持的SO库架构,第三方给的so库哪几种架构,就配置这几种架构
- abiFilters 'armeabi', 'armeabi', 'x86', 'armeabi-v7a', 'x86_64', 'arm64-v8a'
- }
- }
- }
-
- dependencies {
- implementation project(':unityLibrary')
- implementation fileTree(dir: project(':unityLibrary').getProjectDir().toString() + ('\\libs'), include: ['*.jar'])
- ***
- }
在根级build.gradle(Module:NativeAndroidApp)中 加入
- allprojects {
- repositories {
- google()
- jcenter()
-
- // Add Code
- flatDir {
- dirs "${project(':unityLibrary').projectDir}/libs"
- }
- // End
- }
- }
引用完成就能在项目中看到unityLibrary,并修改unityLibrary下面的AndroidManifest.xml文中
选择 NativeAndroidApp 的 strings.xml 添加
<string name=
"game_view_content_description"
>Game view</string>
构建一个ui布局,并加入button按钮
MainActivity.java
- package com.fancy.rush;
-
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
-
- public class MainActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main_unity);
-
- Button btn=(Button) findViewById(R.id.button);
- btn.setOnClickListener(new View.OnClickListener(){
- @Override
- public void onClick(View v) {
- Log.d("/Unity", "UnityActivity");
-
- Intent intent = new Intent(MainActivity.this, UnityActivity.class);
- intent.putExtra("sceneLevel","MatchAndMerge");//传递参数,利用传递的参数可以切换场景,或者数据之类的
- startActivity(intent);
- }
- });
- }
- }
UnityActivity.java
- import android.os.Bundle;
- import android.util.Log;
-
- import com.unity3d.player.UnityPlayer;
- import com.unity3d.player.UnityPlayerActivity;
- import com.unity3d.player.UnityPlayer;
-
-
- public class UnityActivity extends UnityPlayerActivity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
-
- Intent intent=getIntent();
- String sceneLevel=intent.getStringExtra("sceneLevel");
- testSendMessage(sceneLevel);
- }
-
- //当unity场景设置为常驻activity时,就需要运用到,常驻场景goBack切换出去后,再次打开时不会再周OnCreate,而是走onNewIntent
- @Override protected void onNewIntent(Intent intent)
- {
- super.onNewIntent(intent);
-
- String sceneLevel=intent.getStringExtra("sceneLevel");
- testSendMessage(sceneLevel);
- }
-
- // 向unity发送信息
- public void testSendMessage(String sceneLevel)
- {
- //获取意图对象
- Log.d("/Unity", "testSendMessage sceneLevel:"+sceneLevel);
-
- UnityPlayer.UnitySendMessage("unityObject", "fromAndroid", "UnityActivity");
-
- //利用传递的参数切换场景
- //第一个参数是Unity中一个节点对象的名字,第二个参数是节点对象上挂的脚本中一个函数的名字,第三个参数是函数中的参数值
- //UnityPlayer.UnitySendMessage("unityObject", "RunGameScene", sceneLevel);
- }
-
- public void goBack()
- {
- // unity点击按钮跳转回安卓界面
- Intent intent = new Intent(this, MainActivity.class);
- this.startActivity(intent);
- }
- }
再unity和app间相互切换会出现奔溃现象,只要把UnityActivity设置为 singleTask,常驻就不会被清除掉,再次调用打开unity就不会奔溃
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。