Exemplo n.º 1
0
        private void ImportSprites(AssetImportContext ctx, AseFile aseFile, SpriteImportData[] spriteImportData)
        {
            int spriteCount = spriteImportData.Length;

            Sprite[] sprites = new Sprite[spriteCount];

            for (int i = 0; i < spriteCount; i++)
            {
                Sprite sprite = Sprite.Create(atlas,
                                              spriteImportData[i].rect,
                                              spriteImportData[i].pivot, textureSettings.pixelsPerUnit, textureSettings.extrudeEdges,
                                              textureSettings.meshType, spriteImportData[i].border, textureSettings.generatePhysics);

                sprite.name = string.Format("{0}_{1}", name, spriteImportData[i].name);

                ctx.AddObjectToAsset(sprite.name, sprite);

                //AssetDatabase.CreateAsset(sprite, GetFolderPath(ctx.assetPath) + "/" + sprite.name + ".asset");
                //AssetDatabase.Refresh();

                sprites[i] = sprite;
            }

            //如果有Tag
            if (aseFile.GetAnimations().Length > 0)
            {
                GenerateAnimations(ctx, aseFile, sprites);
            }
            else
            {
                GenerateAnimation(ctx, aseFile, sprites);
            }
        }
Exemplo n.º 2
0
        private void GenerateAseAnimations(UnityEditor.AssetImporters.AssetImportContext ctx, AseFile aseFile, Sprite[] sprites)
        {
            if (animationSettings == null)
            {
                animationSettings = new AseFileAnimationSettings[0];
            }

            var animSettings = new List <AseFileAnimationSettings>(animationSettings);
            var animations   = aseFile.GetAnimations();

            if (animations.Length <= 0)
            {
                return;
            }

            if (animationSettings != null)
            {
                RemoveUnusedAnimationSettings(animSettings, animations);
            }

            foreach (var animation in animations)
            {
                // AseAnim
                AseAnimation aseAnim = new AseAnimation();
                aseAnim.name = aseAnim.Name = animation.TagName;

                AseFileAnimationSettings importSettings = GetAnimationSettingFor(animSettings, animation);
                importSettings.about = GetAnimationAbout(animation);

                int length = animation.FrameTo - animation.FrameFrom + 1;
                aseAnim.Points = new AseAnimation.KeyPoint[length];

                aseAnim.LoopType = (AseAnimation.LoopAnimation)animation.Animation;
                aseAnim.Loop     = length > 1;

                float time = 0;

                int from = (animation.Animation != LoopAnimation.Reverse) ? animation.FrameFrom : animation.FrameTo;
                int step = (animation.Animation != LoopAnimation.Reverse) ? 1 : -1;

                int keyIndex = from;

                for (int i = 0; i < length; i++)
                {
                    if (i >= length)
                    {
                        keyIndex = from;
                    }

                    aseAnim.Points[i] = new AseAnimation.KeyPoint(sprites[keyIndex], aseFile.Frames[keyIndex].FrameDuration / 1000f);
                    time += aseFile.Frames[keyIndex].FrameDuration / 1000f;

                    keyIndex += step;
                }

                ctx.AddObjectToAsset(aseAnim.Name, aseAnim);
            }

            animationSettings = animSettings.ToArray();
        }
