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

COCOS2D-CONSOLE

Overview

cocos2d-console is a command line tool for cocos2d-x & cocos2d-js. It contains several commands for developers to create, compile, run the -x or -js projects.
The cocos2d-console is implemented by Python. You can use it in Windows, Mac or Linux.

Requirement

  • Python 2.7 is required (Now only support version 2.7).
  • Required by iOS & Mac:
    • XCode
  • Required by android:
  • Required by win32:
    • Visual Studio
  • Required by linux:
    • Please run the build/install-deps-linux.sh in cocos2d-x or cocos2d-js to install the dependencies.

Setup Environment

Run the setup.py in the root directory of cocos2d-x or cocos2d-js.
If you are developing games for android, you should input the Android SDKAndroid NDK & Apache Ant path when the setup is running.

Usage

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

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: Command run depends on command deploy, and deploy depends on compile.

  • The command order is compile->deploy->run.
  • You can use command run like this: cocos run -p android --ndk-mode release
    (-p is available argument of run--ndk-mode is available argument of compile)

How to add command

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

    plugin_custom.CCPluginCustom
    
  2. Create a file called plugin_custom.py in the plugins folder.
    A new, empty plugin, would look like the code shown below:

    import cocos
    
    # Plugins should be a subclass of CCPlugin
    class 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
    
  3. Check the added command. The output of cocos -h will show the available commands:

    available commands
    The result of run the command cocos custom -p android will look like this:

    run custom

Add custom steps during compiling

When cocos compile is running, some events will be dispatched to the custom python scripts. You can use this function by these steps:

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

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

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

  2. Define a method named handle_event in 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 by cocos compile.

Here are the pre-defined events:

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

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's release or debug
  • output-dir : The path of output directory.
  • ndk-build-mode : The value of parameter --ndk-mode. It's release or debug or none. (This argument is only available when target platform is android).

Advanced Features

Contributing

The code repository of cocos2d-console is 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) zhangbin@cocos2d-x.org, 2014-06-16 02:39

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值