| | |
| | | |
| | | using System; |
| | | using Cysharp.Threading.Tasks; |
| | | using Spine.Unity; |
| | | using UnityEngine; |
| | | using UnityEngine.UI; |
| | |
| | | spineAnimationState.Complete += OnAnimationComplete; |
| | | } |
| | | |
| | | public async UniTask CreateAsync(int _skinID, float scale = 0.8f, Action _onComplete = null, string motionName = "idle", bool isLh = false) |
| | | { |
| | | if (skinID == _skinID) |
| | | { |
| | | //避免重复创建 |
| | | |
| | | if (skeletonGraphic != null) |
| | | { |
| | | SetMaterialNone(); |
| | | if (isLh) |
| | | { |
| | | var skinConfigTmp = HeroSkinConfig.Get(skinID); |
| | | if (skinConfigTmp != null && skinConfigTmp.Tachie.Contains("SkeletonData")) |
| | | { |
| | | skeletonGraphic.enabled = true; |
| | | } |
| | | } |
| | | else |
| | | { |
| | | skeletonGraphic.enabled = true; |
| | | } |
| | | } |
| | | return; |
| | | } |
| | | |
| | | skinID = _skinID; |
| | | var skinConfig = HeroSkinConfig.Get(skinID); |
| | | if (isLh) |
| | | { |
| | | |
| | | //X轴偏移,Y轴偏移,缩放,是否水平翻转(0否1是) |
| | | if (skinConfig.TachieParam.Length == 4) |
| | | { |
| | | this.transform.localPosition = new Vector3(skinConfig.TachieParam[0], skinConfig.TachieParam[1], 0); |
| | | this.transform.localScale = Vector3.one * skinConfig.TachieParam[2]; |
| | | this.transform.localRotation = Quaternion.Euler(0, skinConfig.TachieParam[3] == 0 ? 0 : 180, 0); |
| | | } |
| | | else |
| | | { |
| | | this.transform.localPosition = Vector3.zero; |
| | | this.transform.localScale = Vector3.one; |
| | | this.transform.localRotation = Quaternion.identity; |
| | | } |
| | | |
| | | //立绘特殊处理,没有spine动画的改用图片 |
| | | var lhImg = this.AddMissingComponent<RawImage>(); |
| | | if (!skinConfig.Tachie.Contains("SkeletonData")) |
| | | { |
| | | //图片替换 |
| | | lhImg.SetTexture2DPNG(skinConfig.Tachie); |
| | | lhImg.SetNativeSize(); |
| | | if (skeletonGraphic != null) |
| | | { |
| | | skeletonGraphic.enabled = false; |
| | | } |
| | | lhImg.enabled = true; |
| | | lhImg.raycastTarget = false; |
| | | return; |
| | | } |
| | | else |
| | | { |
| | | if (skeletonGraphic != null) |
| | | { |
| | | skeletonGraphic.enabled = true; |
| | | } |
| | | lhImg.enabled = false; |
| | | } |
| | | } |
| | | else |
| | | { |
| | | this.transform.localScale = Vector3.one * scale; |
| | | } |
| | | |
| | | onComplete = _onComplete; |
| | | pool = GameObjectPoolManager.Instance.GetPool(await UILoader.LoadPrefabAsync("UIHero")); |
| | | if (this == null) return; |
| | | |
| | | if (!transform.gameObject.activeSelf) |
| | | { |
| | | transform.SetActive(true); |
| | | } |
| | | if (instanceGO == null) |
| | | { |
| | | instanceGO = pool.Request(); |
| | | instanceGO.transform.SetParent(transform); |
| | | //transform 的Pivot Y是0,让instanceGO 居中 |
| | | instanceGO.transform.localPosition = new Vector3(0, instanceGO.GetComponent<RectTransform>().sizeDelta.y * 0.5f); |
| | | |
| | | //instanceGO.transform.localPosition = Vector3.zero; |
| | | instanceGO.transform.localScale = Vector3.one; |
| | | instanceGO.transform.localRotation = Quaternion.identity; |
| | | } |
| | | |
| | | skeletonGraphic = instanceGO.GetComponentInChildren<SkeletonGraphic>(true); |
| | | if (isLh) |
| | | { |
| | | skeletonGraphic.skeletonDataAsset = ResManager.Instance.LoadAsset<SkeletonDataAsset>("Hero/SpineRes/", skinConfig.Tachie); |
| | | } |
| | | else |
| | | { |
| | | skeletonGraphic.skeletonDataAsset = ResManager.Instance.LoadAsset<SkeletonDataAsset>("Hero/SpineRes/", skinConfig.SpineRes); |
| | | } |
| | | if (skeletonGraphic.skeletonDataAsset == null) |
| | | { |
| | | |
| | | transform.SetActive(false); |
| | | if (pool != null) |
| | | pool.Release(instanceGO); |
| | | skeletonGraphic = null; |
| | | Destroy(instanceGO); |
| | | Debug.LogError("未配置spine"); |
| | | return; |
| | | } |
| | | skeletonGraphic.initialSkinName = skinConfig.InitialSkinName; |
| | | skeletonGraphic.Initialize(true); |
| | | // 初始化完成后设置皮肤 |
| | | if (!string.IsNullOrEmpty(skinConfig.InitialSkinName)) |
| | | { |
| | | var skeleton = skeletonGraphic.Skeleton; |
| | | skeleton.SetSkin(skinConfig.InitialSkinName); |
| | | skeleton.SetSlotsToSetupPose(); |
| | | skeletonGraphic.Update(0); |
| | | } |
| | | |
| | | skeletonGraphic.enabled = true; |
| | | SetMaterialNone(); |
| | | |
| | | spineAnimationState = skeletonGraphic.AnimationState; |
| | | spineAnimationState.Data.DefaultMix = 0f; |
| | | if (motionName == "") |
| | | motionName = GetFistSpineAnim(); |
| | | |
| | | PlayAnimation(motionName, true); |
| | | spineAnimationState.Complete -= OnAnimationComplete; |
| | | spineAnimationState.Complete += OnAnimationComplete; |
| | | } |
| | | |
| | | |
| | | |
| | | |