如何在编译cocos2d项目加入自己的脚本

如何在编译cocos2d项目加入自己的脚本,第1张

概述COCOS2D-CONSOLE Cocos2d-console Overview Requirement Setup Environment Usage Attentions How to add command Add custom steps during compiling Advanced Features Contributing Overview cocos2d-console is COCOS2D-CONSolE Cocos2d-console Overview Requirement Setup Environment Usage Attentions How to add command Add custom steps during compiling Advanced Features Contributing OvervIEw

cocos2d-console is a command line tool for cocos2d-x & cocos2d-Js. It contains several commands for developers tocreate,compile,runthe-xor-Jsprojects.
The cocos2d-console is implemented byPython. You can use it inwindows,Mac or linux.

Requirement Python 2.7 is required(Now only support version 2.7). Required by iOS & Mac: XCode Required by android: Android NDK Android SDK Apache Ant required by win32: Visual Studio required by linux: Please run thebuild/install-deps-linux.shincocos2d-xorcocos2d-Jsto install the dependencIEs. Setup Environment

Run thesetup.pyin the root directory ofcocos2d-js.
If you are developing games for android,you should input theAndroid SDK,Android NDK&Apache Antpath when the setup is running.

Usage

Use the command line like this:cocos [command] [arguments]
Available commands:

new compile deploy run jscompile luacompile gen-libs gen-simulator gen-templates package Attentions

There are depend relationship between commands. Two rules about dependencies:

The dependent commands will be invoked first. If command A depend on command B. When you are using command A,the arguments you used will be passed to command B.

For example: Commandrundepends on commanddeploy,anddeploydepends oncompile.

The command order iscompile->deploy->run. You can use commandrunlike this:cocos run -p android --ndk-mode release
(-pis available argument ofrun,249)">--ndk-modeis available argument ofcompile) How to add command

Editbin/cocos2d.ini,and add the class name of your new plugin there. For example:

plugin_custom.CCPluginCustom

Create a file calledplugin_custom.pyin thepluginsfolder.
A new,empty plugin,would look like the code shown below:

import cocos# Plugins should be a subclass of CCPluginclass CCPluginCustom(cocos.CCPlugin):       # in default category    @staticmethod    def plugin_category():        return ""    @staticmethod    def plugin_name():        return "custom"    @staticmethod    def brief_description():        return "A custom plugin"                    def run(self,argv,dependencies):        print "plugin called!"        print argv

Check the added command. The output ofcocos -hwill show the available commands:


The result of run the commandcocos custom -p androidwill look like this:

Add custom steps during compiling

Whencocos compileis running,some events will be dispatched to the custom python scripts. You can use this function by these steps:

Define your custom script in.cocos-project.jsonin the root of your project. like this:

..."custom_step_script": "./custom_script.py",...

The key must becustom_step_script. The value can be absolute or relative path.

Define a method namedhandle_eventin your custom script like this:

# custom_script.py# event is the event name which pre-defined in cocos command.# target_platform is the target platform which you are compiling for.# args is more arguments of event.def handle_event(event,target_platform,args):  print("event is %s" % event)  print("target_platform is %s" % target_platform)  print("args is %s" % args)

Then your custom script will be invoked when you are compiling your project bycocos compile.

Here are the pre-defined events:

event support platforms special arguments
pre-build all platforms ---
post-build pre-ndk-build android post-ndk-build pre-copy-assets assets-dir: The path of assets directory
post-copy-assets pre-ant-build post-ant-build ---

All events arguments will contains:

project-path : The path of project root directory. platform-project-path : The path of current compiling platform directory. build-mode : The value of parameter-m. It'sreleaseordebug output-dir : The path of output directory. ndk-build-mode : The value of parameter--ndk-mode. It'sdebugornone. (This argument is only available when target platform is android). Advanced Features How to exclude files in resources directory when building for android Contributing

The code repository ofcocos2d-consoleis on github:console repository
You can fork this github repository,add your contribution,then send a pull request to us.

available_cmds.jpg(54.9 kB)zhangbin@cocos2d-x.org,2014-06-16 02:39

run_custom.jpg(19.7 kB) 总结

以上是内存溢出为你收集整理的如何在编译cocos2d项目加入自己的脚本全部内容,希望文章能够帮你解决如何在编译cocos2d项目加入自己的脚本所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1078698.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存