原由
Cocos creator官方推荐用多进程的方式发布到原生,没有单线程,但是需求方需要让我们把cocos用view的形式嵌入到某一个页面
效果图
工具信息
Cocos Dashboard: 1.2.3
Cocos Editor :3.4.2
Android studio :2021.3.1 Patch 1
第一步:把CocosActivity 改装成一个view
public class CocosNativeHelper implements SurfaceHolder.Callback, LifecycleObserver {
private Activity activity;
private ViewGroup viewGroup;
private CocosSurfaceView mSurfaceView;
private boolean mDestroyed;
private boolean engineInit = false;
public CocosNativeHelper(FragmentActivity activity, ViewGroup viewGroup) {
this.activity = activity;
this.viewGroup = viewGroup;
activity.getLifecycle().addObserver(this);
GlobalObject.setActivity(activity);
CocosHelper.registerBatteryLevelReceiver(activity);
CocosHelper.init(activity);
CanvasRenderingContext2DImpl.init(activity);
onLoadNativeLibraries();
initView();
}
protected void initView() {
ViewGroup.LayoutParams frameLayoutParams = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
mSurfaceView = new CocosSurfaceView(activity);
mSurfaceView.getHolder().addCallback(this);
viewGroup.addView(mSurfaceView);
}
//设置游戏路径
public void start(String path) {
onCreateNative(activity, activity.getAssets(), getAbsolutePath(activity.getObbDir()), Build.VERSION.SDK_INT, path);
}
public void onDestroy() {
onSurfaceDestroyNative();
CocosHelper.unregisterBatteryLevelReceiver(activity);
CanvasRenderingContext2DImpl.destroy();
restartVm();
viewGroup.removeView(mSurfaceView);
}
public void onPause() {
onPauseNative();
}
public void onResume() {
onResumeNative();
}
public void onStop() {
onStopNative();
}
public void onStart() {
onStartNative();
}
public void onLowMemory() {
onLowMemoryNative();
}
private static String getAbsolutePath(File file) {
return (file != null) ? file.getAbsolutePath() : null;
}
private void onLoadNativeLibraries() {
try {
ApplicationInfo ai = activity.getPackageManager().getApplicationInfo(activity.getPackageName(), PackageManager.GET_META_DATA);
Bundle bundle = ai.metaData;
String libName = bundle.getString("android.app.lib_name");
System.loadLibrary(libName);
} catch (Exception e) {
e.printStackTrace();
}
}
private native void onCreateNative(Activity activity, AssetManager resourceManager, String obbPath, int sdkVersion, String path);
private native void onSurfaceCreatedNative(Surface surface);
private native void onSurfaceChangedNative(int width, int height);
private native void onSurfaceDestroyNative();
private native void onPauseNative();
private native void onResumeNative();
private native void onStopNative();
private native void onStartNative();
private native void onLowMemoryNative();
private native void onWindowFocusChangedNative(boolean hasFocus);
private native void restartVm();
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (!mDestroyed) {
onSurfaceCreatedNative(holder.getSurface());
engineInit = true;
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
if (!mDestroyed) {
onSurfaceChangedNative(width, height);
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (!mDestroyed) {
onSurfaceDestroyNative();
engineInit = false;
}
}
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
public void onOwnerCreate() {
SDKWrapper.shared().init(activity);
}
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
public void onOwnerOnResume() {
SDKWrapper.shared().onResume();
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void onOwnerOnStart() {
SDKWrapper.shared().onStart();
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
public void onOwnerOnPause() {
SDKWrapper.shared().onPause();
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
public void onOwnerOnStop() {
SDKWrapper.shared().onStop();
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void onOwnerDestroy() {
SDKWrapper.shared().onDestroy();
onSurfaceDestroyNative();
}
}
第二步:修改JniCocosActivity.cpp,使资源能够动态加载
为了后续游戏容器能动态设置本地下的游戏资源,需要修改JniCocosActivity.cpp
里的Java_com_cocos_lib_CocosActivity_onCreateNative
方法,增加路径参数 filePath
,截图和代码都在下面
if (!defaultResourcePath.empty()) {
cc::FileUtilsAndroid::getInstance()->setDefaultResourceRootPath(defaultResourcePath);
}
第三步:现在已经可以动态加载游戏,但是游戏切换还需要处理
在JniCocosActivity.cpp中,找到createGame
方法的调用,在else中增加 game->restartVM();
,
就可以做到游戏切换了,至此完结