Exemplo n.º 3
0
    //生成动画组
    void GenerateAnimations(AssetImportContext ctx, AseFile aseFile, AseFrame[] frames)
    {
        FrameTag[] tags = aseFile.GetAnimations();
        List <AseAnimationSettings> settingList;

        //如果当前没有保存的动画设置
        if (animationSettings == null || animationSettings.Length == 0)
        {
            settingList = new List <AseAnimationSettings>();
        }
        //已有保存的动画设置
        else
        {
            //去除其中已失效的动画
            RemoveUnusedAnimationSettings(tags);

            settingList = new List <AseAnimationSettings>(animationSettings);
        }

        //如果一个Tag都没有
        if (tags.Length == 0)
        {
            //直接以文件命名
            //生成动画
            AseAnimationSettings animationSetting = GenerateAnimation(ctx, name, frames);
            if (!settingList.Contains(animationSetting))
            {
                settingList.Add(animationSetting);
            }
        }
        //有Tag
        else
        {
            for (int i = 0; i < tags.Length; i++)
            {
                FrameTag tag = tags[i];

                string animationName = GetAnimationName(tag.TagName);

                //生成动画
                AseAnimationSettings animationSetting = GenerateAnimation(ctx, animationName, frames, tag.FrameFrom, tag.FrameTo);

                if (!settingList.Contains(animationSetting))
                {
                    settingList.Add(animationSetting);
                }
            }
        }

        animationSettings = settingList.ToArray();
    }
        public AseFileAnimationSettings[] GetAnimationImportSettings()
        {
            List <AseFileAnimationSettings> animationSettings = new List <AseFileAnimationSettings>();

            FrameTag[] frameTags = aseFile.GetAnimations();

            foreach (var frameTag in frameTags)
            {
                int frames = frameTag.FrameTo - frameTag.FrameFrom + 1;

                AseFileAnimationSettings setting = new AseFileAnimationSettings(frameTag.TagName)
                {
                    about        = GetAnimationAbout(frameTag),
                    loopTime     = true,
                    sprites      = new Sprite[frames],
                    frameNumbers = new int[frames]
                };

                int frameFrom = frameTag.FrameFrom;
                int frameTo   = frameTag.FrameTo;
                int step      = (frameTag.Animation != LoopAnimation.Reverse) ? 1 : -1;

                int frameIndex = frameFrom;
                int i          = 0;
                while (frameIndex != frameTo)
                {
                    setting.frameNumbers[i] = frameIndex;
                    frameIndex += step;
                    ++i;
                }

                setting.frameNumbers[i] = frameTo;

                animationSettings.Add(setting);
            }

            return(animationSettings.ToArray());
        }
