private void UpdateAnimationEvent() { AnimationInfo info = GetCurrentAnimationInfo(); if (info == null) { return; } if (info.eventList.Count == 0) { return; } if (aniEvent == null) { float time = curFrame / info.fps; for (int i = eventIndex >= 0 ? eventIndex : 0; i < info.eventList.Count; ++i) { if (info.eventList[i].time > time) { aniEvent = info.eventList[i]; eventIndex = i; break; } } } if (aniEvent != null) { float time = curFrame / info.fps; if (aniEvent.time <= time) { gameObject.SendMessage(aniEvent.function, aniEvent); aniEvent = null; } } }
void UpdateState() { if (cureState == null || cureState.motionIndex != aniIndex) { return; } AnimationInfo info = aniInfo[aniIndex]; var ttrans = cureState.CheckTransition(curProcess); if (ttrans != null) { RuntimeAnimatorState tstate = null; if (ttrans.destinationType == AssetTransitions.DestinationType.state) { tstate = GetState(ttrans.destinationHashName, cureState.layerIndex, cureState.machineIndex); } else if (ttrans.destinationType == AssetTransitions.DestinationType.stateMachine) { if (ttrans.destinationIndex == -1) { ttrans.destinationIndex = Layers[cureState.layerIndex].GetMachineIndex(ttrans.destinationHashName); } tstate = GetState(0, cureState.layerIndex, ttrans.destinationIndex); } if (tstate == null) { Pause(); } else { CrossFade(tstate, ttrans.duration); } } }
private void SaveAnimationInfo(string name, AnimationBakeInfo pWorkInfo) { string path = GetFilePath(); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } UnityEditor.Animations.AnimatorController controller = pWorkInfo.animator.runtimeAnimatorController as UnityEditor.Animations.AnimatorController; //AssetDatabase.CreateFolder("Assets/Resources", folderName); FileStream file = File.Open(GetFullPahtFile(name), FileMode.Create); BinaryWriter writer = new BinaryWriter(file); //存储参数 int tpamCount = controller.parameters != null ? controller.parameters.Length : 0; writer.Write(tpamCount); if (controller.parameters != null && controller.parameters.Length > 0) { AssetParameter.Parameter[] tparms = new AssetParameter.Parameter[tpamCount]; for (int i = 0; i < tpamCount; i++) { AssetParameter.Parameter tpamdata = new AssetParameter.Parameter(); tpamdata.CopyData(controller.parameters[i]); tpamdata.WriteToFile(writer); } } writer.Write(layerInfos.Count); foreach (var item in layerInfos) { item.WriteToFile(writer); } //存储animation State writer.Write(aniInfo.Count); foreach (var obj in aniInfo) { AnimationInfo info = (AnimationInfo)obj; //writer.Write(info.animationNameHash); writer.Write(info.animationName); writer.Write(info.animationIndex); writer.Write(info.textureIndex); writer.Write(info.totalFrame); writer.Write(info.fps); writer.Write(info.rootMotion); writer.Write((int)info.wrapMode); if (info.rootMotion) { Debug.Assert(info.totalFrame == info.velocity.Length); for (int i = 0; i != info.velocity.Length; ++i) { writer.Write(info.velocity[i].x); writer.Write(info.velocity[i].y); writer.Write(info.velocity[i].z); writer.Write(info.angularVelocity[i].x); writer.Write(info.angularVelocity[i].y); writer.Write(info.angularVelocity[i].z); } } writer.Write(info.eventList.Count); foreach (var evt in info.eventList) { writer.Write(evt.function); writer.Write(evt.floatParameter); writer.Write(evt.intParameter); writer.Write(evt.stringParameter); writer.Write(evt.time); writer.Write(evt.objectParameter); } } //attach writer.Write(exposeAttachments); if (exposeAttachments) { writer.Write(extraBoneInfo.extraBone.Length); for (int i = 0; i != extraBoneInfo.extraBone.Length; ++i) { writer.Write(extraBoneInfo.extraBone[i]); } for (int i = 0; i != extraBoneInfo.extraBindPose.Length; ++i) { for (int j = 0; j != 16; ++j) { writer.Write(extraBoneInfo.extraBindPose[i][j]); } } } //boneTexture Texture2D[] texture = bakedBoneTexture; writer.Write(texture.Length); writer.Write(textureBlockWidth); writer.Write(textureBlockHeight); for (int i = 0; i != texture.Length; ++i) { byte[] bytes = texture[i].GetRawTextureData(); writer.Write(texture[i].width); writer.Write(texture[i].height); writer.Write(bytes.Length); writer.Write(bytes); } file.Close(); aniInfo.Clear(); }
public void SetupAnimationTexture(ArrayList infoList) { int preNameCode = generateObjectData[0].stateName; //int preTextureIndex = m_bakedTextureIndex; for (int i = 0; i != currentDataIndex; ++i) { GenerateOjbectInfo matrixData = generateObjectData[i]; if (matrixData.boneMatrix == null) { continue; } if (preNameCode != matrixData.stateName) { preNameCode = matrixData.stateName; int totalFrames = currentDataIndex - i; for (int j = i; j != currentDataIndex; ++j) { if (preNameCode != generateObjectData[j].stateName) { totalFrames = j - i; break; } } int width = bakedBoneTexture[bakedTextureIndex].width; int height = bakedBoneTexture[bakedTextureIndex].height; int y = pixely; int currentLineBlockCount = (width - pixelx) / textureBlockWidth % (width / textureBlockWidth); totalFrames -= currentLineBlockCount; if (totalFrames > 0) { int framesEachLine = width / textureBlockWidth; y += (totalFrames / framesEachLine) * textureBlockHeight; y += currentLineBlockCount > 0 ? textureBlockHeight : 0; if (height < y + textureBlockHeight) { ++bakedTextureIndex; pixelx = 0; pixely = 0; Debug.Assert(bakedTextureIndex < bakedBoneTexture.Length); } } foreach (var obj in infoList) { AnimationInfo info = obj as AnimationInfo; if (info.animationNameHash == matrixData.stateName) { info.animationIndex = pixelx / textureBlockWidth + pixely / textureBlockHeight * bakedBoneTexture[bakedTextureIndex].width / textureBlockWidth; info.textureIndex = bakedTextureIndex; } } } if (matrixData.boneMatrix != null) { Debug.Assert(pixely + textureBlockHeight <= bakedBoneTexture[bakedTextureIndex].height); Color[] color = UtilityHelper.Convert2Color(matrixData.boneMatrix); bakedBoneTexture[bakedTextureIndex].SetPixels(pixelx, pixely, textureBlockWidth, textureBlockHeight, color); matrixData.frameIndex = pixelx / textureBlockWidth + pixely / textureBlockHeight * bakedBoneTexture[bakedTextureIndex].width / textureBlockWidth; pixelx += textureBlockWidth; if (pixelx + textureBlockWidth > bakedBoneTexture[bakedTextureIndex].width) { pixelx = 0; pixely += textureBlockHeight; } if (pixely + textureBlockHeight > bakedBoneTexture[bakedTextureIndex].height) { Debug.Assert(generateObjectData[i + 1].stateName != matrixData.stateName); ++bakedTextureIndex; pixelx = 0; pixely = 0; Debug.Assert(bakedTextureIndex < bakedBoneTexture.Length); } } else { Debug.Assert(false); ArrayList list = generateMatrixDataPool[matrixData.stateName]; GenerateOjbectInfo originalData = list[matrixData.boneListIndex] as GenerateOjbectInfo; matrixData.frameIndex = originalData.frameIndex; } } currentDataIndex = 0; }