JM_KA/assets/Script/NativeClass.ts

247 lines
6.4 KiB
TypeScript
Raw Normal View History

2022-08-25 09:49:29 +00:00
// import Text_to_Speech from "./Text_to_Speech";
2022-08-29 07:25:13 +00:00
import { System_Eevent } from "./Engine/CatanEngine/CSharp/System/System_Eevent";
2022-08-25 06:25:48 +00:00
const { ccclass } = cc._decorator;
@ccclass
export default class NativeClass extends cc.Component {
//#region static 屬性
private static _instance: NativeClass = null;
public static get Instance(): NativeClass { return NativeClass._instance; }
//#endregion
//#region public 屬性
2022-08-29 07:25:13 +00:00
public WebView: cc.WebView = null;
2022-08-25 06:25:48 +00:00
public URLscheme: Object = {};
public URLschemeFlags: number = 0;
//#endregion
2022-08-25 09:49:29 +00:00
//#region private
// private _text_to_Speech: Text_to_Speech;
//#endregion
2022-08-25 06:25:48 +00:00
//#region Lifecycle
2022-08-29 07:25:13 +00:00
constructor(...param: any[]) {
2022-08-25 06:25:48 +00:00
super();
NativeClass._instance = this;
2022-08-29 07:25:13 +00:00
this.WebView = param[0];
2022-08-25 06:25:48 +00:00
this.init();
}
private init(): void {
let self: this = this;
2022-08-25 09:49:29 +00:00
// this._text_to_Speech = new Text_to_Speech();
2022-08-25 06:25:48 +00:00
let HttpData: any[] = [];
// let HttpParmData: any[] = [];
// 註冊要被Java、OC呼叫的事件
if (!window["JSBridge"]) {
window["JSBridge"] = function (method: string = "", value: string = ""): void {
if (method || self[method]) {
if (value) {
self[method](value);
} else if (self[method]) {
self[method]();
} else {
console.log(`not function: ${method}, value: ${value}`);
}
} else {
console.log(`not function: ${method}, value: ${value}`);
}
};
}
if (cc.sys.isBrowser) {
let url: string = window.location.search;
// let request: object = [];
if (url.indexOf("?") !== -1) {
let str: string = url.substring(1);
let strs: string[] = str.split("&");
for (let i: number = 0; i < strs.length; i++) {
this.URLscheme[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]);
}
}
} else {
switch (cc.sys.os) {
case cc.sys.OS_WINDOWS: {
// cc.sys.platform = 101(DESKTOP_BROWSER)
break;
}
case cc.sys.OS_ANDROID: {
// cc.sys.platform = 3(Android)
// 呼叫Java取得由URL打開時的網址(呼叫getIntent 無帶入參數(null) 回傳字串((Ljava/lang/String;)Ljava/lang/String;))
/** 取得由URL打開時的網址(內容是自己取得後用特殊符號拼成字串) */
let scheme: any = jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "getURLData", "(Ljava/lang/String;)Ljava/lang/String;", null);
if (scheme && scheme !== "null") {
let _Get_Arr: string[] = scheme.split("?");
let Get_Arr: string[] = _Get_Arr[1].split("#");
for (let i: number = 0; i < Get_Arr.length; i++) {
let Get: string[] = Get_Arr[i].split("$");
HttpData[Get[0]] = Get[1];
}
let Query: string = HttpData["Query"];
if (Query) {
Get_Arr = Query.split("&");
for (let i: number = 0; i < Get_Arr.length; i++) {
let Get: string[] = Get_Arr[i].split("=");
this.URLscheme[Get[0]] = Get[1];
}
}
}
// cc.log("getIntent OK, " + getIntent);
break;
}
case cc.sys.OS_IOS: {
// cc.sys.platform = 4(iPhone)
// 呼叫OC取得由URL打開時的網址(呼叫getIntent 無帶入參數 回傳字串)(開發中功能,預計要取得由URL打開時的網址)
/** 取得由URL打開時的網址(內容是自己取得後用特殊符號拼成字串) */
let scheme: any = jsb.reflection.callStaticMethod("AppController", "getIntent");
if (scheme && scheme !== "null") {
let Get_Arr: string[] = scheme.split("#");
for (let i: number = 0; i < Get_Arr.length; i++) {
let Get: string[] = Get_Arr[i].split("$");
HttpData[Get[0]] = Get[1];
}
let Query: string = HttpData["Query"];
if (Query) {
Get_Arr = Query.split("&");
for (let i: number = 0; i < Get_Arr.length; i++) {
let Get: string[] = Get_Arr[i].split("=");
this.URLscheme[Get[0]] = Get[1];
}
}
}
// cc.log("getIntent OK, " + getIntent);
break;
}
default: {
break;
}
}
}
}
//#endregion
//#region Custom Function
2022-08-29 07:25:13 +00:00
/** CocosBridge */
public CocosBridge(type: System_Eevent, ...param: any[]): void {
let value: string = "";
for (let i: number = 0; i < param.length; i++) {
let element: any = param[i];
if (element === "") {
element = "\"\"";
}
value += `,${element}`;
}
let command: string = `window.CocosBridge(${type}${value})`;
this.WebView.evaluateJS(command);
}
2022-08-26 08:36:46 +00:00
/** GetFCMToken */
public GetFCMToken(): string {
let resp: any = "";
switch (cc.sys.os) {
case cc.sys.OS_WINDOWS: {
// 網頁端
break;
}
case cc.sys.OS_ANDROID: {
break;
}
case cc.sys.OS_IOS: {
resp = jsb.reflection.callStaticMethod("AppController", "GetFCMToken");
break;
}
default: {
break;
}
}
return resp;
}
2022-08-25 06:25:48 +00:00
/**
* TTS_Play
* @param {string} msg msg
*/
public TTS_Play(msg: string): void {
switch (cc.sys.os) {
case cc.sys.OS_WINDOWS: {
2022-08-26 08:36:46 +00:00
// 網頁端
2022-08-25 06:25:48 +00:00
break;
}
case cc.sys.OS_ANDROID: {
jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "TTS_Play", "(Ljava/lang/String;)V", msg);
2022-08-25 09:49:29 +00:00
// this._text_to_Speech.speak(msg);
2022-08-25 06:25:48 +00:00
break;
}
case cc.sys.OS_IOS: {
jsb.reflection.callStaticMethod("AppController", "TTS_Play:", msg);
break;
}
default: {
break;
}
}
}
public HideNativeSplash(): void {
if (CC_JSB) {
if (cc.sys.os === cc.sys.OS_ANDROID) {
// 反射调用原生的隐藏方法
jsb.reflection.callStaticMethod(
"org/cocos2dx/javascript/AppActivity",
"hideSplash",
"()V"
);
}
}
}
public CCLOG(msg: String): void {
console.log(msg);
}
public DispatchCallback(msg: string): void {
let data: string[] = msg.split("=");
let data1: string[] = data[1] ? data[1].split("#") : [];
let scenename: string = cc.director.getScene().name;
// cc.director.on(cc.Director.EVENT_AFTER_SCENE_LAUNCH, callback, this);
// this.NativeCallback.DispatchCallback(data[0], data1);
}
public ClearAllCache(): void {
cc.sys.localStorage.clear();
cc.assetManager.bundles.clear();
cc.assetManager.cacheManager.clearCache();
cc.game.restart();
}
//#endregion
}
//#region Enum
export enum DeepLink {
ClearAllCache = "ClearAllCache",
}
//#endregion