Exemplo n.º 5
0
    //生成贴图文件
    void GenerateSprites(AssetImportContext ctx, AseFile aseFile, SpriteImportData[] spriteImportData)
    {
        int spriteCount = spriteImportData.Length;

        AseFrame[] frames = new AseFrame[spriteImportData.Length];

        FrameTag[] tags = aseFile.GetAnimations();

        for (int i = 0; i < spriteCount; i++)
        {
            Sprite sprite = Sprite.Create(atlas, spriteImportData[i].rect, spriteImportData[i].pivot,
                                          textureSettings.pixelsPerUnit, 1, SpriteMeshType.Tight, spriteImportData[i].border);

            sprite.name = string.Format("{0}_{1}", name, i);

            ctx.AddObjectToAsset(sprite.name, sprite);

            frames[i] = new AseFrame(sprite, aseFile.Frames[i].FrameDuration / 1000f);
        }

        //生成动画文件
        GenerateAnimations(ctx, aseFile, frames);
    }
        private List <AnimationClip> GenerateAnimations(AseFile aseFile, List <Sprite> sprites)
        {
            List <AnimationClip> res = new List <AnimationClip>();
            var animations           = aseFile.GetAnimations();

            if (animations.Length <= 0)
            {
                return(res);
            }

            var metadatas = aseFile.GetMetaData(Settings.spritePivot, Settings.pixelsPerUnit);

            int index = 0;

            foreach (var animation in animations)
            {
                var           path = directoryName + "/" + fileName + "_" + animation.TagName + ".anim";
                AnimationClip clip = AssetDatabase.LoadAssetAtPath <AnimationClip>(path);
                if (clip == null)
                {
                    clip = new AnimationClip();
                    AssetDatabase.CreateAsset(clip, path);
                    clip.wrapMode = GetDefaultWrapMode(animation.TagName);
                }
                else
                {
                    AnimationClipSettings animSettings = AnimationUtility.GetAnimationClipSettings(clip);
                    clip.wrapMode = animSettings.loopTime ? WrapMode.Loop : WrapMode.Once;
                }

                clip.name      = fileName + "_" + animation.TagName;
                clip.frameRate = 25;

                EditorCurveBinding editorBinding = new EditorCurveBinding();
                editorBinding.path         = "";
                editorBinding.propertyName = "m_Sprite";

                switch (Settings.bindType)
                {
                case AseAnimationBindType.SpriteRenderer:
                    editorBinding.type = typeof(SpriteRenderer);
                    break;

                case AseAnimationBindType.UIImage:
                    editorBinding.type = typeof(Image);
                    break;
                }

                // plus last frame to keep the duration
                int length = animation.FrameTo - animation.FrameFrom + 1;
                ObjectReferenceKeyframe[]           spriteKeyFrames = new ObjectReferenceKeyframe[length + 1];
                Dictionary <string, AnimationCurve> transformCurveX = new Dictionary <string, AnimationCurve>(),
                                                    transformCurveY = new Dictionary <string, AnimationCurve>();

                float time = 0;
                int   from = (animation.Animation != LoopAnimation.Reverse) ? animation.FrameFrom : animation.FrameTo;
                int   step = (animation.Animation != LoopAnimation.Reverse) ? 1 : -1;

                int keyIndex = from;
                for (int i = 0; i < length; i++)
                {
                    if (i >= length)
                    {
                        keyIndex = from;
                    }

                    ObjectReferenceKeyframe frame = new ObjectReferenceKeyframe();
                    frame.time  = time;
                    frame.value = sprites[keyIndex];

                    time += aseFile.Frames[keyIndex].FrameDuration / 1000f;
                    spriteKeyFrames[i] = frame;

                    foreach (var metadata in metadatas)
                    {
                        if (metadata.Type == MetaDataType.TRANSFORM && metadata.Transforms.ContainsKey(keyIndex))
                        {
                            var childTransform = metadata.Args[0];
                            if (!transformCurveX.ContainsKey(childTransform))
                            {
                                transformCurveX[childTransform] = new AnimationCurve();
                                transformCurveY[childTransform] = new AnimationCurve();
                            }
                            var pos = metadata.Transforms[keyIndex];
                            transformCurveX[childTransform].AddKey(i, pos.x);
                            transformCurveY[childTransform].AddKey(i, pos.y);
                        }
                    }

                    keyIndex += step;
                }

                float frameTime = 1f / clip.frameRate;
                ObjectReferenceKeyframe lastFrame = new ObjectReferenceKeyframe();
                lastFrame.time  = time - frameTime;
                lastFrame.value = sprites[keyIndex - step];

                spriteKeyFrames[spriteKeyFrames.Length - 1] = lastFrame;
                foreach (var metadata in metadatas)
                {
                    if (metadata.Type == MetaDataType.TRANSFORM && metadata.Transforms.ContainsKey(keyIndex - step))
                    {
                        var childTransform = metadata.Args[0];
                        var pos            = metadata.Transforms[keyIndex - step];
                        transformCurveX[childTransform].AddKey(spriteKeyFrames.Length - 1, pos.x);
                        transformCurveY[childTransform].AddKey(spriteKeyFrames.Length - 1, pos.y);
                    }
                }

                AnimationUtility.SetObjectReferenceCurve(clip, editorBinding, spriteKeyFrames);
                foreach (var childTransform in transformCurveX.Keys)
                {
                    EditorCurveBinding
                             bindingX = new EditorCurveBinding {
                        path         = childTransform,
                        type         = typeof(Transform),
                        propertyName = "m_LocalPosition.x"
                    },
                             bindingY = new EditorCurveBinding {
                        path         = childTransform,
                        type         = typeof(Transform),
                        propertyName = "m_LocalPosition.y"
                    };
                    MakeConstant(transformCurveX[childTransform]);
                    AnimationUtility.SetEditorCurve(clip, bindingX, transformCurveX[childTransform]);
                    MakeConstant(transformCurveY[childTransform]);
                    AnimationUtility.SetEditorCurve(clip, bindingY, transformCurveY[childTransform]);
                }

                AnimationClipSettings clipSettings = AnimationUtility.GetAnimationClipSettings(clip);
                clipSettings.loopTime = (clip.wrapMode == WrapMode.Loop);

                AnimationUtility.SetAnimationClipSettings(clip, clipSettings);
                EditorUtility.SetDirty(clip);
                index++;
                res.Add(clip);
            }

            return(res);
        }
