206 lines
6.9 KiB
Plaintext
206 lines
6.9 KiB
Plaintext
import { _decorator, isValid, Label, Node, Size, UITransform, Vec3, view } from "cc";
|
|
import li_EventManager from "./li_EventManager";
|
|
|
|
const { ccclass, property } = _decorator;
|
|
|
|
@ccclass
|
|
export default class Utils {
|
|
//log日志
|
|
public static Log(msg: string | any, ...subst: any[]) {
|
|
let IsLocalCSChanel = true
|
|
if (IsLocalCSChanel) {
|
|
console.log(msg, subst)
|
|
}
|
|
}
|
|
//警告日志
|
|
public static Warning(msg: string | any, ...subst: any[]) {
|
|
let IsLocalCSChanel = true
|
|
if (IsLocalCSChanel) {
|
|
console.warn(msg, subst)
|
|
}
|
|
}
|
|
//错误日志
|
|
public static Error(msg: string | any, ...subst: any[]) {
|
|
let IsLocalCSChanel = true
|
|
if (IsLocalCSChanel) {
|
|
console.error(msg, subst)
|
|
}
|
|
}
|
|
|
|
|
|
//解析节点
|
|
public static parseNode = function (node: Node, oriObj?: any) {
|
|
if (!node || !node.children) return;
|
|
oriObj = oriObj || node;
|
|
let chs = node.children;
|
|
for (let i = 0; i < chs.length; i++) {
|
|
let ch = chs[i];
|
|
let chName = ch.name;
|
|
if (chName && chName.length > 0) {
|
|
try {
|
|
oriObj[chName] = ch;
|
|
} catch (error) {
|
|
//hg_utils.hgLog("chName err:", chName);
|
|
}
|
|
}
|
|
Utils.parseNode(ch, oriObj);
|
|
}
|
|
}
|
|
|
|
|
|
//更换父节点,保留原有的坐标和旋转
|
|
public static changeNodeParent(node:Node, parentNode:Node) {
|
|
let world_Q = node.getWorldRotation()
|
|
let world_pos = node.getWorldPosition()//原来的世界坐标
|
|
node.setParent(parentNode)
|
|
node.setRotation(world_Q);
|
|
node.setWorldPosition(world_pos)
|
|
}
|
|
|
|
|
|
/**
|
|
* 封装setString方法
|
|
*/
|
|
static setString(node: Node, str: string | number) {
|
|
if (!isValid(node)) return;
|
|
let lb:any = node.getComponent(Label);
|
|
if (!lb) return;
|
|
if(str == null) {str = ""}
|
|
str += ""
|
|
lb.string = str;
|
|
}
|
|
|
|
|
|
/**适配背景图
|
|
* @method node 背景图节点
|
|
* @param mod 适配模式 1=按高度适配 2=按宽度适配
|
|
*/
|
|
public static adjustBgPixelRatio(node:Node, mod:number = 1) {
|
|
let windowSize = view.getVisibleSize();
|
|
let bgTf = node.getComponent(UITransform)
|
|
if (bgTf) {
|
|
if (mod == 1 && bgTf.height < windowSize.height) {
|
|
let scale = windowSize.height / bgTf.height
|
|
bgTf.height = bgTf.height * scale
|
|
bgTf.width = bgTf.width * scale
|
|
} else if (mod == 2 && bgTf.width < windowSize.width) {
|
|
let scale = windowSize.width / bgTf.width
|
|
bgTf.height = bgTf.height * scale
|
|
bgTf.width = bgTf.width * scale
|
|
}
|
|
}
|
|
}
|
|
/**适配背景图 按比例缩放 屏幕比设计分辨率小时缩小,反之则不缩放
|
|
* @param node 背景图节点
|
|
* @param mod 适配模式 1=按高度适配 2=按宽度适配
|
|
*/
|
|
public static adjustBgScaleRatio(node:Node, mod:number = 1) {
|
|
let windowSize = view.getVisibleSize();
|
|
let designSize = view.getDesignResolutionSize();
|
|
if (mod == 1 && windowSize.height < designSize.height) {
|
|
let scale = windowSize.height / designSize.height
|
|
node.scale = new Vec3(scale, scale, 1)
|
|
} else if (mod == 2 && windowSize.width < designSize.width) {
|
|
let scale = windowSize.width / designSize.width
|
|
node.scale = new Vec3(scale, scale, 1)
|
|
}
|
|
}
|
|
/**获取屏幕分辨率和设计分辨率的比例
|
|
* @method mod 适配模式 1=按高度适配 2=按宽度适配
|
|
*/
|
|
public static getScaleRatio(mod:number = 1) {
|
|
let windowSize = view.getVisibleSize();
|
|
let designSize = view.getDesignResolutionSize();
|
|
console.log("游戏分辨率:", windowSize, designSize);
|
|
if (mod == 1) {
|
|
return windowSize.height / designSize.height
|
|
} else {
|
|
return windowSize.width / designSize.width
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @method 浅复制一个对象
|
|
* @param source 需要浅复制的对象
|
|
* 合并对象,如果excludes中没有指明需要排除的字段,target也含有相同的字段,则会被object同名字段值覆盖掉
|
|
* @param object
|
|
* @param excludes 需要排除的字段
|
|
*/
|
|
static applyIf(object: any, target: any = {}, excludes: Array<string> = []): Object {
|
|
if (!target) target = {};
|
|
for (let key in object) {
|
|
if (object.hasOwnProperty(key)) {
|
|
if (excludes.indexOf(key) < 0) {
|
|
target[key] = object[key];
|
|
}
|
|
}
|
|
}
|
|
return target;
|
|
}
|
|
/**
|
|
* @method 浅复制一个对象
|
|
* @param source 需要浅复制的对象
|
|
* @return 返回一个新的对象
|
|
* @log 1. vincent,2018-12-18, func、date、reg 和 err 类型不能正常拷贝
|
|
*/
|
|
static easyCopy(source: Object): Object {
|
|
const newObject = [];
|
|
return Utils.applyIf(source);
|
|
}
|
|
/**复制一个数据表 */
|
|
static clone(data:any) {
|
|
return Utils.easyCopy(data);
|
|
}
|
|
|
|
|
|
/**发送内部事件 */
|
|
public static sendInnerMsg(innerId: number, param?: any) {
|
|
li_EventManager.I.onInnerEL(innerId, param);
|
|
}
|
|
/**注册内部事件 */
|
|
public static addInnerEL(innerId: number, i_target, i_callback: Function,) {
|
|
li_EventManager.I.addInnerEL(innerId, i_callback, i_target);
|
|
}
|
|
/**移除内部事件 */
|
|
public static removeInnerEL(innerId: number, i_target, i_callback: Function) {
|
|
li_EventManager.I.removeInnerEL(innerId, i_callback, i_target)
|
|
}
|
|
|
|
|
|
//获取随机数
|
|
public static getRandomInt(i_start:number, i_end:number) : number {
|
|
i_start = Math.ceil(i_start);
|
|
i_end = Math.floor(i_end);
|
|
return Math.floor(Math.random() * (i_end - i_start + 1)) + i_start;
|
|
}
|
|
//数字转字符串,指定位数,位数不足前面补0
|
|
public static PrefixInt(num, length) {
|
|
return (Array(length).join('0') + num).slice(-length);
|
|
}
|
|
//数字保留几位小数
|
|
public static DecimalPlaces(num:number, weishu:number) {
|
|
return num.toFixed(weishu);
|
|
}
|
|
|
|
|
|
/**获取屏幕分辨率 */
|
|
public static GetWinSize():Size {
|
|
return view.getVisibleSize()
|
|
}
|
|
|
|
|
|
//打印消耗时间===================================================================================================
|
|
private static _eplTime = 0;
|
|
/**记录当前时间 */
|
|
public static ProfilerTimeRecord() {
|
|
this._eplTime = new Date().getTime();
|
|
}
|
|
/**打印消耗时间 */
|
|
public static ProfilerTimePrint(key:string = "") {
|
|
let time = new Date().getTime();
|
|
let timecha = time - this._eplTime;
|
|
console.log(`${key}消耗时间:${timecha}毫秒`);
|
|
this._eplTime = time;
|
|
}
|
|
} |