Giter Club home page Giter Club logo

cocos-creator-android's Introduction

allprojects {
        repositories {
            ...
            maven { url 'https://jitpack.io' }
        }
}
dependencies {
       implementation 'com.github.qq326646683:cocos-creator-android:2.0.1'
}

一、如何集成

  1. 文件读写、网络权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
  1. 下载游戏zip并解压
  2. 继承CocosActivity,并将解压后的路径赋值给filePath
class CocosGameActivity: CocosActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun filePath() = intent.getStringExtra("path")
}
  1. 清单文件
 <application>
        <meta-data
            android:name="android.app.lib_name"
            android:value="cocos" />
        <activity android:name=".CocosGameActivity" android:process=":cocos"/>
  1. 本篇的module和事例app代码放在gitlab

二、如何通信

1.实现弹出Android的Dialog,选择后把结果传给cocos显示(不需要主进程的数据,可以直接1->4) android/CocosGameActivity.kt

private val showArray = arrayOf("刘德华", "周华健")
private val cocosListenerInCocos: CocosDataListener = CocosDataListener { action, argument, callbackId ->
  CocosBridgeHelper.log("接收InCocos", action)
  if (action == "action_showStarDialog") {
      runOnUiThread {
          AlertDialog.Builder(this)
              .setTitle("选择")
              .setItems(showArray) { _, index ->
                  CocosBridgeHelper.getInstance().nativeCallCocos(action, showArray[index], callbackId)
              }
              .create()
              .show()
      }
  }

}

override fun onCreate(savedInstanceState: Bundle?) {
  super.onCreate(savedInstanceState)
  CocosBridgeHelper.getInstance().addCocosListener(cocosListenerInCocos)
}
override fun onDestroy() {
  super.onDestroy()
  CocosBridgeHelper.getInstance().removeCocosListener(cocosListenerInCocos)
}

2.实现从主进程取数据给cocos显示 android/MainActivity.kt

private val cocosListenerInMain: CocosDataListener = CocosDataListener { action, argument, callbackId ->
  CocosBridgeHelper.log("接收InMain", action)
  if (action == "action_appVersion") {
      CocosBridgeHelper.getInstance().main2Cocos(action, packageManager.getPackageInfo(packageName, 0).versionName, callbackId)
  }
}
override fun onCreate(savedInstanceState: Bundle?) {
  CocosBridgeHelper.getInstance().addMainListener(cocosListenerInMain)
}

override fun onDestroy() {
  super.onDestroy()
  CocosBridgeHelper.getInstance().removeMainListener(cocosListenerInMain)
}

三、实现原理:

Android实战——Cocos游戏容器搭建篇

Android实战——Cocos游戏容器通信篇

cocos-creator-android's People

Contributors

qq326646683 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.