【Cocos Creator】UITools.ts

这篇博客介绍了一个名为`UITools`的实用工具类,该类包含一系列静态方法,用于处理Unity游戏开发中的常见操作,如按钮事件监听、节点的显示与隐藏、标签文本的设置、颜色调整、节点的定位、旋转、缩放等。此外,还提供了复制文本到剪贴板的功能。这些方法简化了Unity中UI元素的管理和交互,提高了开发效率。
摘要由CSDN通过智能技术生成
import { getColor } from "../utils/ClientUtils";

const { ccclass, property } = cc._decorator;
export default class UITools {

    /**
    * 
    * @param btn 点击按钮
    * @param callback 点击按钮事件
    * @param clickSoundName 点击按钮音效名
    * @param disableTime 禁用按钮时长
    */
    public static AddButtonEvent(btn: cc.Button, callback: Function, target: any, clickSoundName: string = "defaultSound", disableTime: number = 0.4) {

        if(clickSoundName =="defaultSound")
        {
           // clickSoundName = GameApplication.instance.staticConfig.SoundConfig.panel_click;
        }

        btn.node.on('click', () => {
            // 播放声音
           // SoundManager.getInstance().palyEffect(clickSoundName);
            // 
            callback.call(target);
        }, target);
    }

    // 设置obj的显示
    public static SetActive(obj: cc.Node, isaicive: boolean): void {
        if (obj == null)
            return;
        obj.active = isaicive;
    }

    //清空lab
    public static ClearLab(lable: cc.Label): void {
        if (lable == null)
            return;
        lable.string = "";
    }

    // 设置ProgressBar
    public static SetProgressBar(obj: cc.ProgressBar, num: number): void {
        if (obj == null) {
            console.log("ProgressBar 是null");
            return;
        }
        obj.progress = num;
    }

    // 设置lab
    public static SetLab(lable: cc.Label, text: any): void {
        if (lable == null) {
            console.log("lable 是null   " + text);
            return;
        }
        if (text == null)
            UITools.ClearLab(lable);
        else
            lable.string = text;
    }

    // 设置lab
    public static SetRichText(lable: cc.RichText, text: any): void {
        if (lable == null) {
            console.log("lable 是null   " + text);
            return;
        }

        lable.string = text;
    }

    // 设置位置
    public static SetTransfromPosition(obj: cc.Node, x: number, y: number): void {

        if (obj == null)
            return;
        obj.setPosition(x, y);
    }

    // 设置旋转
    public static SetTransfromRotation(obj: cc.Node, x: number, y: number, z: number): void {

        if (obj == null)
            return;
        obj.setRotation(x, y, z);
    }

    // 设置缩放
    public static SetTransfromScale(obj: cc.Node, x: number, y: number, z: number): void {

        if (obj == null)
            return;
        obj.setScale(x, y, z);
    }

    /**
     * 获得 宽
     */
    public static GetWdith(obj: cc.Node): number {

        if (obj == null)
            return;
        return obj.getContentSize().width;
    }
     /**
     * 获得 高
     */
    public static GetHeight(obj: cc.Node): number {

        if (obj == null)
            return;
        return obj.getContentSize().height;
    }
    /**
     * 设置 宽
     */
    public static SetWidth(obj: cc.Node, x: number): void {

        if (obj == null)
            return;
        obj.width = x;
    }
    /**
     * 设置 高
     */
    public static SeHeight(obj: cc.Node, height: number) {

        if (obj == null)
            return;
        obj.getContentSize().height = height;
    }

    /**
     * 设置 宽和高
     */
    public static SetSize(obj: cc.Node, width: number, height: number): void {

        if (obj == null)
            return;
        obj.width = width;
        obj.height = height;
    }

    /**
     * 设置node的颜色
     */
    public static SetColorByStr(node: cc.Node, str: string, char: string = ','): void {
        let color: cc.Color = getColor(str, char);
        node.color = color;
    }

