Cocos creator 大厅子游戏和热更新

工作中,需要把cocos creator创建的多个游戏适配到Android和ios应用中,经过调研,可以利用大厅子游戏模式实现。大厅本身作为一个游戏工程,可以有加载页面,和热加载子游戏。

热更新:
https://www.jianshu.com/p/9fc813fe9e4c

大厅子游戏:
https://www.jianshu.com/p/fe54ca980384

如何动态加载和更新子游戏:
自从jsb 3.0以来,可以用反射调用Android或者ios的代码:

const SubgameManager = require('SubgameManager');

cc.Class({
    extends: cc.Component,

    properties: {

        downloadBtn: {
            default: null,
            type: cc.Node
        },
        
        label: {
            default: null,
            type: cc.Label
        },
        // defaults, set visually when attaching this script to the Canvas
        text: 'Hello, World!'
    },

    // use this for initialization
    onLoad: function () {

        var name = 'subgame';  

        if (cc.sys.OS_ANDROID == cc.sys.os) {
            name = jsb.reflection.callStaticMethod("org/cocos2dx/javascript/GameNameProvider", "getName", "()Ljava/lang/String;");
            console.log("OS_ANDROID platform provides: " + name);
        }  

        if (cc.sys.OS_IOS == cc.sys.os) {
            name = jsb.reflection.callStaticMethod("GameNameProvider", "getName");
            console.log("OS_IOS platform provides: " + name);
            
        }  

        //判断子游戏有没有下载
        if (SubgameManager.isSubgameDownLoad(name)) {
            //已下载,判断是否需要更新
            SubgameManager.needUpdateSubgame(name, (success) => {
                if (success) {
                    this.label.string = "子游戏需要更新";
                    console.log("子游戏需要更新");
                } else {
                    this.label.string = "子游戏不需要更新";
                    console.log("子游戏不需要更新");
                }
            }, () => {
                console.log('出错了');
            });
        } else {
            console.log("子游戏未下载");
            this.label.string = "子游戏未下载";
        }

        this.downloadBtn.on('click', () => {
            //下载子游戏/更新子游戏
            console.log("downloadBtn clicked");
            SubgameManager.downloadSubgame(name, (progress) => {
                if (isNaN(progress)) {
                    progress = 0;
                }
                this.label.string = "资源下载中   " + parseInt(progress * 100) + "%";
                console.log(this.label.string);
            }, function(success) {
                if (success) {
                    SubgameManager.enterSubgame(name);
                    console.log("进入子游戏");
                } else {
                    console.log('下载失败');
                }
            });
        }, this);
    },

    // called every frame
    update: function (dt) {

    },
});

Android代码:

package org.cocos2dx.javascript;

public class GameNameProvider {

    public static String getName() {
        return "subgame";
    }
}

iOS代码:

#import <Foundation/Foundation.h>

@interface GameNameProvider:NSObject {
}

+ (NSString *)getName;
@end
#import "GameNameProvider.h"
#import <Foundation/Foundation.h>

@implementation GameNameProvider

// request login
+ (NSString *) getName {
    return @"subgame";
}
@end

demo地址:
https://github.com/tigershinny/CocosHotupdate

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
CocosCreator游戏热更新是指在不重新发布游戏的情况下,通过网络下载新的资源或代码,实现游戏内容的更新。具体实现方式可以通过以下步骤来完成: 1. 在CocosCreator中使用资源管理器将需要热更新的资源打包成zip文件,并上传到服务器上。 2. 在游戏启动时,通过网络请求获取服务器上的版本信息和需要更新的资源列表。 3. 将需要更新的资源下载到本地,并进行解压和替换。 4. 重启游戏,使新的资源生效。 下面是一个简单的示例代码: ```javascript // 获取服务器版本信息和需要更新的资源列表 let xhr = cc.loader.getXMLHttpRequest(); xhr.open("GET", "http://yourserver/version.json", true); xhr.onreadystatechange = function () { if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 300)) { let response = xhr.responseText; let versionInfo = JSON.parse(response); let remoteVersion = versionInfo.version; let remoteAssets = versionInfo.assets; checkUpdate(remoteVersion, remoteAssets); } }; xhr.send(); // 检查是否需要更新 function checkUpdate(remoteVersion, remoteAssets) { let localVersion = cc.sys.localStorage.getItem("version"); if (localVersion !== remoteVersion) { // 需要更新 cc.sys.localStorage.setItem("version", remoteVersion); updateAssets(remoteAssets); } else { // 不需要更新 startGame(); } } // 更新资源 function updateAssets(remoteAssets) { let storagePath = (jsb.fileUtils ? jsb.fileUtils.getWritablePath() : "/") + "remote-assets"; let am = new jsb.AssetsManager("", storagePath, (version, asset) => { // 下载进度回调 }); am.updateRemoteManifest(remoteAssets); am.checkUpdate(); } // 启动游戏 function startGame() { // ... } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值