Exemplo n.º 7
0
        private void GenerateAnimations(AssetImportContext ctx, AseFile aseFile, Sprite[] sprites)
        {
            if (animationSettings == null)
            {
                animationSettings = new AseFileAnimationSettings[0];
            }

            var animSettings = new List <AseFileAnimationSettings>(animationSettings);
            var animations   = aseFile.GetAnimations();

            if (animations.Length <= 0)
            {
                return;
            }

            if (animationSettings != null)
            {
                RemoveUnusedAnimationSettings(animSettings, animations);
            }

            int index = 0;

            foreach (var animation in animations)
            {
                AnimationClip animationClip = new AnimationClip();
                animationClip.name      = name + "_" + animation.TagName;
                animationClip.frameRate = 25;

                AseFileAnimationSettings importSettings = GetAnimationSettingFor(animSettings, animation);
                importSettings.about = GetAnimationAbout(animation);


                EditorCurveBinding spriteBinding = new EditorCurveBinding();
                spriteBinding.type         = typeof(SpriteRenderer);
                spriteBinding.path         = "";
                spriteBinding.propertyName = "m_Sprite";


                int length = animation.FrameTo - animation.FrameFrom + 1;
                ObjectReferenceKeyframe[] spriteKeyFrames = new ObjectReferenceKeyframe[length + 1]; // plus last frame to keep the duration

                float time = 0;

                int from = (animation.Animation != LoopAnimation.Reverse) ? animation.FrameFrom : animation.FrameTo;
                int step = (animation.Animation != LoopAnimation.Reverse) ? 1 : -1;

                int keyIndex = from;

                for (int i = 0; i < length; i++)
                {
                    if (i >= length)
                    {
                        keyIndex = from;
                    }


                    ObjectReferenceKeyframe frame = new ObjectReferenceKeyframe();
                    frame.time  = time;
                    frame.value = sprites[keyIndex];

                    time += aseFile.Frames[keyIndex].FrameDuration / 1000f;

                    keyIndex          += step;
                    spriteKeyFrames[i] = frame;
                }

                float frameTime = 1f / animationClip.frameRate;

                ObjectReferenceKeyframe lastFrame = new ObjectReferenceKeyframe();
                lastFrame.time  = time - frameTime;
                lastFrame.value = sprites[keyIndex - step];

                spriteKeyFrames[spriteKeyFrames.Length - 1] = lastFrame;


                AnimationUtility.SetObjectReferenceCurve(animationClip, spriteBinding, spriteKeyFrames);
                AnimationClipSettings settings = AnimationUtility.GetAnimationClipSettings(animationClip);

                switch (animation.Animation)
                {
                case LoopAnimation.Forward:
                    animationClip.wrapMode = WrapMode.Loop;
                    settings.loopTime      = true;
                    break;

                case LoopAnimation.Reverse:
                    animationClip.wrapMode = WrapMode.Loop;
                    settings.loopTime      = true;
                    break;

                case LoopAnimation.PingPong:
                    animationClip.wrapMode = WrapMode.PingPong;
                    settings.loopTime      = true;
                    break;
                }

                if (!importSettings.loopTime)
                {
                    animationClip.wrapMode = WrapMode.Once;
                    settings.loopTime      = false;
                }

                AnimationUtility.SetAnimationClipSettings(animationClip, settings);
                ctx.AddObjectToAsset(animation.TagName, animationClip);

                index++;
            }

            animationSettings = animSettings.ToArray();
        }