    /**
     * 设置node的颜色
     */
    public static SetColorByColor(obj: cc.Node, color: cc.Color): void {

        if (obj == null)
            return;
        obj.color = color;
    }

   /**
     * 设置node的颜色
     */
    public static SetColorByNumber(obj: cc.Node, r: number, g: number, b: number, a: number = 255): void {
        let color: cc.Color = cc.color(r, g, b, a);
        obj.color = color;
    }

    /**
     * 局部查找 子节点
     */
    public static GetChild(obj: cc.Node, childPath: string): cc.Node {
        if (obj == null)
            return null;
        return obj.getChildByName(childPath);
    }
    /**
     * 全局查找
     */
    public static GetNode(childPath: string): cc.Node {
        return cc.find(childPath);
    }
    /** 
     * 销毁节点
     */
    public static DestoryNode(child: cc.Node): void {
        if (child = null)
            return;
        child.destroy();
    }
    /** 
     * 隐藏所有节点
     */
    public static SetCachedItemHide(items: Array<cc.Node>) {
        if (items == null) {
            return;
        }
        for (var i = 0; i < items.length; i++) {
            if (items[i] != null) {
                UITools.SetActive(items[i], false);
            }
        }
    }
    /** 
     * 尝试生成一个item
     */
    public static GetOneCached(cachedItems: Array<cc.Node>, parent: cc.Node, item: any): cc.Node {
        if (cachedItems == null) {
            cachedItems = new Array();
        }

        for (let i = 0; i < cachedItems.length; i++) {
            let obj: cc.Node = cachedItems[i];
            if (obj == null)
                continue;
            if (obj.active == false) {
                UITools.SetActive(obj, true);
                return obj;
            }
        }

        if (parent != null && item != null) {
            let go: cc.Node = this.AddChild(item, parent);
            UITools.SetActive(go, true);
            cachedItems.push(go);
            return go;
        }
        return null;
    }
     /** 
     * 生成item
     */
    public static AddChild(prefab: cc.Node, parent: cc.Node): cc.Node {
        if (prefab == null || parent == null)
            return null;
        let obj: cc.Node = cc.instantiate(prefab);
        parent.addChild(obj);

        UITools.SetTransfromPosition(obj, 0, 0);
        UITools.SetTransfromRotation(obj, 0, 0, 0);
        UITools.SetTransfromScale(obj, 1, 1, 1);

        return obj;

    }

    /** 
     * 复制文本到 手机的黏贴版。
     */
    public static copyText(text:string){
        let input = text;
        const el = document.createElement('textarea');
        el.value = input;
        // Prevent keyboard from showing on mobile
        el.setAttribute('readonly', '');
        el.style.contain = 'strict';
        el.style.position = 'absolute';
        el.style.left = '-9999px';
        el.style.fontSize = '12pt'; // Prevent zooming on iOS
        const selection = getSelection();
        let originalRange;
        if (selection.rangeCount > 0) {
            originalRange = selection.getRangeAt(0);
        }
        document.body.appendChild(el);
        el.select();
        // Explicit selection workaround for iOS
        el.selectionStart = 0;
        el.selectionEnd = input.length;
        let success = false;
        try {
            success = document.execCommand('copy');
        } catch (err) { }
        document.body.removeChild(el);
        if (originalRange) {
            selection.removeAllRanges();
            selection.addRange(originalRange);
        }
    }
    
}

使用方法:


 @property(cc.Button)
 btnClosed: cc.Button = null;
 @property(UITabEx)
 btn1: UITabEx = null;

    // Awake,添加兼听事件
    onLoad() {

        UITools.SetActive(this.objItem, false);
        this.btn1.addButtonEvent(this.onClickBtnEquip, this);

        UITools.AddButtonEvent(this.btnClosed, this.onBtnClosedClick, this);
    }

    // 打开界面。默认点击 tabEx1
    public onShow(game: GameApplication) {
       
            this.btn1.OnclickWithoutMusic();
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值