public static void AddSlot(ArmatureEditor armatureEditor) { if (armatureEditor.armatureData.slotDatas != null) { armatureEditor.slotsKV.Clear(); DragonBoneArmature armature = armatureEditor.armature.GetComponent <DragonBoneArmature>(); int len = armatureEditor.armatureData.slotDatas.Length; for (int i = 0; i < len; ++i) { DragonBoneData.SlotData slotData = armatureEditor.armatureData.slotDatas[i]; GameObject go = new GameObject(slotData.name); armatureEditor.slotsKV[slotData.name] = go.transform; Slot slot = go.AddComponent <Slot>(); slot.zOrder = i; slot.armature = armature; slot.blendMode = slot.ConvertBlendMode(slotData.blendMode.ToLower()); if (slotData.color != null) { slot.color = slotData.color.ToColor(); } armatureEditor.slots.Add(slot); } } }
public static void SetIKs(ArmatureEditor armatureEditor) { if (armatureEditor.armatureData.ikDatas != null) { int len = armatureEditor.armatureData.ikDatas.Length; for (int i = 0; i < len; ++i) { DragonBoneData.IKData ikData = armatureEditor.armatureData.ikDatas[i]; Transform ikTrans = armatureEditor.bonesKV[ikData.target]; Transform targetBone = armatureEditor.bonesKV[ikData.bone]; DragonBoneData.BoneData targetBoneData = armatureEditor.bonesDataKV[ikData.bone]; Transform parentBone = targetBone; int y = ikData.chain; while (--y > -1) { parentBone = parentBone.parent; } BoneIK bi = parentBone.gameObject.AddComponent <BoneIK>(); Vector3 v = Vector3.right * targetBoneData.length * 0.01f; v = targetBone.TransformPoint(v); GameObject go = new GameObject(ikData.name); go.transform.parent = targetBone; go.transform.position = v; bi.damping = ikData.weight; bi.endTransform = go.transform; bi.targetIK = ikTrans; bi.iterations = 20; bi.bendPositive = ikData.bendPositive; } } }
static string[] GetTransformPaths(ArmatureEditor armatureEditor) { List <string> result = new List <string>(); result.Add(""); foreach (Transform t in armatureEditor.bones) { string path = AnimationUtility.CalculateTransformPath(t, armatureEditor.armature); result.Add(path); } return(result.ToArray()); }
/// <summary> /// set events /// </summary> static void SetEvent(ArmatureEditor armatureEditor, AnimationClip clip, DragonBoneData.AnimKeyData[] frameDatas) { if (frameDatas == null || frameDatas.Length == 0) { return; } if (armatureEditor.armature.gameObject.GetComponent <DragonBoneEvent>() == null) { armatureEditor.armature.gameObject.AddComponent <DragonBoneEvent>(); } float during = 0; float perKeyTime = 1f / armatureEditor.armatureData.frameRate; List <AnimationEvent> evts = new List <AnimationEvent>(); foreach (DragonBoneData.AnimKeyData keyData in frameDatas) { if (!string.IsNullOrEmpty(keyData.eventName)) { AnimationEvent ae = new AnimationEvent(); ae.messageOptions = SendMessageOptions.DontRequireReceiver; string param = keyData.eventName + "$"; if (!string.IsNullOrEmpty(keyData.actionName)) { param += keyData.actionName + "$"; } else { param += "$"; } if (!string.IsNullOrEmpty(keyData.soundName)) { param += keyData.soundName; } ae.functionName = "OnAnimEvent"; ae.time = during; ae.stringParameter = param; evts.Add(ae); } during += keyData.duration * perKeyTime; } if (evts.Count > 0) { AnimationUtility.SetAnimationEvents(clip, evts.ToArray()); } }
public static void AddSlot(ArmatureEditor armatureEditor) { if (armatureEditor.armatureData.slotDatas != null) { armatureEditor.slotsKV.Clear(); int len = armatureEditor.armatureData.slotDatas.Length; for (int i = 0; i < len; ++i) { DragonBoneData.SlotData slotData = armatureEditor.armatureData.slotDatas[i]; GameObject go = new GameObject(slotData.name); armatureEditor.slotsKV[slotData.name] = go.transform; } } }
//全局BoneIndex转 特定数组中的BoneIndex static int GlobalBoneIndexToLocalBoneIndex(ArmatureEditor armatureEditor, int globalBoneIndex, Transform[] localBones) { Transform globalBone = armatureEditor.bones[globalBoneIndex]; int len = localBones.Length; for (int i = 0; i < len; ++i) { if (localBones[i] == globalBone) { return(i); } } return(globalBoneIndex); }
public static void ParseTextureAtlas(ArmatureEditor armatureEditor, Texture2D texture, TextAsset atlasText) { SimpleJSON.JSONClass obj = SimpleJSON.JSON.Parse(atlasText.text).AsObject; SimpleJSON.JSONArray arr = obj["SubTexture"].AsArray; for (int i = 0; i < arr.Count; ++i) { SimpleJSON.JSONClass frameObj = arr[i].AsObject; string textureName = frameObj["name"].ToString(); textureName = textureName.Replace('/', '_'); ArmatureEditor.Atlas atlas = new ArmatureEditor.Atlas(); atlas.texture = texture; atlas.atlasText = atlasText; armatureEditor.atlasKV[textureName] = atlas; } }
static string GetNodeRelativePath(ArmatureEditor armatureEditor, Transform node) { List <string> path = new List <string>(); while (node != armatureEditor.armature) { path.Add(node.name); node = node.parent; } string result = ""; for (int i = path.Count - 1; i >= 0; i--) { result += path[i] + "/"; } return(result.Substring(0, result.Length - 1)); }
static void CreateAvatar(ArmatureEditor armatureEditor, Animator animator, string path) { Avatar avatar = AvatarBuilder.BuildGenericAvatar(armatureEditor.armature.gameObject, ""); animator.avatar = avatar; AvatarMask avatarMask = new AvatarMask(); string[] transofrmPaths = GetTransformPaths(armatureEditor); avatarMask.transformCount = transofrmPaths.Length; for (int i = 0; i < transofrmPaths.Length; i++) { avatarMask.SetTransformPath(i, transofrmPaths[i]); avatarMask.SetTransformActive(i, true); } AssetDatabase.CreateAsset(avatar, path + "/" + armatureEditor.armature.name + "Avatar.asset"); AssetDatabase.CreateAsset(avatarMask, path + "/" + armatureEditor.armature.name + "Mask.asset"); AssetDatabase.SaveAssets(); }
public static void AddBones(ArmatureEditor armatureEditor) { if (armatureEditor.armatureData.boneDatas != null) { armatureEditor.bonesKV.Clear(); armatureEditor.bones.Clear(); int len = armatureEditor.armatureData.boneDatas.Length; for (int i = 0; i < len; ++i) { DragonBoneData.BoneData boneData = armatureEditor.armatureData.boneDatas[i]; GameObject go = new GameObject(boneData.name); armatureEditor.bonesKV[boneData.name] = go.transform; if (m_rootBone == null) { m_rootBone = go.transform; } armatureEditor.bones.Add(go.transform); } } }
private static void ParseAnimSortOrder(ArmatureEditor armatureEditor, SimpleJSON.JSONClass zOrders, DragonBoneData.AnimationData animData) { if (zOrders.ContainKey("frame")) { //just only one DragonBoneData.AnimSubData subData = new DragonBoneData.AnimSubData(); animData.zOrderDatas = new DragonBoneData.AnimSubData[1] { subData }; if (zOrders.ContainKey("offset")) { subData.offset = zOrders["offset"].AsFloat; } SimpleJSON.JSONArray frames = zOrders["frame"].AsArray; subData.frameDatas = new DragonBoneData.AnimFrameData[frames.Count]; for (int i = 0; i < frames.Count; ++i) { SimpleJSON.JSONClass frameObj = frames[i].AsObject; DragonBoneData.AnimFrameData frameData = new DragonBoneData.AnimFrameData(); subData.frameDatas[i] = frameData; if (frameObj.ContainKey("duration")) { frameData.duration = frameObj["duration"].AsInt; } if (frameObj.ContainKey("zOrder")) { SimpleJSON.JSONArray zs = frameObj["zOrder"].AsArray; if (zs != null) { frameData.zOrder = new int[zs.Count]; for (int z = 0; z < zs.Count; ++z) { frameData.zOrder[z] = zs[z].AsInt; } } } } } }
public static void ParseAnimJsonData(ArmatureEditor armatureEditor) { SimpleJSON.JSONClass json = SimpleJSON.JSON.Parse(armatureEditor.animTextAsset.text.Replace("null", "\"null\"")).AsObject; SimpleJSON.JSONArray armtureArr = json["armature"].AsArray; for (int i = 0; i < armtureArr.Count; ++i) { armatureEditor.armatureData = new DragonBoneData.ArmatureData(); GameObject go = new GameObject("DragonBone"); go.AddComponent <DragonBoneArmature>(); armatureEditor.armature = go.transform; armatureEditor.bonesKV.Clear(); armatureEditor.slotsKV.Clear(); armatureEditor.slots.Clear(); armatureEditor.bonesDataKV.Clear(); armatureEditor.slotsDataKV.Clear(); armatureEditor.bones.Clear(); armatureEditor.ffdKV.Clear(); armatureEditor.bonePoseKV.Clear(); SimpleJSON.JSONClass armtureObj = armtureArr[i].AsObject; if (armtureObj.ContainKey("name")) { string armatureName = armtureObj["name"].ToString().Trim(); armatureEditor.armature.name = armatureName; } if (armtureObj.ContainKey("type")) { armatureEditor.armatureData.type = armtureObj["type"].ToString(); } if (armtureObj.ContainKey("frameRate")) { armatureEditor.armatureData.frameRate = armtureObj["frameRate"].AsFloat; if (armatureEditor.armatureData.frameRate == 0) { armatureEditor.armatureData.frameRate = 24; //db默认为24 } } ParseArmtureData(armatureEditor, armtureObj); armatureEditor.InitShow(); } }
public static void ShowSlots(ArmatureEditor armatureEditor) { foreach (Transform s in armatureEditor.slotsKV.Values) { DragonBoneData.SlotData slotData = armatureEditor.slotsDataKV[s.name]; if (!string.IsNullOrEmpty(slotData.parent)) { if (armatureEditor.bonesKV.ContainsKey(slotData.parent)) { Transform parent = armatureEditor.bonesKV[slotData.parent]; s.transform.parent = parent.transform; } } else { s.transform.parent = armatureEditor.armature; } s.transform.localScale = new Vector3(slotData.scale, slotData.scale, 1f); s.transform.localPosition = new Vector3(0, 0, slotData.z); s.transform.localEulerAngles = Vector3.zero; } }
static void SetDragonBoneArmature(ArmatureEditor armature) { DragonBoneArmature dbArmature = armature.armature.GetComponent <DragonBoneArmature>(); if (dbArmature) { dbArmature.updateFrames = new SpriteFrame[changedSpriteFramesKV.Count]; int i = 0; foreach (SpriteFrame frame in changedSpriteFramesKV.Values) { dbArmature.updateFrames[i] = frame; ++i; } dbArmature.updateMeshs = new SpriteMesh[changedSpriteMeshsKV.Count]; i = 0; foreach (SpriteMesh mesh in changedSpriteMeshsKV.Values) { dbArmature.updateMeshs[i] = mesh; ++i; } } }
static void ShowCustomCollider(DragonBoneData.SkinSlotDisplayData displayData, Transform slot, ArmatureEditor armatureEditor, DragonBoneData.SlotData slotData) { GameObject go = new GameObject(displayData.textureName); go.transform.parent = slot; Vector3 localPos = Vector3.zero; if (!float.IsNaN(displayData.transform.x)) { localPos.x = displayData.transform.x; } if (!float.IsNaN(displayData.transform.y)) { localPos.y = displayData.transform.y; } go.transform.localPosition = localPos; Vector3 localSc = Vector3.one; if (!float.IsNaN(displayData.transform.scx)) { localSc.x = displayData.transform.scx; } if (!float.IsNaN(displayData.transform.scy)) { localSc.y = displayData.transform.scy; } go.transform.localScale = localSc; if (!float.IsNaN(displayData.transform.rotate)) { go.transform.localRotation = Quaternion.Euler(0, 0, displayData.transform.rotate); } if (armatureEditor.genCustomCollider) { PolygonCollider2D collider = go.AddComponent <PolygonCollider2D>(); Vector2[] points = new Vector2[displayData.vertices.Length]; int len = points.Length; for (int i = 0; i < len; ++i) { points[i] = (Vector2)displayData.vertices[i]; } collider.points = points; } }
public static void CreateAnimFile(ArmatureEditor armatureEditor) { changedSpriteFramesKV = new Dictionary <string, SpriteFrame>(); changedSpriteMeshsKV = new Dictionary <string, SpriteMesh>(); slotPathKV = new Dictionary <string, string>(); string path = AssetDatabase.GetAssetPath(armatureEditor.animTextAsset); path = path.Substring(0, path.LastIndexOf('/')) + "/" + armatureEditor.armature.name + "_Anims"; if (!AssetDatabase.IsValidFolder(path)) { Directory.CreateDirectory(path); } path += "/"; Animator animator = armatureEditor.armature.gameObject.AddComponent <Animator>(); AnimatorController controller = AssetDatabase.LoadAssetAtPath <AnimatorController>(path + armatureEditor.armature.name + ".controller"); AnimatorStateMachine rootStateMachine = null; if (controller == null) { controller = AnimatorController.CreateAnimatorControllerAtPath(path + armatureEditor.armature.name + ".controller"); rootStateMachine = controller.layers[0].stateMachine; } animator.runtimeAnimatorController = controller; if (armatureEditor.armatureData.animDatas != null) { int len = armatureEditor.armatureData.animDatas.Length; for (int i = 0; i < len; ++i) { DragonBoneData.AnimationData animationData = armatureEditor.armatureData.animDatas[i]; string clipPath = path + animationData.name + ".anim"; AnimationClip clip = AssetDatabase.LoadAssetAtPath <AnimationClip>(clipPath); if (clip == null) { clip = new AnimationClip(); AssetDatabase.CreateAsset(clip, clipPath); } else { clip.ClearCurves(); } clip.name = animationData.name; clip.frameRate = armatureEditor.armatureData.frameRate; CreateBoneAnim(armatureEditor, clip, animationData.boneDatas, armatureEditor.bonesKV); CreateSlotAnim(armatureEditor, clip, animationData.slotDatas, armatureEditor.slotsKV); CreateFFDAnim(armatureEditor, clip, animationData.ffdDatas, armatureEditor.slotsKV); CreateAnimZOrder(armatureEditor, clip, animationData.zOrderDatas); SetDragonBoneArmature(armatureEditor); SetEvent(armatureEditor, clip, animationData.keyDatas); SerializedObject serializedClip = new SerializedObject(clip); AnimationClipSettings clipSettings = new AnimationClipSettings(serializedClip.FindProperty("m_AnimationClipSettings")); clipSettings.loopTime = animationData.playTimes == 0; serializedClip.ApplyModifiedProperties(); if (rootStateMachine != null) { AnimatorState state = rootStateMachine.AddState(clip.name); state.motion = clip; } } AssetDatabase.SaveAssets(); } if (rootStateMachine != null && rootStateMachine.states != null && rootStateMachine.states.Length > 0) { rootStateMachine.defaultState = rootStateMachine.states[0].state; } //createAvatar if (armatureEditor.createAvatar) { CreateAvatar(armatureEditor, animator, path); } }
public static void ShowSkins(ArmatureEditor armatureEditor) { if (armatureEditor.armatureData.skinDatas != null && armatureEditor.armatureData.skinDatas.Length > 0) { DragonBoneArmature armature = armatureEditor.armature.GetComponent <DragonBoneArmature>(); Dictionary <Texture2D, Material> matKV = new Dictionary <Texture2D, Material>(); List <Material> mats = new List <Material>(); //创建贴图集的材质 Material atlasMat = null; if (armatureEditor.altasTexture) { Material mat = null; string path = AssetDatabase.GetAssetPath(armatureEditor.altasTexture); path = path.Substring(0, path.LastIndexOf('.')) + "_Mat.mat"; mat = AssetDatabase.LoadAssetAtPath <Material>(path); if (!mat) { mat = new Material(Shader.Find("DragonBone/DragonBone Simple")); AssetDatabase.CreateAsset(mat, path); } mat.mainTexture = armatureEditor.altasTexture; matKV[armatureEditor.altasTexture] = mat; atlasMat = mat; mats.Add(mat); } if (armatureEditor.otherTextures != null && armatureEditor.otherTextures.Length > 0) { for (int r = 0; r < armatureEditor.otherTextures.Length; ++r) { ArmatureEditor.Atlas atlas = armatureEditor.otherTextures[r]; Material mat = null; string path = AssetDatabase.GetAssetPath(atlas.texture); path = path.Substring(0, path.LastIndexOf('.')) + "_Mat.mat"; mat = AssetDatabase.LoadAssetAtPath <Material>(path); if (!mat) { mat = new Material(Shader.Find("DragonBone/DragonBone Simple")); AssetDatabase.CreateAsset(mat, path); } mat.mainTexture = atlas.texture; matKV[atlas.texture] = mat; mats.Add(mat); } } //create Frames Dictionary <Texture2D, SpriteFrame> frameKV = new Dictionary <Texture2D, SpriteFrame>(); List <TextureFrame> tfs = new List <TextureFrame>(); SpriteFrame frame = null; if (armatureEditor.altasTextAsset != null) { GameObject go = new GameObject(); SpriteFrame sf = go.AddComponent <SpriteFrame>(); sf.atlasMat = atlasMat; sf.atlasText = armatureEditor.altasTextAsset; sf.ParseAtlasText(); sf.CreateQuad(); frameKV[armatureEditor.altasTexture] = sf; frame = sf; tfs.AddRange(frame.frames); } if (armatureEditor.otherTextures != null && armatureEditor.otherTextures.Length > 0) { for (int r = 0; r < armatureEditor.otherTextures.Length; ++r) { ArmatureEditor.Atlas atlas = armatureEditor.otherTextures[r]; GameObject go = new GameObject(); SpriteFrame sf = go.AddComponent <SpriteFrame>(); sf.atlasMat = matKV[atlas.texture]; sf.atlasText = atlas.atlasText; sf.ParseAtlasText(); sf.CreateQuad(); frameKV[atlas.texture] = sf; tfs.AddRange(sf.frames); } } List <SpriteMetaData> metaDatas = new List <SpriteMetaData>(); List <SpriteRenderer> sprites = new List <SpriteRenderer>(); int meshSpriteCount = 0; int len = armatureEditor.armatureData.skinDatas.Length; for (int i = 0; i < len; ++i) { DragonBoneData.SkinData skinData = armatureEditor.armatureData.skinDatas[i]; for (int j = 0; j < skinData.slots.Length; ++j) { DragonBoneData.SkinSlotData skinSlotData = skinData.slots[j]; Transform slot = armatureEditor.slotsKV[skinSlotData.slotName]; DragonBoneData.SlotData slotData = armatureEditor.slotsDataKV[skinSlotData.slotName]; if (slot && skinSlotData.displays != null && skinSlotData.displays.Length > 0) { for (int k = 0; k < skinSlotData.displays.Length; ++k) { DragonBoneData.SkinSlotDisplayData displayData = skinSlotData.displays[k]; if (displayData.type != "image" && displayData.type != "mesh" && displayData.type != "boundingBox") { continue; } if (displayData.type.Equals("boundingBox")) { if (displayData.subType == "polygon") { ShowCustomCollider(displayData, slot, armatureEditor, slotData); } continue; } ArmatureEditor.Atlas atlas = armatureEditor.GetAtlasByTextureName(displayData.texturePath); if (!armatureEditor.isSingleSprite) { atlasMat = matKV[atlas.texture]; frame = frameKV[atlas.texture]; } if (displayData.type == "image") { if (armatureEditor.useUnitySprite) { if (armatureEditor.isSingleSprite) { Sprite sprite = armatureEditor.spriteKV[displayData.textureName]; string spritePath = AssetDatabase.GetAssetPath(sprite); if (displayData.pivot.x != 0 || displayData.pivot.y != 0) { TextureImporter textureImporter = AssetImporter.GetAtPath(spritePath) as TextureImporter; textureImporter.textureType = TextureImporterType.Sprite; textureImporter.spriteImportMode = SpriteImportMode.Single; textureImporter.spritePixelsPerUnit = 100; textureImporter.spritePivot = new Vector2((displayData.pivot.x + sprite.rect.width / 2) / sprite.rect.width, (displayData.pivot.y + sprite.rect.height / 2) / sprite.rect.height); AssetDatabase.ImportAsset(spritePath, ImportAssetOptions.ForceUpdate); } sprites.Add(ShowUnitySpriteSingle(sprite, displayData, slot, slotData)); } else { SpriteMetaData metaData = new SpriteMetaData(); metaData.name = displayData.textureName; metaData.rect = frame.GetFrameByName(displayData.texturePath).rect; metaData.rect.y = armatureEditor.altasTexture.height - metaData.rect.y - metaData.rect.height; if (displayData.pivot.x != 0 || displayData.pivot.y != 0) { metaData.alignment = (int)SpriteAlignment.Custom; metaData.pivot = new Vector2((displayData.pivot.x + metaData.rect.width / 2) / metaData.rect.width, (displayData.pivot.y + metaData.rect.height / 2) / metaData.rect.height); } metaDatas.Add(metaData); sprites.Add(ShowUnitySprite(atlasMat, displayData, slot, metaData, slotData)); } } else { ShowSpriteFrame(frame, atlasMat, displayData, slot, slotData); } } else if (displayData.type == "mesh") { TextureFrame textureFrame = new TextureFrame(); if (armatureEditor.isSingleSprite) { Sprite sprite = armatureEditor.spriteKV[displayData.textureName]; textureFrame.name = displayData.textureName; textureFrame.frameSize = sprite.rect; textureFrame.rect = sprite.rect; textureFrame.atlasTextureSize = new Vector2(sprite.rect.width, sprite.rect.height); string path = AssetDatabase.GetAssetPath(sprite); string materialFolder = path.Substring(0, path.LastIndexOf("/")); materialFolder = materialFolder.Substring(0, materialFolder.LastIndexOf("/")) + "/Materials/"; if (!Directory.Exists(materialFolder)) { Directory.CreateDirectory(materialFolder); } string matPath = materialFolder + sprite.name + "_Mat.mat"; atlasMat = AssetDatabase.LoadAssetAtPath <Material>(matPath); if (!atlasMat) { atlasMat = new Material(Shader.Find("DragonBone/DragonBone Simple")); AssetDatabase.CreateAsset(atlasMat, matPath); } atlasMat.mainTexture = AssetDatabase.LoadAssetAtPath <Texture>(path); mats.Add(atlasMat); textureFrame.material = atlasMat; textureFrame.texture = atlasMat.mainTexture; tfs.Add(textureFrame); } else { foreach (TextureFrame st in frame.frames) { if (st.name.Equals(displayData.textureName)) { textureFrame = st; break; } } } if (textureFrame.rect.width > 0 && textureFrame.rect.height > 0 && atlasMat && atlasMat.mainTexture) { ShowSpriteMesh(textureFrame, atlasMat, displayData, slot, armatureEditor, slotData); ++meshSpriteCount; } } } slot.GetComponent <Slot>().displayIndex = slotData.displayIndex; } } } armature.materials = mats.ToArray(); armature.textureFrames = tfs.ToArray(); foreach (SpriteFrame sf in frameKV.Values) { GameObject.DestroyImmediate(sf.gameObject); } if (armatureEditor.useUnitySprite) { if (!armatureEditor.isSingleSprite) { if (metaDatas.Count > 0) { string textureAtlasPath = AssetDatabase.GetAssetPath(armatureEditor.altasTexture); TextureImporter textureImporter = AssetImporter.GetAtPath(textureAtlasPath) as TextureImporter; textureImporter.maxTextureSize = 2048; textureImporter.spritesheet = metaDatas.ToArray(); textureImporter.textureType = TextureImporterType.Sprite; textureImporter.spriteImportMode = SpriteImportMode.Multiple; textureImporter.spritePixelsPerUnit = 100; AssetDatabase.ImportAsset(textureAtlasPath, ImportAssetOptions.ForceUpdate); Object[] savedSprites = AssetDatabase.LoadAllAssetsAtPath(textureAtlasPath); foreach (Object obj in savedSprites) { Sprite objSprite = obj as Sprite; if (objSprite) { len = sprites.Count; for (int i = 0; i < len; ++i) { if (sprites[i].name.Equals(objSprite.name)) { sprites[i].sprite = objSprite; } } } } } } if (atlasMat != null) { if (meshSpriteCount == 0) { //can delete safely foreach (Material mat in matKV.Values) { if (!armatureEditor.spriteMeshUsedMatKV.ContainsKey(mat)) { AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(mat)); } } } else { foreach (SpriteRenderer sprite in sprites) { sprite.material = atlasMat; } } } } } }
static void CreateFFDAnim(ArmatureEditor armatureEditor, AnimationClip clip, DragonBoneData.AnimSubData[] subDatas, Dictionary <string, Transform> transformKV) { if (subDatas == null) { return; } for (int i = 0; i < subDatas.Length; ++i) { DragonBoneData.AnimSubData animSubData = subDatas[i]; string slotName = string.IsNullOrEmpty(animSubData.slot) ? animSubData.name : animSubData.slot; Transform slotNode = transformKV[slotName]; List <AnimationCurve[]> vertexcurvexArray = null; List <AnimationCurve[]> vertexcurveyArray = null; if (slotNode.childCount > 0) { vertexcurvexArray = new List <AnimationCurve[]>(); vertexcurveyArray = new List <AnimationCurve[]>(); for (int j = 0; j < slotNode.childCount; ++j) { Transform ffdNode = slotNode.GetChild(j); if (ffdNode.name == animSubData.name) { AnimationCurve[] vertex_xcurves = new AnimationCurve[ffdNode.childCount]; AnimationCurve[] vertex_ycurves = new AnimationCurve[ffdNode.childCount]; for (int r = 0; r < vertex_xcurves.Length; ++r) { vertex_xcurves[r] = new AnimationCurve(); vertex_ycurves[r] = new AnimationCurve(); } vertexcurvexArray.Add(vertex_xcurves); vertexcurveyArray.Add(vertex_ycurves); } } } float during = animSubData.offset; float perKeyTime = 1f / armatureEditor.armatureData.frameRate; bool isHaveCurve = false; for (int j = 0; j < animSubData.frameDatas.Length; ++j) { DragonBoneData.AnimFrameData frameData = animSubData.frameDatas[j]; float prevTweeneasing = float.PositiveInfinity; //前一帧的tweenEasing float[] prevCurves = null; if (j > 0) { prevTweeneasing = animSubData.frameDatas[j - 1].tweenEasing; prevCurves = animSubData.frameDatas[j - 1].curve; } TangentMode tanModeL = GetPrevFrameTangentMode(prevTweeneasing, prevCurves); TangentMode tanModeR = TangentMode.Linear; if (frameData.curve != null && frameData.curve.Length > 0) { tanModeR = TangentMode.Editable; isHaveCurve = true; } else { if (frameData.tweenEasing == float.PositiveInfinity) { tanModeR = TangentMode.Stepped; } else if (frameData.tweenEasing == 0) { tanModeR = TangentMode.Linear; } else if (frameData.tweenEasing == 1) { tanModeR = TangentMode.Smooth; } else if (frameData.tweenEasing == 2) { tanModeR = TangentMode.Linear; } } //mesh animation if (vertexcurvexArray != null) { for (int k = 0; k < vertexcurvexArray.Count; ++k) { Transform ffdNode = slotNode.GetChild(k); if (ffdNode.name == animSubData.name) { AnimationCurve[] vertex_xcurves = vertexcurvexArray[k]; AnimationCurve[] vertex_ycurves = vertexcurveyArray[k]; int len = ffdNode.childCount; if (frameData.vertices != null && frameData.vertices.Length > 0) { for (int r = 0; r < len; ++r) { AnimationCurve vertex_xcurve = vertex_xcurves[r]; AnimationCurve vertex_ycurve = vertex_ycurves[r]; Transform vCtr = ffdNode.GetChild(r); //顶点控制点 if (r >= frameData.offset && r - frameData.offset < frameData.vertices.Length) { Keyframe kfx = KeyframeUtil.GetNew(during, vCtr.localPosition.x + frameData.vertices[r - frameData.offset].x, tanModeL, tanModeR); vertex_xcurve.AddKey(kfx); Keyframe kfy = KeyframeUtil.GetNew(during, vCtr.localPosition.y + frameData.vertices[r - frameData.offset].y, tanModeL, tanModeR); vertex_ycurve.AddKey(kfy); } else { Keyframe kfx = KeyframeUtil.GetNew(during, vCtr.localPosition.x, tanModeL, tanModeR); vertex_xcurve.AddKey(kfx); Keyframe kfy = KeyframeUtil.GetNew(during, vCtr.localPosition.y, tanModeL, tanModeR); vertex_ycurve.AddKey(kfy); } } } else { //add default vertex position for (int r = 0; r < len; ++r) { AnimationCurve vertex_xcurve = vertex_xcurves[r]; AnimationCurve vertex_ycurve = vertex_ycurves[r]; Transform vCtr = slotNode.GetChild(k).GetChild(r); //顶点控制点 Keyframe kfx = KeyframeUtil.GetNew(during, vCtr.localPosition.x, tanModeL, tanModeR); vertex_xcurve.AddKey(kfx); Keyframe kfy = KeyframeUtil.GetNew(during, vCtr.localPosition.y, tanModeL, tanModeR); vertex_ycurve.AddKey(kfy); } } } } } during += frameData.duration * perKeyTime; } string path = ""; if (slotPathKV.ContainsKey(slotName)) { path = slotPathKV[slotName]; } else { path = GetNodeRelativePath(armatureEditor, slotNode); slotPathKV[slotName] = path; } if (vertexcurvexArray != null) { for (int k = 0; k < vertexcurvexArray.Count; ++k) { Transform ffdNode = slotNode.GetChild(k); if (ffdNode.name == animSubData.name) { changedSpriteMeshsKV[path + "/" + ffdNode.name] = ffdNode.GetComponent <SpriteMesh>(); AnimationCurve[] vertex_xcurves = vertexcurvexArray[k]; AnimationCurve[] vertex_ycurves = vertexcurveyArray[k]; for (int r = 0; r < vertex_xcurves.Length; ++r) { AnimationCurve vertex_xcurve = vertex_xcurves[r]; AnimationCurve vertex_ycurve = vertex_ycurves[r]; Transform v = ffdNode.GetChild(r); string ctrlPath = path + "/" + ffdNode.name + "/" + v.name; CurveExtension.OptimizesCurve(vertex_xcurve); CurveExtension.OptimizesCurve(vertex_ycurve); bool vcurveFlag = false; if (vertex_xcurve.keys != null && vertex_xcurve.keys.Length > 0 && CheckCurveValid(vertex_xcurve, v.localPosition.x)) { vcurveFlag = true; } if (vertex_ycurve.keys != null && vertex_ycurve.keys.Length > 0 && CheckCurveValid(vertex_ycurve, v.localPosition.y)) { vcurveFlag = true; } if (vcurveFlag) { if (isHaveCurve) { SetCustomCurveTangents(vertex_xcurve, animSubData.frameDatas); } CurveExtension.UpdateAllLinearTangents(vertex_xcurve); AnimationUtility.SetEditorCurve(clip, EditorCurveBinding.FloatCurve(ctrlPath, typeof(Transform), "m_LocalPosition.x"), vertex_xcurve); if (isHaveCurve) { SetCustomCurveTangents(vertex_ycurve, animSubData.frameDatas); } CurveExtension.UpdateAllLinearTangents(vertex_ycurve); AnimationUtility.SetEditorCurve(clip, EditorCurveBinding.FloatCurve(ctrlPath, typeof(Transform), "m_LocalPosition.y"), vertex_ycurve); } } } } } } }
static void CreateAnimZOrder(ArmatureEditor armatureEditor, AnimationClip clip, DragonBoneData.AnimSubData[] subDatas) { if (subDatas == null) { return; } int len = subDatas.Length; float perKeyTime = 1f / armatureEditor.armatureData.frameRate; for (int i = 0; i < len; ++i) { DragonBoneData.AnimSubData animSubData = subDatas[i]; float during = animSubData.offset; Dictionary <string, AnimationCurve> slotZOrderKV = new Dictionary <string, AnimationCurve>(); DragonBoneData.AnimFrameData[] frameDatas = animSubData.frameDatas; for (int j = 0; j < frameDatas.Length; ++j) { DragonBoneData.AnimFrameData frameData = frameDatas[j]; if (frameData.zOrder != null && frameData.zOrder.Length > 0) { for (int z = 0; z < frameData.zOrder.Length; z += 2) { int slotIdx = frameData.zOrder[z]; int changeZ = frameData.zOrder[z + 1]; Slot slot = armatureEditor.slots[slotIdx]; string path = ""; if (slotPathKV.ContainsKey(slot.name)) { path = slotPathKV[slot.name]; } else { path = GetNodeRelativePath(armatureEditor, slot.transform); slotPathKV[slot.name] = path; } AnimationCurve curve = null; if (slotZOrderKV.ContainsKey(slot.name)) { curve = slotZOrderKV[slot.name]; } else { curve = new AnimationCurve(); slotZOrderKV[slot.name] = curve; } if (curve.length == 0 && during > 0) { //first key curve.AddKey(new Keyframe(0, 0, float.PositiveInfinity, float.PositiveInfinity)); } if (frameDatas.Length == j + 1) { //last curve.AddKey(new Keyframe(during + frameData.duration * perKeyTime, changeZ, float.PositiveInfinity, float.PositiveInfinity)); } curve.AddKey(new Keyframe(during, changeZ, float.PositiveInfinity, float.PositiveInfinity)); } } else { //set to default z for (int z = 0; z < armatureEditor.slots.Count; ++z) { Slot slot = armatureEditor.slots[z]; string path = ""; if (slotPathKV.ContainsKey(slot.name)) { path = slotPathKV[slot.name]; } else { path = GetNodeRelativePath(armatureEditor, slot.transform); slotPathKV[slot.name] = path; } if (slotZOrderKV.ContainsKey(slot.name)) { slotZOrderKV[slot.name].AddKey(new Keyframe(during, 0, float.PositiveInfinity, float.PositiveInfinity)); } } } during += frameData.duration * perKeyTime; } foreach (string name in slotZOrderKV.Keys) { AnimationCurve zOrderCurve = slotZOrderKV[name]; Slot slot = armatureEditor.slotsKV[name].GetComponent <Slot>(); CurveExtension.OptimizesCurve(zOrderCurve); if (zOrderCurve != null && zOrderCurve.keys != null && zOrderCurve.keys.Length > 0 && CheckCurveValid(zOrderCurve, slot.zOrder)) { clip.SetCurve(slotPathKV[name], typeof(Slot), "m_z", zOrderCurve); } } } }
public static void ParseArmtureData(ArmatureEditor armatureEditor, SimpleJSON.JSONClass armtureObj) { //parse bone data if (armtureObj.ContainKey("bone")) { SimpleJSON.JSONArray bones = armtureObj["bone"].AsArray; DragonBoneData.BoneData[] boneDatas = new DragonBoneData.BoneData[bones.Count]; for (int i = 0; i < bones.Count; ++i) { SimpleJSON.JSONClass boneObj = bones[i].AsObject; DragonBoneData.BoneData boneData = new DragonBoneData.BoneData(); if (boneObj.ContainKey("length")) { boneData.length = boneObj["length"].AsFloat; } if (boneObj.ContainKey("name")) { boneData.name = boneObj["name"].ToString().Trim(); } if (boneObj.ContainKey("parent")) { boneData.parent = boneObj["parent"].ToString(); } if (boneObj.ContainKey("inheritRotation")) { boneData.inheritRotation = boneObj["inheritRotation"].AsInt == 1?true:false; } if (boneObj.ContainKey("inheritScale")) { boneData.inheritScale = boneObj["inheritScale"].AsInt == 1?true:false; } if (boneObj.ContainKey("transform")) { SimpleJSON.JSONClass transformObj = boneObj["transform"].AsObject; DragonBoneData.TransformData transData = new DragonBoneData.TransformData(); if (transformObj.ContainKey("x")) { transData.x = transformObj["x"].AsFloat * 0.01f; } if (transformObj.ContainKey("y")) { transData.y = -transformObj["y"].AsFloat * 0.01f; } if (transformObj.ContainKey("skX")) { transData.rotate = -transformObj["skX"].AsFloat; } if (transformObj.ContainKey("scX")) { transData.scx = transformObj["scX"].AsFloat; } if (transformObj.ContainKey("scY")) { transData.scy = transformObj["scY"].AsFloat; } boneData.transform = transData; } boneDatas[i] = boneData; armatureEditor.bonesDataKV[boneData.name] = boneData; } armatureEditor.armatureData.boneDatas = boneDatas; } //parse slot data if (armtureObj.ContainKey("slot")) { SimpleJSON.JSONArray slots = armtureObj["slot"].AsArray; DragonBoneData.SlotData[] slotDatas = new DragonBoneData.SlotData[slots.Count]; bool isMC = armatureEditor.armatureData.type.Equals("MovieClip"); for (int i = 0; i < slots.Count; ++i) { SimpleJSON.JSONClass slotObj = slots[i].AsObject; DragonBoneData.SlotData slotData = new DragonBoneData.SlotData(); if (slotObj.ContainKey("name")) { slotData.name = slotObj["name"].ToString(); } if (slotObj.ContainKey("parent")) { slotData.parent = slotObj["parent"].ToString(); } if (slotObj.ContainKey("z")) { slotData.z = -slotObj["z"].AsFloat * armatureEditor.zoffset; } if (!isMC) { if (slotObj.ContainKey("displayIndex")) { slotData.displayIndex = slotObj["displayIndex"].AsInt; } } if (slotObj.ContainKey("scale")) { slotData.scale = slotObj["scale"].AsFloat; } if (slotObj.ContainKey("blendMode")) { slotData.blendMode = slotObj["blendMode"].ToString(); } if (slotObj.ContainKey("color")) { SimpleJSON.JSONClass colorObj = slotObj["color"].AsObject; DragonBoneData.ColorData colorData = new DragonBoneData.ColorData(); if (colorObj.ContainKey("aM")) { colorData.aM = colorObj["aM"].AsFloat * 0.01f; } if (colorObj.ContainKey("a0")) { colorData.aM += colorObj["a0"].AsFloat / 255f; } if (colorObj.ContainKey("rM")) { colorData.rM = colorObj["rM"].AsFloat * 0.01f; } if (colorObj.ContainKey("r0")) { colorData.rM += colorObj["r0"].AsFloat / 255f; } if (colorObj.ContainKey("gM")) { colorData.gM = colorObj["gM"].AsFloat * 0.01f; } if (colorObj.ContainKey("g0")) { colorData.gM += colorObj["g0"].AsFloat / 255f; } if (colorObj.ContainKey("bM")) { colorData.bM = colorObj["bM"].AsFloat * 0.01f; } if (colorObj.ContainKey("b0")) { colorData.bM += colorObj["b0"].AsFloat / 255f; } slotData.color = colorData; } slotDatas[i] = slotData; armatureEditor.slotsDataKV[slotData.name] = slotData; } armatureEditor.armatureData.slotDatas = slotDatas; } //parse IK data if (armtureObj.ContainKey("ik")) { SimpleJSON.JSONArray iks = armtureObj["ik"].AsArray; DragonBoneData.IKData[] ikDatas = new DragonBoneData.IKData[iks.Count]; for (int i = 0; i < iks.Count; ++i) { SimpleJSON.JSONClass ikObj = iks[i].AsObject; DragonBoneData.IKData ikData = new DragonBoneData.IKData(); if (ikObj.ContainKey("name")) { ikData.name = ikObj["name"].ToString(); } if (ikObj.ContainKey("bone")) { ikData.bone = ikObj["bone"].ToString(); } if (ikObj.ContainKey("target")) { ikData.target = ikObj["target"].ToString(); } if (ikObj.ContainKey("bendPositive")) { ikData.bendPositive = ikObj["bendPositive"].AsBool; } if (ikObj.ContainKey("chain")) { ikData.chain = ikObj["chain"].AsInt; } if (ikObj.ContainKey("weight")) { ikData.weight = ikObj["weight"].AsFloat; } ikDatas[i] = ikData; } armatureEditor.armatureData.ikDatas = ikDatas; } //parse animation file if (armtureObj.ContainKey("animation")) { SimpleJSON.JSONArray anims = armtureObj["animation"].AsArray; DragonBoneData.AnimationData[] animDatas = new DragonBoneData.AnimationData[anims.Count]; for (int i = 0; i < anims.Count; ++i) { SimpleJSON.JSONClass animObj = anims[i].AsObject; DragonBoneData.AnimationData animData = new DragonBoneData.AnimationData(); if (animObj.ContainKey("name")) { animData.name = animObj["name"].ToString().Trim(); } if (animObj.ContainKey("playTimes")) { animData.playTimes = animObj["playTimes"].AsInt; } if (animObj.ContainKey("duration")) { animData.duration = animObj["duration"].AsInt; } if (animData.duration == 0) { animData.duration = 1; } if (animObj.ContainKey("frame")) { ParseAnimFrames(animObj["frame"].AsArray, animData); } if (animObj.ContainKey("bone")) { SimpleJSON.JSONArray bones = animObj["bone"].AsArray; animData.boneDatas = new DragonBoneData.AnimSubData[bones.Count]; ParsetAnimBoneSlot(armatureEditor, bones, animData.boneDatas); } if (animObj.ContainKey("slot")) { SimpleJSON.JSONArray slots = animObj["slot"].AsArray; animData.slotDatas = new DragonBoneData.AnimSubData[slots.Count]; ParsetAnimBoneSlot(armatureEditor, slots, animData.slotDatas); } //ffd if (animObj.ContainKey("ffd")) { SimpleJSON.JSONArray ffds = animObj["ffd"].AsArray; animData.ffdDatas = new DragonBoneData.AnimSubData[ffds.Count]; ParsetAnimBoneSlot(armatureEditor, ffds, animData.ffdDatas); } //zOrder if (animObj.ContainKey("zOrder")) { SimpleJSON.JSONClass zOrders = animObj["zOrder"].AsObject; ParseAnimSortOrder(armatureEditor, zOrders, animData); } animDatas[i] = animData; } armatureEditor.armatureData.animDatas = animDatas; } //parse skin data if (armtureObj.ContainKey("skin")) { SimpleJSON.JSONArray skins = armtureObj["skin"].AsArray; DragonBoneData.SkinData[] skinDatas = new DragonBoneData.SkinData[skins.Count]; for (int i = 0; i < skins.Count; ++i) { DragonBoneData.SkinData skinData = new DragonBoneData.SkinData(); skinDatas[i] = skinData; SimpleJSON.JSONClass skinObj = skins[i].AsObject; string skinName = skinObj["name"].ToString(); skinData.skinName = skinName; if (skinObj.ContainKey("slot")) { SimpleJSON.JSONArray slots = skinObj["slot"].AsArray; skinData.slots = new DragonBoneData.SkinSlotData[slots.Count]; for (int j = 0; j < slots.Count; ++j) { DragonBoneData.SkinSlotData skinSlotData = new DragonBoneData.SkinSlotData(); SimpleJSON.JSONClass slot = slots[j].AsObject; skinData.slots[j] = skinSlotData; if (slot.ContainKey("name")) { skinSlotData.slotName = slot["name"].ToString(); } if (slot.ContainKey("display")) { SimpleJSON.JSONArray display = slot["display"].AsArray; skinSlotData.displays = new DragonBoneData.SkinSlotDisplayData[display.Count]; for (int k = 0; k < display.Count; ++k) { DragonBoneData.SkinSlotDisplayData displayData = new DragonBoneData.SkinSlotDisplayData(); skinSlotData.displays[k] = displayData; SimpleJSON.JSONClass displayObj = display[k].AsObject; if (displayObj.ContainKey("name")) { displayData.textureName = displayObj["name"].ToString().Replace('/', '_'); } if (displayObj.ContainKey("path")) { displayData.texturePath = displayObj["path"].ToString(); } else { displayData.texturePath = displayData.textureName; } if (displayObj.ContainKey("type")) { displayData.type = displayObj["type"].ToString(); } if (displayObj.ContainKey("subType")) { displayData.subType = displayObj["subType"].ToString(); } if (displayObj.ContainKey("pivot")) { displayData.pivot = new Vector2(displayObj["pivot"].AsObject["x"].AsFloat, displayObj["pivot"].AsObject["y"].AsFloat); } if (displayObj.ContainKey("transform")) { SimpleJSON.JSONClass transformObj = displayObj["transform"].AsObject; DragonBoneData.TransformData transData = new DragonBoneData.TransformData(); if (transformObj.ContainKey("x")) { transData.x = transformObj["x"].AsFloat * 0.01f; } if (transformObj.ContainKey("y")) { transData.y = -transformObj["y"].AsFloat * 0.01f; } if (transformObj.ContainKey("skX")) { transData.rotate = -transformObj["skX"].AsFloat; } if (transformObj.ContainKey("scX")) { transData.scx = transformObj["scX"].AsFloat; } if (transformObj.ContainKey("scY")) { transData.scy = transformObj["scY"].AsFloat; } displayData.transform = transData; } //uv if (displayObj.ContainKey("uvs")) { SimpleJSON.JSONArray uvsObj = displayObj["uvs"].AsArray; int index = 0; displayData.uvs = new Vector2[uvsObj.Count / 2]; for (int z = 0; z < uvsObj.Count; z += 2) { Vector2 uv = new Vector2(uvsObj[z].AsFloat, 1 - uvsObj[z + 1].AsFloat); displayData.uvs[index] = uv; ++index; } } //weight if (displayObj.ContainKey("weights")) { SimpleJSON.JSONArray weightsObj = displayObj["weights"].AsArray; displayData.weights = new float[weightsObj.Count]; for (int z = 0; z < weightsObj.Count; ++z) { displayData.weights[z] = weightsObj[z].AsFloat; } } //bonepose if (displayObj.ContainKey("bonePose")) { SimpleJSON.JSONArray bonePoseObj = displayObj["bonePose"].AsArray; displayData.bonePose = new float[bonePoseObj.Count]; for (int z = 0; z < bonePoseObj.Count; z += 7) { displayData.bonePose[z] = bonePoseObj[z].AsFloat; displayData.bonePose[z + 1] = bonePoseObj[z + 1].AsFloat; //a displayData.bonePose[z + 2] = bonePoseObj[z + 2].AsFloat; //b displayData.bonePose[z + 3] = bonePoseObj[z + 3].AsFloat; //c displayData.bonePose[z + 4] = bonePoseObj[z + 4].AsFloat; //d displayData.bonePose[z + 5] = bonePoseObj[z + 5].AsFloat; //tx displayData.bonePose[z + 6] = bonePoseObj[z + 6].AsFloat; //ty Matrix2D m = new Matrix2D(displayData.bonePose[z + 1], displayData.bonePose[z + 2], displayData.bonePose[z + 3], displayData.bonePose[z + 4], displayData.bonePose[z + 5], displayData.bonePose[z + 6]); armatureEditor.bonePoseKV[displayData.textureName + armatureEditor.armatureData.boneDatas[(int)displayData.bonePose[z]].name] = m; } } Matrix2D slotPoseMat = null; //slotpose if (displayObj.ContainKey("slotPose")) { SimpleJSON.JSONArray slotPoseObj = displayObj["slotPose"].AsArray; slotPoseMat = new Matrix2D(slotPoseObj[0].AsFloat, slotPoseObj[1].AsFloat, slotPoseObj[2].AsFloat, slotPoseObj[3].AsFloat, slotPoseObj[4].AsFloat, slotPoseObj[5].AsFloat); } //vertex if (displayObj.ContainKey("vertices")) { SimpleJSON.JSONArray verticesObj = displayObj["vertices"].AsArray; displayData.vertices = new Vector3[verticesObj.Count / 2]; for (int z = 0; z < verticesObj.Count; z += 2) { int vertexIndex = z / 2; Vector3 vertex = new Vector3(verticesObj[z].AsFloat, verticesObj[z + 1].AsFloat, 0f); if (slotPoseMat != null) { //slotPose转换 vertex = (Vector3)slotPoseMat.TransformPoint(vertex.x, vertex.y); } vertex.x *= 0.01f; vertex.y *= -0.01f; displayData.vertices[vertexIndex] = vertex; } } //triangles if (displayObj.ContainKey("triangles")) { SimpleJSON.JSONArray trianglesObj = displayObj["triangles"].AsArray; displayData.triangles = new int[trianglesObj.Count]; for (int z = 0; z < trianglesObj.Count; z++) { displayData.triangles[z] = trianglesObj[z].AsInt; } //dragonBone和unity的z相反 for (int z = 0; z < displayData.triangles.Length; z += 3) { int f1 = displayData.triangles[z]; int f3 = displayData.triangles[z + 2]; displayData.triangles[z] = f3; displayData.triangles[z + 2] = f1; } } //edges //userdeges } } } } } armatureEditor.armatureData.skinDatas = skinDatas; } }
static void ParseFrames(ArmatureEditor armatureEditor, DragonBoneData.AnimFrameData[] frameDatas, SimpleJSON.JSONArray frames, string subDataName, DragonBoneData.FrameType type) { for (int j = 0; j < frames.Count; ++j) { SimpleJSON.JSONClass frameObj = frames[j].AsObject; DragonBoneData.AnimFrameData frameData = new DragonBoneData.AnimFrameData(); if (frameObj.ContainKey("duration")) { frameData.duration = frameObj["duration"].AsInt; } if (frameData.duration == 0) { frameData.duration = 1; } if (frameObj.ContainKey("displayIndex")) { frameData.displayIndex = frameObj["displayIndex"].AsInt; } if (type == DragonBoneData.FrameType.DisplayFrame) { if (frameObj.ContainKey("value")) { frameData.displayIndex = frameObj ["value"].AsInt; } } if (frameObj.ContainKey("z")) { frameData.z = -frameObj["z"].AsInt * armatureEditor.zoffset; } if (frameObj.ContainKey("tweenEasing") && frameObj["tweenEasing"].ToString() != "null") { frameData.tweenEasing = frameObj["tweenEasing"].AsFloat; } if (frameObj.ContainKey("tweenRotate")) { frameData.tweenRotate = frameObj["tweenRotate"].AsInt; } if (frameObj.ContainKey("curve")) { SimpleJSON.JSONArray curves = frameObj["curve"].AsArray; if (curves.Count > 3) { frameData.curve = new float[4] { curves[0].AsFloat, curves[1].AsFloat, curves[curves.Count - 2].AsFloat, curves[curves.Count - 1].AsFloat }; } } if (frameObj.ContainKey("transform")) { SimpleJSON.JSONClass transformObj = frameObj["transform"].AsObject; DragonBoneData.TransformData transData = new DragonBoneData.TransformData(); if (transformObj.ContainKey("x")) { transData.x = transformObj["x"].AsFloat * 0.01f; } if (transformObj.ContainKey("y")) { transData.y = -transformObj["y"].AsFloat * 0.01f; } if (transformObj.ContainKey("skX")) { transData.rotate = -transformObj["skX"].AsFloat; } if (transformObj.ContainKey("scX")) { transData.scx = transformObj["scX"].AsFloat; } if (transformObj.ContainKey("scY")) { transData.scy = transformObj["scY"].AsFloat; } frameData.transformData = transData; } else { if (frameObj.ContainKey("x")) { if (frameData.transformData == null) { frameData.transformData = new DragonBoneData.TransformData(); } if (type == DragonBoneData.FrameType.TranslateFrame) { frameData.transformData.x = frameObj["x"].AsFloat * 0.01f; } else { //scx frameData.transformData.scx = frameObj["x"].AsFloat; } } if (frameObj.ContainKey("y")) { if (frameData.transformData == null) { frameData.transformData = new DragonBoneData.TransformData(); } if (type == DragonBoneData.FrameType.TranslateFrame) { frameData.transformData.y = -frameObj["y"].AsFloat * 0.01f; } else { //scy frameData.transformData.scy = frameObj["y"].AsFloat; } } if (frameObj.ContainKey("rotate")) { if (frameData.transformData == null) { frameData.transformData = new DragonBoneData.TransformData(); } frameData.transformData.rotate = -frameObj["rotate"].AsFloat; } } if (frameObj.ContainKey("color")) { SimpleJSON.JSONClass colorObj = frameObj["color"].AsObject; DragonBoneData.ColorData colorData = new DragonBoneData.ColorData(); if (colorObj.ContainKey("aM")) { colorData.aM = colorObj["aM"].AsFloat * 0.01f; } if (colorObj.ContainKey("a0")) { colorData.aM += colorObj["a0"].AsFloat / 255f; } if (colorObj.ContainKey("rM")) { colorData.rM = colorObj["rM"].AsFloat * 0.01f; } if (colorObj.ContainKey("r0")) { colorData.rM += colorObj["r0"].AsFloat / 255f; } if (colorObj.ContainKey("gM")) { colorData.gM = colorObj["gM"].AsFloat * 0.01f; } if (colorObj.ContainKey("g0")) { colorData.gM += colorObj["g0"].AsFloat / 255f; } if (colorObj.ContainKey("bM")) { colorData.bM = colorObj["bM"].AsFloat * 0.01f; } if (colorObj.ContainKey("b0")) { colorData.bM += colorObj["b0"].AsFloat / 255f; } frameData.color = colorData; } //ffd animation //vertex offset bool startFromY = false; if (frameObj.ContainKey("offset")) { startFromY = frameObj["offset"].AsInt % 2 != 0; //从Y开始 frameData.offset = frameObj["offset"].AsInt / 2; } if (frameObj.ContainKey("vertices")) //local vertex { SimpleJSON.JSONArray verticesObj = frameObj["vertices"].AsArray; int index = 0; int k = 0; if (startFromY) { frameData.vertices = new Vector2[verticesObj.Count / 2 + 1]; frameData.vertices[index] = new Vector2(0, -verticesObj[k].AsFloat * 0.01f); k = 1; ++index; } else { frameData.vertices = new Vector2[verticesObj.Count / 2]; } for (; k < verticesObj.Count && k + 1 < verticesObj.Count; k += 2) { frameData.vertices[index] = new Vector2(verticesObj[k].AsFloat * 0.01f, -verticesObj[k + 1].AsFloat * 0.01f); ++index; } armatureEditor.ffdKV[subDataName] = true; } frameDatas[j] = frameData; } }
public static void ParsetAnimBoneSlot(ArmatureEditor armatureEditor, SimpleJSON.JSONArray animBonesSlots, DragonBoneData.AnimSubData[] animDatas) { for (int i = 0; i < animBonesSlots.Count; ++i) { SimpleJSON.JSONClass boneSlotObj = animBonesSlots[i].AsObject; DragonBoneData.AnimSubData subData = new DragonBoneData.AnimSubData(); if (boneSlotObj.ContainKey("name")) { subData.name = boneSlotObj["name"].ToString().Replace('/', '_'); } if (boneSlotObj.ContainKey("slot")) { subData.slot = boneSlotObj["slot"].ToString().Replace('/', '_'); } if (boneSlotObj.ContainKey("scale")) { subData.scale = boneSlotObj["scale"].AsFloat; } if (boneSlotObj.ContainKey("offset")) { subData.offset = boneSlotObj["offset"].AsFloat; } if (boneSlotObj.ContainKey("frame")) //for 5.3 以下 { SimpleJSON.JSONArray frames = boneSlotObj["frame"].AsArray; if (frames != null && frames.Count > 0) { subData.frameDatas = new DragonBoneData.AnimFrameData[frames.Count]; ParseFrames(armatureEditor, subData.frameDatas, frames, subData.name, DragonBoneData.FrameType.Frame); } } else //for 5.5 及以上 { if (boneSlotObj.ContainKey("translateFrame")) { SimpleJSON.JSONArray frames = boneSlotObj["translateFrame"].AsArray; if (frames != null && frames.Count > 0) { subData.translateFrameDatas = new DragonBoneData.AnimFrameData[frames.Count]; ParseFrames(armatureEditor, subData.translateFrameDatas, frames, subData.name, DragonBoneData.FrameType.TranslateFrame); } } if (boneSlotObj.ContainKey("rotateFrame")) { SimpleJSON.JSONArray frames = boneSlotObj["rotateFrame"].AsArray; if (frames != null && frames.Count > 0) { subData.rotateFrameDatas = new DragonBoneData.AnimFrameData[frames.Count]; ParseFrames(armatureEditor, subData.rotateFrameDatas, frames, subData.name, DragonBoneData.FrameType.RotateFrame); } } if (boneSlotObj.ContainKey("scaleFrame")) { SimpleJSON.JSONArray frames = boneSlotObj["scaleFrame"].AsArray; if (frames != null && frames.Count > 0) { subData.scaleFrameDatas = new DragonBoneData.AnimFrameData[frames.Count]; ParseFrames(armatureEditor, subData.scaleFrameDatas, frames, subData.name, DragonBoneData.FrameType.ScaleFrame); } } if (boneSlotObj.ContainKey("colorFrame")) { SimpleJSON.JSONArray frames = boneSlotObj["colorFrame"].AsArray; if (frames != null && frames.Count > 0) { subData.colorFrameDatas = new DragonBoneData.AnimFrameData[frames.Count]; ParseFrames(armatureEditor, subData.colorFrameDatas, frames, subData.name, DragonBoneData.FrameType.ColorFrame); } } if (boneSlotObj.ContainKey("displayFrame")) { SimpleJSON.JSONArray frames = boneSlotObj["displayFrame"].AsArray; if (frames != null && frames.Count > 0) { subData.displayFrameDatas = new DragonBoneData.AnimFrameData[frames.Count]; ParseFrames(armatureEditor, subData.displayFrameDatas, frames, subData.name, DragonBoneData.FrameType.DisplayFrame); } } } animDatas[i] = subData; } }
static void CreateBoneAnim(ArmatureEditor armatureEditor, AnimationClip clip, DragonBoneData.AnimSubData[] subDatas, Dictionary <string, Transform> transformKV) { if (subDatas == null) { return; } Dictionary <string, string> bonePathKV = new Dictionary <string, string>(); for (int i = 0; i < subDatas.Length; ++i) { DragonBoneData.AnimSubData animSubData = subDatas[i]; string boneName = animSubData.name; Transform boneNode = transformKV[boneName]; DragonBoneData.TransformData defaultTransformData = armatureEditor.bonesDataKV[animSubData.name].transform; AnimationCurve xcurve = new AnimationCurve(); AnimationCurve ycurve = new AnimationCurve(); AnimationCurve sxcurve = new AnimationCurve(); AnimationCurve sycurve = new AnimationCurve(); AnimationCurve rotatecurve = new AnimationCurve(); float during = animSubData.offset; float perKeyTime = 1f / armatureEditor.armatureData.frameRate; bool isHaveCurve = false; for (int j = 0; j < animSubData.frameDatas.Length; ++j) { DragonBoneData.AnimFrameData frameData = animSubData.frameDatas[j]; float prevTweeneasing = float.PositiveInfinity; //前一帧的tweenEasing float[] prevCurves = null; if (j > 0) { prevTweeneasing = animSubData.frameDatas[j - 1].tweenEasing; prevCurves = animSubData.frameDatas[j - 1].curve; } TangentMode tanModeL = GetPrevFrameTangentMode(prevTweeneasing, prevCurves); TangentMode tanModeR = TangentMode.Linear; if (frameData.curve != null && frameData.curve.Length > 0) { tanModeR = TangentMode.Editable; isHaveCurve = true; } else { if (frameData.tweenEasing == float.PositiveInfinity) { tanModeR = TangentMode.Stepped; } else if (frameData.tweenEasing == 0) { tanModeR = TangentMode.Linear; } else if (frameData.tweenEasing == 1) { tanModeR = TangentMode.Smooth; } else if (frameData.tweenEasing == 2) { tanModeR = TangentMode.Linear; } } if (frameData.transformData != null) { if (!float.IsNaN(frameData.transformData.x)) { if (!float.IsNaN(defaultTransformData.x)) { xcurve.AddKey(KeyframeUtil.GetNew(during, frameData.transformData.x + defaultTransformData.x, tanModeL, tanModeR)); } else { xcurve.AddKey(KeyframeUtil.GetNew(during, frameData.transformData.x, tanModeL, tanModeR)); } } else if (!float.IsNaN(defaultTransformData.x)) { xcurve.AddKey(KeyframeUtil.GetNew(during, defaultTransformData.x, tanModeL, tanModeR)); } if (!float.IsNaN(frameData.transformData.y)) { if (!float.IsNaN(defaultTransformData.y)) { ycurve.AddKey(KeyframeUtil.GetNew(during, frameData.transformData.y + defaultTransformData.y, tanModeL, tanModeR)); } else { ycurve.AddKey(KeyframeUtil.GetNew(during, frameData.transformData.y, tanModeL, tanModeR)); } } else if (!float.IsNaN(defaultTransformData.y)) { ycurve.AddKey(KeyframeUtil.GetNew(during, defaultTransformData.y, tanModeL, tanModeR)); } if (!float.IsNaN(frameData.transformData.rotate)) { float rotate = frameData.transformData.rotate + defaultTransformData.rotate; rotatecurve.AddKey(KeyframeUtil.GetNew(during, rotate, tanModeL, tanModeR)); } else if (!float.IsNaN(defaultTransformData.rotate)) { rotatecurve.AddKey(KeyframeUtil.GetNew(during, boneNode.localEulerAngles.z, tanModeL, tanModeR)); } if (!float.IsNaN(frameData.transformData.scx)) { sxcurve.AddKey(KeyframeUtil.GetNew(during, frameData.transformData.scx * defaultTransformData.scx, tanModeL, tanModeR)); } else { sxcurve.AddKey(KeyframeUtil.GetNew(during, boneNode.localScale.x, tanModeL, tanModeR)); } if (!float.IsNaN(frameData.transformData.scy)) { sycurve.AddKey(KeyframeUtil.GetNew(during, frameData.transformData.scy * defaultTransformData.scy, tanModeL, tanModeR)); } else { sycurve.AddKey(KeyframeUtil.GetNew(during, boneNode.localScale.y, tanModeL, tanModeR)); } } during += frameData.duration * perKeyTime; } CurveExtension.OptimizesCurve(xcurve); CurveExtension.OptimizesCurve(ycurve); CurveExtension.OptimizesCurve(sxcurve); CurveExtension.OptimizesCurve(sycurve); CurveExtension.OptimizesCurve(rotatecurve); string path = ""; if (bonePathKV.ContainsKey(boneName)) { path = bonePathKV[boneName]; } else { path = GetNodeRelativePath(armatureEditor, boneNode); bonePathKV[boneName] = path; } bool localPosFlag = false; if (xcurve.keys != null && xcurve.keys.Length > 0 && CheckCurveValid(xcurve, boneNode.localPosition.x)) { localPosFlag = true; } if (ycurve.keys != null && ycurve.keys.Length > 0 && CheckCurveValid(ycurve, boneNode.localPosition.y)) { localPosFlag = true; } if (localPosFlag) { if (isHaveCurve) { SetCustomCurveTangents(xcurve, animSubData.frameDatas); } CurveExtension.UpdateAllLinearTangents(xcurve); AnimationUtility.SetEditorCurve(clip, EditorCurveBinding.FloatCurve(path, typeof(Transform), "m_LocalPosition.x"), xcurve); if (isHaveCurve) { SetCustomCurveTangents(ycurve, animSubData.frameDatas); } CurveExtension.UpdateAllLinearTangents(ycurve); AnimationUtility.SetEditorCurve(clip, EditorCurveBinding.FloatCurve(path, typeof(Transform), "m_LocalPosition.y"), ycurve); } bool localSc = false; if (sxcurve.keys != null && sxcurve.keys.Length > 0 && CheckCurveValid(sxcurve, defaultTransformData.scx)) { localSc = true; } if (sycurve.keys != null && sycurve.keys.Length > 0 && CheckCurveValid(sycurve, defaultTransformData.scy)) { localSc = true; } if (localSc) { if (isHaveCurve) { SetCustomCurveTangents(sxcurve, animSubData.frameDatas); } CurveExtension.UpdateAllLinearTangents(sxcurve); AnimationUtility.SetEditorCurve(clip, EditorCurveBinding.FloatCurve(path, typeof(Transform), "m_LocalScale.x"), sxcurve); if (isHaveCurve) { SetCustomCurveTangents(sycurve, animSubData.frameDatas); } CurveExtension.UpdateAllLinearTangents(sycurve); AnimationUtility.SetEditorCurve(clip, EditorCurveBinding.FloatCurve(path, typeof(Transform), "m_LocalScale.y"), sycurve); } if (rotatecurve.keys != null && rotatecurve.keys.Length > 0 && CheckCurveValid(rotatecurve, defaultTransformData.rotate)) { CurveExtension.ClampCurveRotate360(rotatecurve); if (isHaveCurve) { SetCustomCurveTangents(rotatecurve, animSubData.frameDatas); } CurveExtension.UpdateAllLinearTangents(rotatecurve); clip.SetCurve(path, typeof(Transform), "localEulerAngles.z", rotatecurve); } } }
static void CreateSlotAnim(ArmatureEditor armatureEditor, AnimationClip clip, DragonBoneData.AnimSubData[] subDatas, Dictionary <string, Transform> transformKV) { for (int i = 0; i < subDatas.Length; ++i) { DragonBoneData.AnimSubData animSubData = subDatas[i]; string slotName = string.IsNullOrEmpty(animSubData.slot) ? animSubData.name : animSubData.slot; Transform slotNode = transformKV[slotName]; DragonBoneData.SlotData defaultSlotData = armatureEditor.slotsDataKV[slotName]; DragonBoneData.ColorData defaultColorData = defaultSlotData.color; AnimationCurve color_rcurve = new AnimationCurve(); AnimationCurve color_gcurve = new AnimationCurve(); AnimationCurve color_bcurve = new AnimationCurve(); AnimationCurve color_acurve = new AnimationCurve(); Renderer[] renders = slotNode.GetComponentsInChildren <Renderer>(); AnimationCurve[] renderCurves = new AnimationCurve[renders.Length]; for (int r = 0; r < renderCurves.Length; ++r) { renderCurves[r] = new AnimationCurve(); } float during = animSubData.offset; float perKeyTime = 1f / armatureEditor.armatureData.frameRate; bool isHaveCurve = false; for (int j = 0; j < animSubData.frameDatas.Length; ++j) { DragonBoneData.AnimFrameData frameData = animSubData.frameDatas[j]; float prevTweeneasing = float.PositiveInfinity; //前一帧的tweenEasing float[] prevCurves = null; if (j > 0) { prevTweeneasing = animSubData.frameDatas[j - 1].tweenEasing; prevCurves = animSubData.frameDatas[j - 1].curve; } TangentMode tanModeL = GetPrevFrameTangentMode(prevTweeneasing, prevCurves); TangentMode tanModeR = TangentMode.Linear; if (frameData.curve != null && frameData.curve.Length > 0) { tanModeR = TangentMode.Editable; isHaveCurve = true; } else { if (frameData.tweenEasing == float.PositiveInfinity) { tanModeR = TangentMode.Stepped; } else if (frameData.tweenEasing == 0) { tanModeR = TangentMode.Linear; } else if (frameData.tweenEasing == 1) { tanModeR = TangentMode.Smooth; } else if (frameData.tweenEasing == 2) { tanModeR = TangentMode.Linear; } } if (frameData.color != null) { if (defaultColorData == null) { defaultColorData = new DragonBoneData.ColorData(); } Color c = new Color( frameData.color.rM + frameData.color.r0, frameData.color.gM + frameData.color.g0, frameData.color.bM + frameData.color.b0, frameData.color.aM + frameData.color.a0 ); color_rcurve.AddKey(KeyframeUtil.GetNew(during, c.r, tanModeL, tanModeR)); color_gcurve.AddKey(KeyframeUtil.GetNew(during, c.g, tanModeL, tanModeR)); color_bcurve.AddKey(KeyframeUtil.GetNew(during, c.b, tanModeL, tanModeR)); color_acurve.AddKey(KeyframeUtil.GetNew(during, c.a, tanModeL, tanModeR)); } //改displyindex if (frameData.displayIndex == -1) { for (int r = 0; r < renders.Length; ++r) { renderCurves[r].AddKey(new Keyframe(during, 0f, float.PositiveInfinity, float.PositiveInfinity)); } } else { for (int r = 0; r < renders.Length; ++r) { if (r != frameData.displayIndex) { renderCurves[r].AddKey(new Keyframe(during, 0f, float.PositiveInfinity, float.PositiveInfinity)); } else { renderCurves[r].AddKey(new Keyframe(during, 1f, float.PositiveInfinity, float.PositiveInfinity)); } } } during += frameData.duration * perKeyTime; } CurveExtension.OptimizesCurve(color_rcurve); CurveExtension.OptimizesCurve(color_gcurve); CurveExtension.OptimizesCurve(color_bcurve); CurveExtension.OptimizesCurve(color_acurve); string path = ""; if (slotPathKV.ContainsKey(slotName)) { path = slotPathKV[slotName]; } else { path = GetNodeRelativePath(armatureEditor, slotNode); slotPathKV[slotNode.name] = path; } if (defaultColorData == null) { defaultColorData = new DragonBoneData.ColorData(); } float da = defaultColorData.aM + defaultColorData.a0; float dr = defaultColorData.rM + defaultColorData.r0; float dg = defaultColorData.gM + defaultColorData.g0; float db = defaultColorData.bM + defaultColorData.b0; if (armatureEditor.useUnitySprite) { SpriteRenderer[] sprites = slotNode.GetComponentsInChildren <SpriteRenderer>(); if (sprites != null) { for (int z = 0; z < sprites.Length; ++z) { string childPath = path + "/" + sprites[z].name; SetColorCurve <SpriteRenderer>(childPath, clip, color_rcurve, "m_Color.r", isHaveCurve, dr, animSubData.frameDatas); SetColorCurve <SpriteRenderer>(childPath, clip, color_gcurve, "m_Color.g", isHaveCurve, dg, animSubData.frameDatas); SetColorCurve <SpriteRenderer>(childPath, clip, color_bcurve, "m_Color.b", isHaveCurve, db, animSubData.frameDatas); SetColorCurve <SpriteRenderer>(childPath, clip, color_acurve, "m_Color.a", isHaveCurve, da, animSubData.frameDatas); } } } else { SpriteFrame[] sprites = slotNode.GetComponentsInChildren <SpriteFrame>(); if (sprites != null) { for (int z = 0; z < sprites.Length; ++z) { string childPath = path + "/" + sprites[z].name; bool anim_r = SetColorCurve <SpriteFrame>(childPath, clip, color_rcurve, "m_color.r", isHaveCurve, dr, animSubData.frameDatas); bool anim_g = SetColorCurve <SpriteFrame>(childPath, clip, color_gcurve, "m_color.g", isHaveCurve, dg, animSubData.frameDatas); bool anim_b = SetColorCurve <SpriteFrame>(childPath, clip, color_bcurve, "m_color.b", isHaveCurve, db, animSubData.frameDatas); bool anim_a = SetColorCurve <SpriteFrame>(childPath, clip, color_acurve, "m_color.a", isHaveCurve, da, animSubData.frameDatas); if (anim_r || anim_g || anim_b || anim_a) { changedSpriteFramesKV[childPath] = sprites[z]; } } } SpriteMesh[] spriteMeshs = slotNode.GetComponentsInChildren <SpriteMesh>(); if (spriteMeshs != null) { for (int z = 0; z < spriteMeshs.Length; ++z) { string childPath = path + "/" + spriteMeshs[z].name; bool anim_r = SetColorCurve <SpriteMesh>(childPath, clip, color_rcurve, "m_color.r", isHaveCurve, da, animSubData.frameDatas); bool anim_g = SetColorCurve <SpriteMesh>(childPath, clip, color_gcurve, "m_color.g", isHaveCurve, dg, animSubData.frameDatas); bool anim_b = SetColorCurve <SpriteMesh>(childPath, clip, color_bcurve, "m_color.b", isHaveCurve, db, animSubData.frameDatas); bool anim_a = SetColorCurve <SpriteMesh>(childPath, clip, color_acurve, "m_color.a", isHaveCurve, da, animSubData.frameDatas); if (anim_r || anim_g || anim_b || anim_a) { changedSpriteMeshsKV[childPath] = spriteMeshs[z]; } } } } for (int r = 0; r < renderCurves.Length; ++r) { AnimationCurve ac = renderCurves[r]; Renderer render = renders[r]; float defaultValue = render.enabled? 1: 0; if (ac.keys != null && ac.keys.Length > 0 && CheckCurveValid(ac, defaultValue)) { clip.SetCurve(path + "/" + render.name, typeof(GameObject), "m_IsActive", ac); //m_Enabled } } } }
static void ShowSpriteMesh(TextureFrame frame, Material mat, DragonBoneData.SkinSlotDisplayData displayData, Transform slot, ArmatureEditor armatureEditor, DragonBoneData.SlotData slotData) { armatureEditor.spriteMeshUsedMatKV[mat] = true; GameObject go = new GameObject(displayData.textureName); SpriteMesh sm = go.AddComponent <SpriteMesh>(); sm.atlasMat = mat; sm.vertices = displayData.vertices; sm.frame = frame; sm.uvs = displayData.uvs; sm.triangles = displayData.triangles; sm.colors = new Color[sm.vertices.Length]; for (int i = 0; i < sm.colors.Length; ++i) { sm.colors[i] = Color.white; } if (displayData.weights != null && displayData.weights.Length > 0) { sm.CreateMesh(true); if (armatureEditor.ffdKV.ContainsKey(displayData.textureName)) { //Vertex controller sm.vertControlTrans = new Transform[sm.vertices.Length]; for (int i = 0; i < sm.vertices.Length; ++i) { GameObject gov = new GameObject(go.name + "_v" + i); gov.transform.parent = go.transform; gov.transform.localPosition = sm.vertices[i]; gov.transform.localScale = Vector3.zero; sm.vertControlTrans[i] = gov.transform; } } } else { sm.CreateMesh(false); if (displayData.bonePose == null) { //Vertex controller sm.vertControlTrans = new Transform[sm.vertices.Length]; for (int i = 0; i < sm.vertices.Length; ++i) { GameObject gov = new GameObject(go.name + "_v" + i); gov.transform.parent = go.transform; gov.transform.localPosition = sm.vertices[i]; gov.transform.localScale = Vector3.zero; sm.vertControlTrans[i] = gov.transform; } } } sm.transform.parent = slot; if (displayData.bonePose != null) { SkinnedMeshRenderer skinnedMesh = sm.GetComponent <SkinnedMeshRenderer>(); if (skinnedMesh) { skinnedMesh.quality = SkinQuality.Bone4; if (displayData.weights != null && displayData.weights.Length > 0) { Transform[] bones = new Transform[displayData.bonePose.Length / 7]; for (int i = 0; i < displayData.bonePose.Length; i += 7) { int index = i / 7; int boneIndex = (int)displayData.bonePose[i]; bones[index] = armatureEditor.bones[boneIndex]; } List <BoneWeight> boneWeights = new List <BoneWeight>(); for (int i = 0; i < displayData.weights.Length; ++i) { int boneCount = (int)displayData.weights[i]; //骨骼数量 List <KeyValuePair <int, float> > boneWeightList = new List <KeyValuePair <int, float> >(); for (int j = 0; j < boneCount * 2; j += 2) { int boneIdx = (int)displayData.weights[i + j + 1]; float weight = displayData.weights[i + j + 2]; boneWeightList.Add(new KeyValuePair <int, float>(boneIdx, weight)); } //sort boneWeightList,desc boneWeightList.Sort(delegate(KeyValuePair <int, float> x, KeyValuePair <int, float> y) { if (x.Value == y.Value) { return(0); } return(x.Value < y.Value? 1: -1); }); BoneWeight bw = new BoneWeight(); for (int j = 0; j < boneWeightList.Count; ++j) { if (j == 0) { bw.boneIndex0 = GlobalBoneIndexToLocalBoneIndex(armatureEditor, boneWeightList[j].Key, bones); bw.weight0 = boneWeightList[j].Value; } else if (j == 1) { bw.boneIndex1 = GlobalBoneIndexToLocalBoneIndex(armatureEditor, boneWeightList[j].Key, bones); bw.weight1 = boneWeightList[j].Value; } else if (j == 2) { bw.boneIndex2 = GlobalBoneIndexToLocalBoneIndex(armatureEditor, boneWeightList[j].Key, bones); bw.weight2 = boneWeightList[j].Value; } else if (j == 3) { bw.boneIndex3 = GlobalBoneIndexToLocalBoneIndex(armatureEditor, boneWeightList[j].Key, bones); bw.weight3 = boneWeightList[j].Value; } } boneWeights.Add(bw); i += boneCount * 2; } Matrix4x4[] matrixArray = new Matrix4x4[bones.Length]; for (int i = 0; i < matrixArray.Length; ++i) { Transform bone = bones[i]; Vector3 bonePos = bone.localPosition; Quaternion boneRotate = bone.localRotation; Matrix2D m2d = armatureEditor.bonePoseKV[displayData.textureName + bone.name]; bone.position = new Vector3(m2d.tx * 0.01f, -m2d.ty * 0.01f, bone.position.z); bone.rotation = Quaternion.Euler(0f, 0f, -m2d.GetAngle()); matrixArray[i] = bone.worldToLocalMatrix * armatureEditor.armature.localToWorldMatrix; matrixArray[i] *= Matrix4x4.TRS(slot.localPosition, slot.localRotation, slot.localScale); bone.localPosition = bonePos; bone.localRotation = boneRotate; } skinnedMesh.bones = bones; skinnedMesh.sharedMesh.boneWeights = boneWeights.ToArray(); skinnedMesh.sharedMesh.bindposes = matrixArray; skinnedMesh.rootBone = slot; sm.bindposes = matrixArray; SpriteMesh.BoneWeightClass[] bwcs = new SpriteMesh.BoneWeightClass[boneWeights.Count]; for (int i = 0; i < boneWeights.Count; ++i) { SpriteMesh.BoneWeightClass bwc = new SpriteMesh.BoneWeightClass(); BoneWeight bw = boneWeights[i]; bwc.boneIndex0 = bw.boneIndex0; bwc.boneIndex1 = bw.boneIndex1; bwc.boneIndex2 = bw.boneIndex2; bwc.boneIndex3 = bw.boneIndex3; bwc.weight0 = bw.weight0; bwc.weight1 = bw.weight1; bwc.weight2 = bw.weight2; bwc.weight3 = bw.weight3; bwcs[i] = bwc; } sm.weights = bwcs; } } } DragonBoneData.TransformData tranform = displayData.transform; Vector3 localPos = Vector3.zero; if (!float.IsNaN(tranform.x)) { localPos.x = tranform.x; } if (!float.IsNaN(tranform.y)) { localPos.y = tranform.y; } sm.transform.localPosition = localPos; Vector3 localSc = Vector3.one; if (!float.IsNaN(tranform.scx)) { localSc.x = tranform.scx; } if (!float.IsNaN(tranform.scy)) { localSc.y = tranform.scy; } sm.transform.localScale = localSc; sm.color = slot.GetComponent <Slot>().color; sm.transform.localRotation = Quaternion.Euler(0, 0, tranform.rotate); }
static void CreateDragonBoneByDir(bool useUnitySprite) { if (Selection.activeObject is DefaultAsset) { string dirPath = AssetDatabase.GetAssetOrScenePath(Selection.activeObject); if (Directory.Exists(dirPath)) { string animJsonPath = null; Dictionary <string, string> texturePathKV = new Dictionary <string, string>(); Dictionary <string, string> textureJsonPathKV = new Dictionary <string, string>(); foreach (string path in Directory.GetFiles(dirPath)) { if (path.LastIndexOf(".meta") == -1) { if (path.IndexOf("_tex.json") > -1 || (System.IO.Path.GetExtension(path) == ".json" && path.IndexOf("texture.") > -1)) { int start = path.LastIndexOf("/") + 1; int end = path.LastIndexOf(".json"); textureJsonPathKV[path.Substring(start, end - start)] = path; continue; } if (path.IndexOf("_tex.png") > -1 || (System.IO.Path.GetExtension(path) == ".png" && path.IndexOf("texture.") > -1)) { int start = path.LastIndexOf("/") + 1; int end = path.LastIndexOf(".png"); texturePathKV[path.Substring(start, end - start)] = path; continue; } if (path.IndexOf("_ske.json") > -1 || (path.IndexOf("texture.json") == -1 && System.IO.Path.GetExtension(path) == ".json")) { animJsonPath = path; } } } if (!string.IsNullOrEmpty(animJsonPath) && texturePathKV.Count > 0 && textureJsonPathKV.Count > 0) { ArmatureEditor instance = ScriptableObject.CreateInstance <ArmatureEditor>(); instance.useUnitySprite = useUnitySprite; List <Atlas> atlasList = new List <Atlas>(); foreach (string name in texturePathKV.Keys) { if (textureJsonPathKV.ContainsKey(name)) { if (instance.altasTexture == null) { instance.altasTextAsset = AssetDatabase.LoadAssetAtPath <TextAsset>(textureJsonPathKV[name]); instance.altasTexture = AssetDatabase.LoadAssetAtPath <Texture2D>(texturePathKV[name]); } else { Atlas atlas = new Atlas(); atlas.atlasText = AssetDatabase.LoadAssetAtPath <TextAsset>(textureJsonPathKV[name]); atlas.texture = AssetDatabase.LoadAssetAtPath <Texture2D>(texturePathKV[name]); atlasList.Add(atlas); } } } instance.otherTextures = atlasList.ToArray(); instance.animTextAsset = AssetDatabase.LoadAssetAtPath <TextAsset>(animJsonPath); if (instance.altasTexture && instance.altasTextAsset && instance.animTextAsset) { instance.OnWizardCreate(); } DestroyImmediate(instance); } else if (useUnitySprite && !string.IsNullOrEmpty(animJsonPath)) { string spritesPath = null; foreach (string path in Directory.GetDirectories(dirPath)) { if (path.LastIndexOf("texture") > -1) { spritesPath = path; break; } } if (!string.IsNullOrEmpty(spritesPath)) { Dictionary <string, Sprite> spriteKV = new Dictionary <string, Sprite>(); foreach (string path in Directory.GetFiles(spritesPath)) { if (path.LastIndexOf(".png") > -1 && path.LastIndexOf(".meta") == -1) { Sprite sprite = AssetDatabase.LoadAssetAtPath <Sprite>(path); spriteKV[sprite.name] = sprite; } } if (spriteKV.Count > 0) { ArmatureEditor instance = ScriptableObject.CreateInstance <ArmatureEditor>(); instance.useUnitySprite = true; instance.isSingleSprite = true; instance.spriteKV = spriteKV; instance.animTextAsset = AssetDatabase.LoadAssetAtPath <TextAsset>(animJsonPath); instance.OnWizardCreate(); DestroyImmediate(instance); } } } } } }
public static void ShowBones(ArmatureEditor armatureEditor) { foreach (Transform b in armatureEditor.bonesKV.Values) { DragonBoneData.BoneData boneData = armatureEditor.bonesDataKV[b.name]; if (!string.IsNullOrEmpty(boneData.parent)) { if (armatureEditor.bonesKV.ContainsKey(boneData.parent)) { Transform parent = armatureEditor.bonesKV[boneData.parent]; b.transform.parent = parent.transform; } } else { b.transform.parent = armatureEditor.armature; } if (boneData.transform != null) { Vector3 localPos = Vector3.zero; if (!float.IsNaN(boneData.transform.x)) { localPos.x = boneData.transform.x; } if (!float.IsNaN(boneData.transform.y)) { localPos.y = boneData.transform.y; } b.transform.localPosition = localPos; Vector3 localSc = Vector3.one; if (!float.IsNaN(boneData.transform.scx)) { localSc.x = boneData.transform.scx; } if (!float.IsNaN(boneData.transform.scy)) { localSc.y = boneData.transform.scy; } b.transform.localScale = localSc; if (!float.IsNaN(boneData.transform.rotate)) { b.transform.localRotation = Quaternion.Euler(0, 0, boneData.transform.rotate); } } else { b.transform.localScale = Vector3.one; b.transform.localPosition = Vector3.zero; } } //设置inheritRotation,inheritScale ( not support) foreach (Transform b in armatureEditor.bonesKV.Values) { DragonBoneData.BoneData boneData = armatureEditor.bonesDataKV[b.name]; if (!boneData.inheritRotation) { Transform parent = b.parent; Vector3 rotate = Vector3.zero; while (parent != armatureEditor.armature) { rotate += parent.localEulerAngles; parent = parent.parent; } b.localEulerAngles -= rotate; } if (!boneData.inheritScale) { Transform parent = b.parent; Vector3 scale = Vector3.one; while (parent != armatureEditor.armature) { scale = new Vector3(scale.x * parent.localScale.x, scale.y * parent.localScale.y, scale.z * parent.localScale.z); parent = parent.parent; } b.localScale = new Vector3(b.localScale.x / scale.x, b.localScale.y / scale.y, b.localScale.z / scale.z); } } }
static void CreateWizard() { ArmatureEditor editor = ScriptableWizard.DisplayWizard <ArmatureEditor>("Create DragonBone", "Create"); editor.minSize = new Vector2(200f, 400f); if (Selection.activeObject is DefaultAsset) { string dirPath = AssetDatabase.GetAssetOrScenePath(Selection.activeObject); if (Directory.Exists(dirPath)) { string animJsonPath = null; Dictionary <string, string> texturePathKV = new Dictionary <string, string>(); Dictionary <string, string> textureJsonPathKV = new Dictionary <string, string>(); foreach (string path in Directory.GetFiles(dirPath)) { if (path.LastIndexOf(".meta") == -1) { if (System.IO.Path.GetExtension(path) == ".json" && (path.IndexOf("_tex") > -1 || path.IndexOf("texture") > -1)) { int start = path.LastIndexOf("/") + 1; int end = path.LastIndexOf(".json"); textureJsonPathKV[path.Substring(start, end - start)] = path; continue; } if (System.IO.Path.GetExtension(path) == ".png" && (path.IndexOf("_tex") > -1 || path.IndexOf("texture") > -1)) { int start = path.LastIndexOf("/") + 1; int end = path.LastIndexOf(".png"); texturePathKV[path.Substring(start, end - start)] = path; continue; } if (System.IO.Path.GetExtension(path) == ".json" && (path.IndexOf("_ske") > -1 || path.IndexOf("texture.json") == -1)) { animJsonPath = path; } } } if (!string.IsNullOrEmpty(animJsonPath)) { editor.animTextAsset = AssetDatabase.LoadAssetAtPath <TextAsset>(animJsonPath); } if (texturePathKV.Count > 0 && textureJsonPathKV.Count > 0) { List <Atlas> atlasList = new List <Atlas>(); foreach (string name in texturePathKV.Keys) { if (textureJsonPathKV.ContainsKey(name)) { if (editor.altasTexture == null) { editor.altasTextAsset = AssetDatabase.LoadAssetAtPath <TextAsset>(textureJsonPathKV[name]); editor.altasTexture = AssetDatabase.LoadAssetAtPath <Texture2D>(texturePathKV[name]); } else { Atlas atlas = new Atlas(); atlas.atlasText = AssetDatabase.LoadAssetAtPath <TextAsset>(textureJsonPathKV[name]); atlas.texture = AssetDatabase.LoadAssetAtPath <Texture2D>(texturePathKV[name]); atlasList.Add(atlas); } } } editor.otherTextures = atlasList.ToArray(); } else { string texturesFolder = ""; foreach (string path in Directory.GetDirectories(dirPath)) { if (path.IndexOf("texture") > -1) { editor.texturesFolderName = path.Substring(path.LastIndexOf("/") + 1); texturesFolder = path; break; } } if (!string.IsNullOrEmpty(texturesFolder)) { string[] paths = Directory.GetFiles(texturesFolder); if (paths.Length > 0) { editor.isSingleSprite = true; } } } } } }
static void CreateSlotAnim(ArmatureEditor armatureEditor, AnimationClip clip, DragonBoneData.AnimSubData[] subDatas, Dictionary <string, Transform> transformKV) { for (int i = 0; i < subDatas.Length; ++i) { DragonBoneData.AnimSubData animSubData = subDatas[i]; string slotName = string.IsNullOrEmpty(animSubData.slot) ? animSubData.name : animSubData.slot; Transform slotNode = transformKV[slotName]; DragonBoneData.SlotData defaultSlotData = armatureEditor.slotsDataKV[slotName]; DragonBoneData.ColorData defaultColorData = defaultSlotData.color; AnimationCurve color_rcurve = new AnimationCurve(); AnimationCurve color_gcurve = new AnimationCurve(); AnimationCurve color_bcurve = new AnimationCurve(); AnimationCurve color_acurve = new AnimationCurve(); AnimationCurve display_curve = new AnimationCurve(); float during = animSubData.offset; float perKeyTime = 1f / armatureEditor.armatureData.frameRate; bool isHaveCurve = false; for (int j = 0; j < animSubData.frameDatas.Length; ++j) { DragonBoneData.AnimFrameData frameData = animSubData.frameDatas[j]; float prevTweeneasing = float.PositiveInfinity; //前一帧的tweenEasing float[] prevCurves = null; if (j > 0) { prevTweeneasing = animSubData.frameDatas[j - 1].tweenEasing; prevCurves = animSubData.frameDatas[j - 1].curve; } TangentMode tanModeL = GetPrevFrameTangentMode(prevTweeneasing, prevCurves); TangentMode tanModeR = TangentMode.Linear; if (frameData.curve != null && frameData.curve.Length > 0) { tanModeR = TangentMode.Editable; isHaveCurve = true; } else { if (frameData.tweenEasing == float.PositiveInfinity) { tanModeR = TangentMode.Stepped; } else if (frameData.tweenEasing == 0) { tanModeR = TangentMode.Linear; } else if (frameData.tweenEasing == 1) { tanModeR = TangentMode.Smooth; } else if (frameData.tweenEasing == 2) { tanModeR = TangentMode.Linear; } } if (frameData.color != null) { if (defaultColorData == null) { defaultColorData = new DragonBoneData.ColorData(); } Color c = new Color( frameData.color.rM + frameData.color.r0, frameData.color.gM + frameData.color.g0, frameData.color.bM + frameData.color.b0, frameData.color.aM + frameData.color.a0 ); color_rcurve.AddKey(KeyframeUtil.GetNew(during, c.r, tanModeL, tanModeR)); color_gcurve.AddKey(KeyframeUtil.GetNew(during, c.g, tanModeL, tanModeR)); color_bcurve.AddKey(KeyframeUtil.GetNew(during, c.b, tanModeL, tanModeR)); color_acurve.AddKey(KeyframeUtil.GetNew(during, c.a, tanModeL, tanModeR)); } //改displyindex if (frameData.displayIndex > -2) { display_curve.AddKey(new Keyframe(during, frameData.displayIndex, float.PositiveInfinity, float.PositiveInfinity)); } during += frameData.duration * perKeyTime; } CurveExtension.OptimizesCurve(color_rcurve); CurveExtension.OptimizesCurve(color_gcurve); CurveExtension.OptimizesCurve(color_bcurve); CurveExtension.OptimizesCurve(color_acurve); CurveExtension.OptimizesCurve(display_curve); string path = ""; if (slotPathKV.ContainsKey(slotName)) { path = slotPathKV[slotName]; } else { path = GetNodeRelativePath(armatureEditor, slotNode); slotPathKV[slotNode.name] = path; } if (defaultColorData == null) { defaultColorData = new DragonBoneData.ColorData(); } float da = defaultColorData.aM + defaultColorData.a0; float dr = defaultColorData.rM + defaultColorData.r0; float dg = defaultColorData.gM + defaultColorData.g0; float db = defaultColorData.bM + defaultColorData.b0; SetColorCurve <Slot>(path, clip, color_rcurve, "color.r", isHaveCurve, dr, animSubData.frameDatas); SetColorCurve <Slot>(path, clip, color_gcurve, "color.g", isHaveCurve, dg, animSubData.frameDatas); SetColorCurve <Slot>(path, clip, color_bcurve, "color.b", isHaveCurve, db, animSubData.frameDatas); SetColorCurve <Slot>(path, clip, color_acurve, "color.a", isHaveCurve, da, animSubData.frameDatas); if (display_curve.keys != null && display_curve.keys.Length > 0 && CheckCurveValid(display_curve, slotNode.GetComponent <Slot>().displayIndex)) { clip.SetCurve(path, typeof(Slot), "m_DisplayIndex", display_curve); } } }
public static void ParsetAnimBoneSlot(ArmatureEditor armatureEditor, SimpleJSON.JSONArray animBonesSlots, DragonBoneData.AnimSubData[] animDatas) { for (int i = 0; i < animBonesSlots.Count; ++i) { SimpleJSON.JSONClass boneSlotObj = animBonesSlots[i].AsObject; DragonBoneData.AnimSubData subData = new DragonBoneData.AnimSubData(); if (boneSlotObj.ContainKey("name")) { subData.name = boneSlotObj["name"].ToString().Replace('/', '_'); } if (boneSlotObj.ContainKey("slot")) { subData.slot = boneSlotObj["slot"].ToString().Replace('/', '_'); } if (boneSlotObj.ContainKey("scale")) { subData.scale = boneSlotObj["scale"].AsFloat; } if (boneSlotObj.ContainKey("offset")) { subData.offset = boneSlotObj["offset"].AsFloat; } if (boneSlotObj.ContainKey("frame")) { SimpleJSON.JSONArray frames = boneSlotObj["frame"].AsArray; subData.frameDatas = new DragonBoneData.AnimFrameData[frames.Count]; for (int j = 0; j < frames.Count; ++j) { SimpleJSON.JSONClass frameObj = frames[j].AsObject; DragonBoneData.AnimFrameData frameData = new DragonBoneData.AnimFrameData(); if (frameObj.ContainKey("duration")) { frameData.duration = frameObj["duration"].AsInt; } if (frameData.duration == 0) { frameData.duration = 1; } if (frameObj.ContainKey("displayIndex")) { frameData.displayIndex = frameObj["displayIndex"].AsInt; } if (frameObj.ContainKey("z")) { frameData.z = -frameObj["z"].AsInt * armatureEditor.zoffset; } if (frameObj.ContainKey("tweenEasing") && frameObj["tweenEasing"].ToString() != "null") { frameData.tweenEasing = frameObj["tweenEasing"].AsFloat; } if (frameObj.ContainKey("curve")) { SimpleJSON.JSONArray curves = frameObj["curve"].AsArray; frameData.curve = new float[4] { curves[0].AsFloat, curves[1].AsFloat, curves[2].AsFloat, curves[3].AsFloat }; } if (frameObj.ContainKey("transform")) { SimpleJSON.JSONClass transformObj = frameObj["transform"].AsObject; DragonBoneData.TransformData transData = new DragonBoneData.TransformData(); if (transformObj.ContainKey("x")) { transData.x = transformObj["x"].AsFloat * 0.01f; } if (transformObj.ContainKey("y")) { transData.y = -transformObj["y"].AsFloat * 0.01f; } if (transformObj.ContainKey("skX")) { transData.rotate = -transformObj["skX"].AsFloat; } if (transformObj.ContainKey("scX")) { transData.scx = transformObj["scX"].AsFloat; } if (transformObj.ContainKey("scY")) { transData.scy = transformObj["scY"].AsFloat; } frameData.transformData = transData; } if (frameObj.ContainKey("color")) { SimpleJSON.JSONClass colorObj = frameObj["color"].AsObject; DragonBoneData.ColorData colorData = new DragonBoneData.ColorData(); if (colorObj.ContainKey("aM")) { colorData.aM = colorObj["aM"].AsFloat * 0.01f; } if (colorObj.ContainKey("a0")) { colorData.aM += colorObj["a0"].AsFloat / 255f; } if (colorObj.ContainKey("rM")) { colorData.rM = colorObj["rM"].AsFloat * 0.01f; } if (colorObj.ContainKey("r0")) { colorData.rM += colorObj["r0"].AsFloat / 255f; } if (colorObj.ContainKey("gM")) { colorData.gM = colorObj["gM"].AsFloat * 0.01f; } if (colorObj.ContainKey("g0")) { colorData.gM += colorObj["g0"].AsFloat / 255f; } if (colorObj.ContainKey("bM")) { colorData.bM = colorObj["bM"].AsFloat * 0.01f; } if (colorObj.ContainKey("b0")) { colorData.bM += colorObj["b0"].AsFloat / 255f; } frameData.color = colorData; } //ffd animation //vertex offset bool startFromY = false; if (frameObj.ContainKey("offset")) { startFromY = frameObj["offset"].AsInt % 2 != 0; //从Y开始 frameData.offset = frameObj["offset"].AsInt / 2; } if (frameObj.ContainKey("vertices")) //local vertex { SimpleJSON.JSONArray verticesObj = frameObj["vertices"].AsArray; int index = 0; int k = 0; if (startFromY) { frameData.vertices = new Vector2[verticesObj.Count / 2 + 1]; frameData.vertices[index] = new Vector2(0, -verticesObj[k].AsFloat * 0.01f); k = 1; ++index; } else { frameData.vertices = new Vector2[verticesObj.Count / 2]; } for (; k < verticesObj.Count && k + 1 < verticesObj.Count; k += 2) { frameData.vertices[index] = new Vector2(verticesObj[k].AsFloat * 0.01f, -verticesObj[k + 1].AsFloat * 0.01f); ++index; } armatureEditor.ffdKV[subData.name] = true; } subData.frameDatas[j] = frameData; } } animDatas[i] = subData; } }