Version: 1.7
语言 : 中文
Call TypeScript plug-in code from C# scripts
Custom method Call UI thread worker from TypeScript plug-in code (.etslib)

Call C# scripts from TypeScript

As mentioned above Call TypeScript plug-in code from C# scripts provides C# APIs that communicate with the OpenHarmony. Tuanjie also provides TypeScript APIs that communicate with the Tuanjie Player.

High-level API

The high-level TuanjieSendMessage API can be called in TypeScript to send messages to TuanjiePlayer, invoking the C# script.

Here is the function declaration for the TuanjieSendMessage API

/**
 * Calls the method named methodName on every MonoBehaviour in this game object. 
 * @param gameObjectName - Name of the GameObject that mount script in Scene.
 * @param methodName - Name of the method to call. 
 * @param options - Optional parameter for the method.
*/
public static TuanjieSendMessage(gameObjectName: string, methodName: string, options: string): void {
  tuanjie.TuanjieSendMessage(gameObjectName, methodName, options);  
}

Demo

This section contains code samples that show how to use the TuanjieSendMessage.

Call C# method from Custom TypeScript

First, Create a GameObject and name it TsGameObject, then create and attach the following C# script to the TsGameObject.

public class JsCallCSharp : MonoBehaviour
{
    public void Test(string param)
    {
        Debug.Log($"Js Call CSharp Success: {param}");
    }
}

Second, Call TuanjieSendMessage in TypeScript and ensure TsGameObject is created and not destroyed.

...
import { Tuanjie } from 'tuanjieLib';  
export default class TuanjiePlayerAbility extends TuanjiePlayerAbilityBase {  
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {  
    this.setConfig();  
    super.onCreate(want, launchParam);  
    Tuanjie.TuanjieSendMessage("TsGameObject","Test","123");
  }
...
}
Call TypeScript plug-in code from C# scripts
Custom method Call UI thread worker from TypeScript plug-in code (.etslib)