A game about forced loneliness, made by TACStudios
at master 131 lines 4.2 kB view raw
1using System; 2using System.Collections.Generic; 3using UnityEngine; 4 5namespace UnityEditor.U2D.Animation 6{ 7 internal static class BoneCacheExtensions 8 { 9 public static BoneCache[] ToCharacterIfNeeded(this BoneCache[] bones) 10 { 11 return Array.ConvertAll(bones, ToCharacterIfNeeded); 12 } 13 14 public static BoneCache[] ToSpriteSheetIfNeeded(this BoneCache[] bones) 15 { 16 return Array.ConvertAll(bones, ToSpriteSheetIfNeeded); 17 } 18 19 public static BoneCache ToCharacterIfNeeded(this BoneCache bone) 20 { 21 if (bone == null) 22 return null; 23 24 var skinningCache = bone.skinningCache; 25 26 if (skinningCache.hasCharacter) 27 { 28 if (bone.skeleton != skinningCache.character.skeleton) 29 { 30 var selectedSprite = skinningCache.selectedSprite; 31 32 if (selectedSprite == null) 33 return null; 34 35 var skeleton = selectedSprite.GetSkeleton(); 36 var characterPart = selectedSprite.GetCharacterPart(); 37 38 Debug.Assert(skeleton != null); 39 Debug.Assert(characterPart != null); 40 Debug.Assert(bone.skeleton == skeleton); 41 Debug.Assert(skeleton.boneCount == characterPart.boneCount); 42 43 var index = skeleton.IndexOf(bone); 44 45 if (index == -1) 46 bone = null; 47 else 48 bone = characterPart.GetBone(index); 49 } 50 } 51 52 return bone; 53 } 54 55 public static BoneCache ToSpriteSheetIfNeeded(this BoneCache bone) 56 { 57 if (bone == null) 58 return null; 59 60 var skinningCache = bone.skinningCache; 61 62 if (skinningCache.hasCharacter && skinningCache.mode == SkinningMode.SpriteSheet) 63 { 64 var selectedSprite = skinningCache.selectedSprite; 65 66 if (selectedSprite == null) 67 return null; 68 69 var characterSkeleton = skinningCache.character.skeleton; 70 71 Debug.Assert(bone.skeleton == characterSkeleton); 72 73 var skeleton = selectedSprite.GetSkeleton(); 74 var characterPart = selectedSprite.GetCharacterPart(); 75 76 Debug.Assert(skeleton != null); 77 Debug.Assert(characterPart != null); 78 Debug.Assert(skeleton.boneCount == characterPart.boneCount); 79 80 var index = characterPart.IndexOf(bone); 81 82 if (index == -1) 83 bone = null; 84 else 85 bone = skeleton.GetBone(index); 86 } 87 88 return bone; 89 } 90 91 public static UnityEngine.U2D.SpriteBone ToSpriteBone(this BoneCache bone, Matrix4x4 rootTransform, int parentId) 92 { 93 var position = bone.localPosition; 94 var rotation = bone.localRotation; 95 96 if (parentId == -1) 97 { 98 rotation = bone.rotation; 99 position = rootTransform.inverse.MultiplyPoint3x4(bone.position); 100 } 101 102 return new UnityEngine.U2D.SpriteBone() 103 { 104 name = bone.name, 105 guid = bone.guid, 106 position = new Vector3(position.x, position.y, bone.depth), 107 rotation = rotation, 108 length = bone.localLength, 109 parentId = parentId, 110 color = bone.bindPoseColor 111 }; 112 } 113 114 public static UnityEngine.U2D.SpriteBone[] ToSpriteBone(this BoneCache[] bones, Matrix4x4 rootTransform) 115 { 116 var spriteBones = new List<UnityEngine.U2D.SpriteBone>(); 117 118 foreach (var bone in bones) 119 { 120 var parentId = -1; 121 122 if (ArrayUtility.Contains(bones, bone.parentBone)) 123 parentId = Array.IndexOf(bones, bone.parentBone); 124 125 spriteBones.Add(bone.ToSpriteBone(rootTransform, parentId)); 126 } 127 128 return spriteBones.ToArray(); 129 } 130 } 131}