/****************************************************************** * Copyright(C) 2019 - 2020 Nx Studio * * 精灵强化 * * 2023.11.01 ******************************************************************/ cc.Class( { extends: cc.Component, // 编辑器特性 editor: { // 允许当前组件在编辑器模式下运行 executeInEditMode: false, // 当本组件添加到节点上后,禁止同类型(含子类)的组件再添加到同一个节点,防止逻辑发生冲突 disallowMultiple: true, // menu 用来将当前组件添加到组件菜单中,方便用户查找 menu: "Nx/组件|精灵", }, // 销毁 onDestroy: function() { this.cacheFrame(); }, // 设置精灵 setSpriteFrame: function( _sfkey, _cb ) { let sprite = this.getComponent( cc.Sprite ); if( !sprite ) { nx.error( `NxFxSprite:无效精灵组件!` ); return; } // 直接设置 if( _sfkey == null || _sfkey instanceof cc.SpriteFrame ) { sprite.spriteFrame = _sfkey; this.cacheFrame( _sfkey, "" ); nx.dt.fnInvoke( _cb, true ); return; } // 空字符串 if( typeof ( _sfkey ) == "string" && _sfkey == "" ) { sprite.spriteFrame = null; this.cacheFrame(); nx.dt.fnInvoke( _cb, true ); return; } // 重复设置 if( this.curKey == _sfkey ) { return; } // 重载载入 nx.res.loadSpriteFrame( _sfkey, ( _err, _sframe ) => { if( _err ) { sprite.spriteFrame = null; this.cacheFrame(); nx.dt.fnInvoke( _cb, false ); return; } sprite.spriteFrame = _sframe; this.cacheFrame( _sframe, _sfkey ); nx.dt.fnInvoke( _cb, true ); } ); }, // 资源缓存 cacheFrame: function( _sframe, _key ) { if( this.sframe ) { if( window.nx && nx && nx.assets ) { nx.assets.uncacheAsset( this.sframe ); } this.sframe = null; this.curKey = ""; } if( _sframe ) { if( window.nx && nx && nx.assets ) { nx.assets.cacheAsset( _sframe, _key ); } this.sframe = _sframe; this.curKey = _key || ""; } }, } );