Exemplo n.º 8
0
        private void GenerateAnimations(AssetImportContext ctx, AseFile aseFile, Sprite[] sprites, MetaData[] metadatas)
        {
            if (animationSettings == null)
            {
                animationSettings = new AseFileAnimationSettings[0];
            }

            var animSettings = new List <AseFileAnimationSettings>(animationSettings);
            var animations   = aseFile.GetAnimations();

            if (animations.Length <= 0)
            {
                return;
            }


            if (animationSettings != null)
            {
                RemoveUnusedAnimationSettings(animSettings, animations);
            }

            int index = 0;

            foreach (var animation in animations)
            {
                AnimationClip animationClip = new AnimationClip();
                animationClip.name      = name + "_" + animation.TagName;
                animationClip.frameRate = 25;

                AseFileAnimationSettings importSettings = GetAnimationSettingFor(animSettings, animation);
                importSettings.about = GetAnimationAbout(animation);

                EditorCurveBinding editorBinding = new EditorCurveBinding();
                editorBinding.path         = "";
                editorBinding.propertyName = "m_Sprite";

                switch (bindType)
                {
                case AseEditorBindType.SpriteRenderer:
                    editorBinding.type = typeof(SpriteRenderer);
                    break;

                case AseEditorBindType.UIImage:
                    editorBinding.type = typeof(Image);
                    break;
                }


                int length = animation.FrameTo - animation.FrameFrom + 1;
                ObjectReferenceKeyframe[]           spriteKeyFrames = new ObjectReferenceKeyframe[length + 1]; // plus last frame to keep the duration
                Dictionary <string, AnimationCurve> transformCurveX = new Dictionary <string, AnimationCurve>(),
                                                    transformCurveY = new Dictionary <string, AnimationCurve>();

                float time = 0;

                int from = (animation.Animation != LoopAnimation.Reverse) ? animation.FrameFrom : animation.FrameTo;
                int step = (animation.Animation != LoopAnimation.Reverse) ? 1 : -1;

                int keyIndex = from;

                for (int i = 0; i < length; i++)
                {
                    if (i >= length)
                    {
                        keyIndex = from;
                    }


                    ObjectReferenceKeyframe frame = new ObjectReferenceKeyframe();
                    frame.time  = time;
                    frame.value = sprites[keyIndex];

                    time += aseFile.Frames[keyIndex].FrameDuration / 1000f;
                    spriteKeyFrames[i] = frame;

                    foreach (var metadata in metadatas)
                    {
                        if (metadata.Type == MetaDataType.TRANSFORM && metadata.Transforms.ContainsKey(keyIndex))
                        {
                            var childTransform = metadata.Args[0];
                            if (!transformCurveX.ContainsKey(childTransform))
                            {
                                transformCurveX[childTransform] = new AnimationCurve();
                                transformCurveY[childTransform] = new AnimationCurve();
                            }
                            var pos = metadata.Transforms[keyIndex];
                            transformCurveX[childTransform].AddKey(i, pos.x);
                            transformCurveY[childTransform].AddKey(i, pos.y);
                        }
                    }

                    keyIndex += step;
                }

                float frameTime = 1f / animationClip.frameRate;

                ObjectReferenceKeyframe lastFrame = new ObjectReferenceKeyframe();
                lastFrame.time  = time - frameTime;
                lastFrame.value = sprites[keyIndex - step];

                spriteKeyFrames[spriteKeyFrames.Length - 1] = lastFrame;
                foreach (var metadata in metadatas)
                {
                    if (metadata.Type == MetaDataType.TRANSFORM && metadata.Transforms.ContainsKey(keyIndex - step))
                    {
                        var childTransform = metadata.Args[0];
                        var pos            = metadata.Transforms[keyIndex - step];
                        transformCurveX[childTransform].AddKey(spriteKeyFrames.Length - 1, pos.x);
                        transformCurveY[childTransform].AddKey(spriteKeyFrames.Length - 1, pos.y);
                    }
                }

                AnimationUtility.SetObjectReferenceCurve(animationClip, editorBinding, spriteKeyFrames);

                foreach (var childTransform in transformCurveX.Keys)
                {
                    EditorCurveBinding
                             bindingX = new EditorCurveBinding {
                        path = childTransform, type = typeof(Transform), propertyName = "m_LocalPosition.x"
                    },
                             bindingY = new EditorCurveBinding {
                        path = childTransform, type = typeof(Transform), propertyName = "m_LocalPosition.y"
                    };
                    MakeConstant(transformCurveX[childTransform]);
                    AnimationUtility.SetEditorCurve(animationClip, bindingX, transformCurveX[childTransform]);
                    MakeConstant(transformCurveY[childTransform]);
                    AnimationUtility.SetEditorCurve(animationClip, bindingY, transformCurveY[childTransform]);
                }

                AnimationClipSettings settings = AnimationUtility.GetAnimationClipSettings(animationClip);

                switch (animation.Animation)
                {
                case LoopAnimation.Forward:
                    animationClip.wrapMode = WrapMode.Loop;
                    settings.loopTime      = true;
                    break;

                case LoopAnimation.Reverse:
                    animationClip.wrapMode = WrapMode.Loop;
                    settings.loopTime      = true;
                    break;

                case LoopAnimation.PingPong:
                    animationClip.wrapMode = WrapMode.PingPong;
                    settings.loopTime      = true;
                    break;
                }

                if (!importSettings.loopTime)
                {
                    animationClip.wrapMode = WrapMode.Once;
                    settings.loopTime      = false;
                }

                AnimationUtility.SetAnimationClipSettings(animationClip, settings);
                ctx.AddObjectToAsset(animation.TagName, animationClip);

                index++;
            }

            animationSettings = animSettings.ToArray();
        }