view_ui_att_item.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { _decorator, assetManager, Component, ImageAsset, Node, Size, Sprite, SpriteFrame, Texture2D, UITransform, Vec3 } from 'cc';
  2. import { ui_att_item } from '../../../data/data';
  3. import { config } from '../../config';
  4. const { ccclass, property } = _decorator;
  5. @ccclass('view_ui_att_item')
  6. export class view_ui_att_item extends Component {
  7. private m_data:ui_att_item = null;
  8. getData(new_size:Size=null){
  9. if(this.m_data===null){
  10. this.m_data = new ui_att_item;
  11. let size = this.node.getComponent(UITransform).contentSize;
  12. let pos = this.node.position;
  13. this.m_data.width = new_size?new_size.width:size.width;
  14. this.m_data.height = new_size?new_size.height:size.height;
  15. this.m_data.x = pos.x;
  16. this.m_data.y = pos.y;
  17. }else{
  18. this.m_data.width = new_size?new_size.width:this.m_data.width;
  19. this.m_data.height = new_size?new_size.height:this.m_data.height;
  20. }
  21. return this.m_data
  22. }
  23. updateAtt(data:ui_att_item,check_type:number=config.update_type.update_all){
  24. if(data!=null){
  25. if(check_type===config.update_type.update_all||check_type===config.update_type.update_img){
  26. if(data.res!=""&&data.res.length>0){
  27. this.updateRes(data.res)
  28. }
  29. }
  30. this.m_data = data;
  31. if(check_type===config.update_type.update_all||check_type===config.update_type.update_info){
  32. let size = new Size(this.m_data.width,this.m_data.height)
  33. let pos = new Vec3(this.m_data.x,this.m_data.y)
  34. this.node.getComponent(UITransform).contentSize = size;
  35. this.node.position = pos;
  36. }
  37. }
  38. }
  39. updateRes(res_path){
  40. assetManager.loadRemote<ImageAsset>(res_path, (err, imageAsset2)=>{
  41. if (!err && imageAsset2) {
  42. const texture = new Texture2D();
  43. texture.image = imageAsset2;
  44. let spFrame2 = new SpriteFrame();
  45. spFrame2.texture = texture;
  46. this.getComponent(Sprite).spriteFrame = spFrame2;
  47. }
  48. });
  49. }
  50. }