123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- import { Color, Component, sp, sys, _decorator } from 'cc';
- const { ccclass, property } = _decorator;
- /**
- * spine动画表现管理
- * @author
- *
- */
- @ccclass('SpineView')
- export default class SpineView extends Component {
- skin:string="default";
-
- @property({ displayName: '待机' })
- idle:string = "";
- @property({type: sp.Skeleton, tooltip: '角色动画' })
- spine:sp.Skeleton = null!;
- cur:string =null!;
-
- public OnComp:Function;
- private resetColor:Color=Color.WHITE;
- onLoad() {
- //console.log("111111111111");
- //this.spine.enableBatch=false;
- if(sys.isNative){//原生共享模式有bug
- this.spine.setAnimationCacheMode(sp.AnimationCacheMode.REALTIME);
- this.spine.clearTracks();//清理所有播放队列的动画
- }
- //this.SetSkin(this.skin);
- }
- start(){
- this.DoAnimation(this.idle,true);
- this.spine.setCompleteListener(function(){
- if(this.OnComp)this.OnComp();
- }.bind(this));
- }
- onBeforeRemove(){
- this.spine.setCompleteListener(null);
- }
- public Dispos():void{
- this.ResetColor();
- this.OnComp=null;
- //this.spine.setCompleteListener(null);
- }
- public SetSkin(skin:string):void{
- this.spine.setSkin(skin);
- }
- public DoAnimation(animation:string,loop:boolean=true,track:number=0,force:boolean=false){
- if(force){
- this.spine.setAnimation(track,animation,loop);
- return;
- }
- if(this.cur==animation)return;
- this.spine.setAnimation(track,animation,loop);
- }
- /**设置是否暂停 */
- public SetPaused(paused:boolean):void{
- this.spine.paused=paused;
- }
- /**获取是否暂停 */
- public GetPaused():boolean{
- return this.spine.paused;
- }
- public CheckPaused(paused:boolean):void{
- if(this.spine.paused!=paused)this.spine.paused=paused;
- }
- /** 闪色表现 Color.RED 0.1*/
- public Flash(color:Color,time:number):void{
- if(this.spine.color!=color){
- this.spine.color=color;
- this.unscheduleAllCallbacks();
- this.scheduleOnce(()=>{
- this.spine.color = this.resetColor;
- },time);
- }
- }
- public ChangeMagenta(){
- this.ChangeColor(Color.MAGENTA);
- }
- public ChangeGreen(){
- this.ChangeColor(Color.GREEN);
- }
- public ChangeGray(){
- this.ChangeColor(Color.GRAY);
- }
- public ChangeRed(){
- this.ChangeColor(Color.RED);
- }
- public ChangeCyan(){
- this.ChangeColor(Color.CYAN);
- }
- public ChangeBlack(){
- this.ChangeColor(Color.BLACK);
- }
- public ChangeBule(){
- this.ChangeColor(Color.BLUE);
- }
- public ChangeYellow(){
- this.ChangeColor(Color.YELLOW);
- }
- //
- /** 变色表现 Color.RED 0.1*/
- public ChangeColor(color:Color):void{
- this.resetColor=color;
- if(this.spine.color != this.resetColor){
- this.spine.color = this.resetColor;
- }
- }
- public ResetColor():void{
- this.resetColor = Color.WHITE;
- if(this.spine.color != this.resetColor){
- this.spine.color = this.resetColor;
- }
- }
- /**
- * 缩放动画播放速率
- * @override
- * @param scale 缩放倍率
- */
- public scaleTime(scale: number) {
- if (scale > 0)
- this.spine.timeScale = scale;
- }
- public getBone(name: string): any {
- var bone = this.spine.findBone(name);
- return bone
- }
- /**
- * 换装
- * @param slotName 插槽名字
- * @param attachmentName 元素名字
- */
- public changeEquipment(slotName:string, attachmentName:string|null=null) {
- const slot = this.spine.findSlot(slotName);
- if (slot) {
- if(attachmentName){
- const attachment = this.spine.getAttachment(slotName, attachmentName);
- slot.setAttachment(attachment);
- }else{
- slot.setAttachment(null);
- }
- }
- }
- /**换装
- * @param skinName 要替换的部件皮肤名称
- * @param slotName 要替换的部件的插槽名称
- * @param targetAttaName Spine中皮肤占位符的名字
- */
- public changeSlot(skinName: string, slotName: string, targetAttaName: string):void{
- //查找局部皮肤
- let skeletonData = this.spine.skeletonData.getRuntimeData();
- let targetSkin: sp.spine.Skin = skeletonData.findSkin(skinName);
- //查找局部皮肤下的插槽与附件
- let targetSkinSlotIndex = skeletonData.findSlotIndex(slotName);
- let atta = targetSkin.getAttachment(targetSkinSlotIndex, targetAttaName);
- //查找全身皮肤下的插槽
- let curSlot = this.spine.findSlot(slotName);
- //替换全身皮肤插槽的附件
- curSlot && curSlot.setAttachment(atta);
- }
- }
|