Cocos Creator 3.x native TS interaction

Foreword: Cocos creator3.x has changed a lot, and many APIs have changed. Fill in a hole here for you;

I am using version 3.5.2 here

Android:

TS calls java: 

Official document: How to use JavaScript to directly call Java methods on the Android platform Cocos Creator

TS script in creator:


if (sys.os == sys.OS.ANDROID) {
    let className = "com/cocos/game/AppActivity";
    let methodName = "showAd_unified";
    let methodSignature = "()V";
    jsb.reflection.callStaticMethod(className, methodName, methodSignature);
} else if (sys.os == sys.OS.IOS) {

}
  • className: The class file name path in the native project , corresponding to the script name
  •  methodName: the corresponding static
    private static void showAd_unified(){
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showInsertAd();
            }
        });
    }
  •  methodSignature: static method parameter type, also called method signature

The method signature is a little more complicated, the simplest method signature is ()Vthat it represents a method with no parameters and no return value. Some other examples:

  • (I)VA method that indicates that the parameter is an int and has no return value
  • (I)IIndicates that the parameter is an int and the return value is an int method
  • (IF)ZIndicates that the parameter is an int and a float, and the return value is a method of boolean

The symbol inside the parentheses indicates the parameter type , and the symbol after the parentheses indicates the return value type. Because Java allows function overloading, there can be multiple methods with the same method name but different parameter return values, and the method signature is used to help distinguish these methods with the same name.

Currently, there are four types of Java type signatures supported in Cocos Creator:

Java type sign
int I
float F
boolean FROM
String Ljava/lang/String;

 The parameters can be zero or any number, just use number, bool and string in JavaScript directly.

Example of use:

// 调用 hello 方法
jsb.reflection.callStaticMethod("com/cocos/game/Test", "hello", "(Ljava/lang/String;)V", "this is a message from JavaScript");

// 调用第一个 sum 方法
var result = jsb.reflection.callStaticMethod("com/cocos/game/Test", "sum", "(II)I", 3, 7);
log(result); // 10

// 调用第二个 sum 方法
var result = jsb.reflection.callStaticMethod("com/cocos/game/Test", "sum", "(I)I", 3);
log(result); // 5

Summary: There are two points to note :

  1. TS can only call Java static methods :
  2. It should be that Cocos Creator is in the GL thread, so switch to the UI thread when executing Java methods
    
    private static void showAd_unified(){
        // activity = this;
        // activity 是当前的 activtiy
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                /// TODO
            }
        });
    }

Java calls TS: 

There's a lot of change here

The previous method was this: ( I later found that  runOnGLThread didn't click on it )

activty.runOnGLThread(new Runnable() {
	@Override
	public void run() {
        //相当于直接在项目运行后的console里执行代码 eval() 字符串形式
		Cocos2dxJavascriptJavaBridge.evalString("cc.log(\"Javascript Java bridge!\")");
	}
});

3.X already looks like this:

 

CocosHelper.runOnGameThread(new Runnable() {
    @Override
    public void run() {
        CocosJavascriptJavaBridge.evalString(String.format("cc.find('Canvas/Layout').getComponent('NativeMannager').showAnimation(%s);", value));
    }
});

The following parameters are written very similar to unity. First find the corresponding Node node and take the above script and then call the method.

 

 When passing parameters, when the parameter is very long or the json string or in the parameter, you need to turn Base64

 

static void callJSFunction(final  String value){
    String strBase64 = Base64.encodeToString(value.getBytes(), Base64.DEFAULT);
    final  String jsCall = String.format("cc.find('Canvas/Layout').getComponent('NativeMannager').showAnimation('%s');", strBase64);
    Log.d("AdvertisingManager",jsCall);
    CocosHelper.runOnGameThread(new Runnable() {
        @Override
        public void run() {
            CocosJavascriptJavaBridge.evalString(jsCall);
            //JsbBridge.sendToScript();
        }
    });
}

Ios:

(I don't have a mac to add it later)

Guess you like

Origin blog.csdn.net/nicepainkiller/article/details/126378580