cocoscreator实现spine用外部图片进行换皮

简介: cocoscreator实现spine用外部图片进行换皮

我希望可以做到外部图片替换spine一个部位上面的图片

网上有看到2dx的做法是新建sp.spine.RegionAttachment,这个时候问题就来了:

如何通过我们的Texture2D来设置sp.spine.TextureAtlasRegion和sp.spine.TextureAtlasPage


笔者研究一个礼拜,自己写了个实现函数,大家可以参考下

void SkeletonRenderer::createRegion(const std::string& slotName,cocos2d::middleware::Texture2D * texture){
    Slot *slot = _skeleton->findSlot(slotName.c_str());
    Texture* texture2D  = texture->getNativeTexture();
    RegionAttachment* attachment = (RegionAttachment*)slot->getAttachment();
    float width = texture2D->getWidth();
    float height = texture2D->getHeight();
    float wide = texture->getPixelsWide();
    float high = texture->getPixelsHigh();
    attachment->setUVs(0, 0, 1,1, false);
    attachment->setWidth(wide);
    attachment->setHeight(high);
    attachment->setRegionWidth(wide);
    attachment->setRegionHeight(high);
    attachment->setRegionOriginalWidth(wide);
    attachment->setRegionOriginalHeight(high);
    attachment->setRegionOffsetX(0);
    attachment->setRegionOffsetY(15);
    texture->setPixelsWide(width);
    texture->setPixelsHigh(height);
    texture->setRealTextureIndex(1);
    AttachmentVertices * attachV = (AttachmentVertices *)attachment->getRendererObject();
    if (attachV->_texture == texture) return;
    CC_SAFE_RELEASE(attachV->_texture);
    attachV->_texture = texture;
    CC_SAFE_RETAIN(texture);
    V2F_T2F_C4B*  vertices = attachV->_triangles->verts;
    for (int i = 0, ii = 0; i < 4; ++i, ii += 2) {
        vertices[i].texCoord.u = attachment->getUVs()[ii];
        vertices[i].texCoord.v = attachment->getUVs()[ii+1];
    }
    attachment->updateOffset();
}
public changeSpineTexture(slotname: string, texture2d: cc.Texture2D, size: cc.Size = null): void {
    if (this._spine == null || texture2d == null) {
      return;
    }
    let slot = this._spine.findSlot(slotname);
    if (slot) {
      let att = slot.getAttachment();
      if (cc.sys.isNative) {
        let jsbTexture = new middleware.Texture2D();
        let texture = texture2d.getImpl();
        if (size == null) {
          jsbTexture.setPixelsWide(texture2d.width);
          jsbTexture.setPixelsHigh(texture2d.height);
        } else {
          jsbTexture.setPixelsWide(size.width);
          jsbTexture.setPixelsHigh(size.height);
        }
        jsbTexture.setNativeTexture(texture);
        this._spine._nativeSkeleton.createRegion(slotname, jsbTexture);
      } else {
        texture2d.setPremultiplyAlpha(true);
        let skeletonTexture = new sp.SkeletonTexture();
        skeletonTexture.setRealTexture(texture2d);
        let page = new sp.spine.TextureAtlasPage();
        page.name = texture2d.name
        page.uWrap = sp.spine.TextureWrap.ClampToEdge;
        page.vWrap = sp.spine.TextureWrap.ClampToEdge;
        page.texture = skeletonTexture;
        page.texture.setWraps(page.uWrap, page.vWrap);
        page.width = texture2d.width;
        page.height = texture2d.height;
        let region = new sp.spine.TextureAtlasRegion();
        region.page = page;
        region.width = texture2d.width;
        region.height = texture2d.height;
        region.originalWidth = texture2d.width;
        region.originalHeight = texture2d.height;
        region.rotate = false;
        region.u = 0;
        region.v = 0;
        region.u2 = 1;
        region.v2 = 1;
        region.texture = skeletonTexture;
        att.region = region;
        if (size != null) {
          att.width = size.width;
          att.height = size.height;
        }
        att.setRegion(region);
        att.updateOffset();
      }
    } else {
      cc.error('!!!no slot name!!=', slotname);
    }
  }
相关文章
|
12月前
|
人工智能 Java API
Java 正式进入 Agentic AI 时代:Spring AI Alibaba 1.1 发布背后的技术演进
Spring AI Alibaba 1.1 正式发布,提供极简方式构建企业级AI智能体。基于ReactAgent核心,支持多智能体协作、上下文工程与生产级管控,助力开发者快速打造可靠、可扩展的智能应用。
5844 43
|
9月前
|
人工智能 监控 安全
OpenClaw(原Clawdbot/Moltbot)介绍及2026年一键安装教程、功能、应用场景说明
在AI自动化技术飞速普及的2026年,OpenClaw凭借“低门槛操作、全场景适配、高自由度定制”的核心优势,成为个人、新手开发者及轻量团队搭建专属AI助手的首选工具。其前身为Clawdbot、Moltbot,历经版本迭代与品牌整合后,于2026年正式统一命名为OpenClaw,核心功能一脉相承且全面升级,不仅解决了传统AI工具“只会说不会做”的痛点,更依托阿里云专属部署方案,实现了零基础用户15分钟快速上手,无需掌握编程技能,就能解锁自动化办公、跨工具协同等强大能力。
4170 1
|
数据采集
美团外卖商家数据采集软件,提取电话号手机号评分地址,销量评价内容【autojs版】
这是一款基于AutoJS开发的美团外卖商家数据采集工具,可自动抓取商家名称、电话、评分、地址、月销量及评价内容等信息。支持多页滚动加载与详细数据采集
|
人工智能 JavaScript 测试技术
构建智能 API 开发环境:在 Cursor 中连接 Apifox MCP Server
本文介绍了如何将Apifox MCP Server与Cursor结合,通过AI直接获取和理解API文档,大幅提升开发效率。首先需配置Apifox的Access Token和项目ID,并在Cursor中设置MCP连接。实际应用场景包括快速生成模型代码、同步更新接口文档与代码、生成CRUD操作、搜索API文档及自动生成测试用例。此外,还提供了管理多项目、安全性实践和优化AI响应质量的技巧。这种组合可显著减少从API规范到代码实现的时间,降低错误率并加速迭代过程,为开发者带来更高效的体验。
|
人工智能 自然语言处理 程序员
【重磅升级】通义灵码内置 DeepSeek-V3 和 R1 满血版,免费不限量,免部署!
近期通义灵码能力再升级全新上线模型选择功能,目前已经支持 Qwen2.5、DeepSeek-V3 和 R1系列模型,用户可以在 VSCode 和 JetBrains 里搜索并下载最新通义灵码插件,在输入框里选择模型,即可轻松切换模型。
|
编解码 前端开发 UED
CocosCreator 面试题(十一)Cocos Creator 屏幕适配
CocosCreator 面试题(十一)Cocos Creator 屏幕适配
1673 0
|
JavaScript 前端开发
js的math.max的用法
js的math.max的用法
1083 6
|
人工智能 API 语音技术
[AI MoneyPrinterTurbo] 一键成片,超级印钞机
探索MoneyPrinterTurbo的奇妙旅程,一个文生视频工具,让您只需一键,就能体验从安装到配置,再到创建高清短视频的全过程。
[AI MoneyPrinterTurbo] 一键成片,超级印钞机
|
前端开发 安全 开发者
前端开发中的跨域资源共享(CORS)问题及解决方案探讨
在前端开发中,跨域资源共享(CORS)是一个常见且重要的问题。本文将深入探讨CORS的原理、影响以及解决方案,帮助开发者更好地应对跨域请求问题。