import { PREVIEW } from "cc/env"; import { director, sys } from "cc"; import LocalStorage from "../Mgr_LocalData/LocalStorage"; export class Model { private readonly saveKey = "ifOpenVibration"; private vibrationSetting = { Vibration: true, } private curFarme:number = 0; private saveSetting() { LocalStorage.setItem(this.saveKey, this.vibrationSetting); } /**平台 */ private readonly Platform = window['ks'] || window["wx"] || window["tt"]; /**获取 震动状态 */ get vibration(){ return this.vibrationSetting.Vibration; } /**初始化 */ init(){ let Value = LocalStorage.getItem(this.saveKey); if (!Value) { Value = this.vibrationSetting; } this.vibrationSetting = Value; } /**切换震动状态 */ switchVibration() { this.vibrationSetting.Vibration = !this.vibrationSetting.Vibration; this.saveSetting(); } startVibrate(Long: boolean = false) { if (!this.vibrationSetting.Vibration) { return; } const frame = director.getTotalFrames(); if (this.curFarme == frame) { return; } this.curFarme = frame; if(!this.Platform){ !PREVIEW && ConsoleEx.Warn("没有对应的震动平台调用!") return; } if(Long){ this.Platform.vibrateLong({ type: "medium", success: function () { }, fail: function () { }, complete: function () { } }); }else{ this.Platform.vibrateShort({ type: "medium", success: function () { }, fail: function () { }, complete: function () { } }); return ; } !PREVIEW && ConsoleEx.Warn("没有对应的震动平台调用!") } } export const VibrateUtils = new Model();