Android Fragment展示Unity场景,android移动应用开发

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 Fragment展示Unity场景

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重写其他方法

@Override

protected void onDestroy() {

UnityScene.mUnityPlayer.quit();

super.onDestroy();

}

@Override

protected void onPause() {

super.onPause();

UnityScene.mUnityPlayer.pause();

}

@Override

protected void onResume() {

super.onResume();

UnityScene.mUnityPlayer.resume();

}

@Override

public void onConfigurationChanged(Configuration newConfig) {

super.onConfigurationChanged(newConfig);

UnityScene.mUniAndroid Fragment展示Unity场景,android移动应用开发_第1张图片
tyPlayer.configurationChanged(newConfig);

}

@Override

public void onWindowFocusChanged(boolean hasFocus) {

super.onWindowFocusChanged(hasFocus);

UnityScene.mUnityPlayer.windowFocusChanged(hasFocus);

}

@Override

public boolean dispatchKeyEvent(KeyEvent event) {

if (event.getAction() == KeyEvent.ACTION_MULTIPLE)

return UnityScene.mUnityPlayer.injectEvent(event);

return super.dispatchKeyEvent(event);

}

r.windowFocusChanged(hasFocus);

}

@Override

public boolean dispatchKeyEvent(KeyEvent event) {

if (event.getAction() == KeyEvent.ACTION_MULTIPLE)

return UnityScene.mUnityPlayer.injectEvent(event);

return super.dispatchKeyEvent(event);

}

你可能感兴趣的:(程序员,架构,移动开发,android)