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.
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);
}
This section contains code samples that show how to use the TuanjieSendMessage.
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");
}
...
}