首先新建一个unity项目和一个AndroID项目,对应本demo的UnityScene和AndroIDProject
Unity Scene将unity配置为AndroID
file --> buildingSettings
勾选export project,building,新建UnityScene文件夹保存,导出后文件结构大致如下
以下文件放到AndroID 项目中对应位置(我仅导出arm-v7)
gradle中添加(与buildTypes同级)
sourceSets { main { //unity3D jnilibs.srcDirs = ['libs', 'libs-sdk'] }}
defaultConfig中添加
ndk { abiFilters "armeabi-v7a" }
在local.propertIEs中配置ndk(替换你自己的ndk)
ndk.dir=.../AndroID/sdk/ndk/xxxx
AndroID架构中应当看到
AndroID Project创建Fragment创建UnityScene.java
package com.example.androIDproject.unity;import com.unity3d.player.UnityPlayer;public class UnityScene { public static UnityPlayer munityPlayer; public UnityScene(){ }}
创建一个空的Fragment,添加如下代码
private VIEw playerVIEw; @OverrIDe public VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment playerVIEw = UnityScene.munityPlayer.getVIEw(); //具体参数 跟自己公司Unity开发人员协商 //第一个参数是unity那边的挂载脚本名字 //第二个参数是 unity提供的方法名 //第三个参数是 自己要给unity传的值// UnityScene.munityPlayer.UnitySendMessage("Main Camera","ID","1"); return playerVIEw; }
改写MainActivity在MainAcivity的onCreate中添加
UnityScene.munityPlayer = new UnityPlayer(getApplicationContext());getwindow().setFormat(PixelFormat.RGBX_8888);if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction() .setReorderingallowed(true) .add(R.ID.fragment_container_vIEw, UnityFragment.class, null) .commit();}
MainAcivity重写其他方法
@OverrIDeprotected voID onDestroy() { UnityScene.munityPlayer.quit(); super.onDestroy();}@OverrIDeprotected voID onPause() { super.onPause(); UnityScene.munityPlayer.pause();}@OverrIDeprotected voID onResume() { super.onResume(); UnityScene.munityPlayer.resume();}@OverrIDepublic voID onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); UnityScene.munityPlayer.configurationChanged(newConfig);}@OverrIDepublic voID onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); UnityScene.munityPlayer.windowFocusChanged(hasFocus);}@OverrIDepublic boolean dispatchKeyEvent(KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_MulTIPLE) return UnityScene.munityPlayer.injectEvent(event); return super.dispatchKeyEvent(event);}@OverrIDepublic boolean onKeyUp(int keyCode, KeyEvent event) { return UnityScene.munityPlayer.injectEvent(event);}@OverrIDepublic boolean onKeyDown(int keyCode, KeyEvent event) { return UnityScene.munityPlayer.injectEvent(event);}@OverrIDepublic boolean ontouchEvent(MotionEvent event) { return UnityScene.munityPlayer.injectEvent(event);}public boolean onGenericMotionEvent(MotionEvent event) { return UnityScene.munityPlayer.injectEvent(event);}
gradle中增加
implementation "androIDx.fragment:fragment:1.2.1"activity_main.xml如下<?xml version="1.0" enCoding="utf-8"?><androIDx.constraintlayout.Widget.ConstraintLayout 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=".MainActivity"> <androIDx.fragment.app.FragmentContainerVIEw xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:ID="@+ID/fragment_container_vIEw" androID:layout_wIDth="match_parent" androID:layout_height="500dp" app:layout_constrainttop_totopOf="parent" /></androIDx.constraintlayout.Widget.ConstraintLayout>
string.xml中添加
Game VIEw完成
运行项目即可看到unity场景嵌套在fragment中
最后我这里同大佬整理了AndroID进阶、音视频相关的学习资料,大家可以私信我:资料,免费获取
以上是内存溢出为你收集整理的Android Fragment展示Unity场景全部内容,希望文章能够帮你解决Android Fragment展示Unity场景所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)