Code Monkey home page Code Monkey logo

creator-native-bridge's Introduction

cocos-creator Native Caller

从javascript调用本地代码的辅助工具。

  • 无需关心什么类型签名、包名。调用android和oc的代码相同
  • 支持把js函数直接传入本地代码,处理完功能直接调用js回调

javascript

native.callClass("SomeClass", "FuncName", "your string", 2017, true, (str, number, bool) => {
    cc.log(str, number, bool)
})

objective-c

@interface SomeClass : NSObject 
+(void) FuncName : (NSString*) str
        arg1 : (NSNumber) number 
        arg2 : (BOOL) b
        arg3 : (NSString*) cbName {
            JS::call(cbName, @[@"your string", @2017, Yes]);
        }
@end

java

class SomeClass {
    static public void FuncName(String s, double d, boolean b, String cbName) {
        Js.call(cbName, "your string", 2017, true);
    }
}

使用方法

  • 拷贝Native.js 到你的creator项目中
  • 拷贝Js.mm & Js.h 到你的ios目录,并添加到xcode中
  • 拷贝Js.java 到安卓 org/cocos2dx/javascript 下
  • ios这边因为Api经常变,需要设置一下js调用方式: 如果有报错请自行修改。
    Js::setEvalFunc([](std::string content) {
        if (!se::ScriptEngine::getInstance()->evalString(content.c_str())) {
            NSLog(@"evalString fail.");
        }
    });

注意

  • 在ios端调用js时候,bool值必须使用 Yes 和 No
  • 在ios端 函数多个参数 必须写成arg1 arg2 ... arg10这样。
  • js回调函数在本地代码中用string类型接收
  • js回调调用一次之后就会被移除。
  • 代码比较少,有bug请自行修改,当然也可以给个Pr。

creator-native-bridge's People

Contributors

qcdong2016 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

creator-native-bridge's Issues

Classloader failed to find class of XXX

JS:
native.callClass("NativeTest", "test", "Hello Native", 123, true, (str, number, bool) => {
console.log(str, number, bool)
that.label.string = str;
})

JAVA:
package org.cocos2dx.javascript;
class NativeTest {
static public void test(String s, float d, boolean b, String cbName) {
System.out.print("NativeTest test:" + s);
Js.call(cbName, "Hello Js", 321, false);
}
}

JS: clazz: NativeTest
JS: method: test
JS: methodSignature: (Ljava/lang/String;FZLjava/lang/String;)V
JS: parameters: Hello Native 123 true 1

检查了路径个参数都对,不知为何Classloader failed to find class of:

D/JniHelper: JniHelper::getJavaVM(), pthread_self() = -1053613600
D/JavaScriptJavaBridge: Classloader failed to find class of org/cocos2dx/javascript/NativeTest
E/JavaScriptJavaBridge: [ERROR] (/Applications/CocosCreator/Creator/2.4.4/CocosCreator.app/Contents/Resources/cocos2d-x/cocos/scripting/js-bindings/manual/JavaScriptJavaBridge.cpp, 605): call valid: 0, call.getArgumentsCount()= 4
E/JavaScriptJavaBridge: [ERROR] Failed to invoke JavaScriptJavaBridge_callStaticMethod, location: /Applications/CocosCreator/Creator/2.4.4/CocosCreator.app/Contents/Resources/cocos2d-x/cocos/scripting/js-bindings/manual/JavaScriptJavaBridge.cpp:611

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.