Exemple #1
0
        public ImportedAnimationSheet ImportSpritesAndAnimationSheet(string assetPath, string additionalCommandLineArguments = null)
        {
            if (assetPath == null)
            {
                return(null);
            }

            if (sharedData == null)
            {
                LoadUserConfig();
            }

            string fileName  = Path.GetFileName(assetPath);
            string assetName = Path.GetFileNameWithoutExtension(fileName);
            string basePath  = GetBasePath(assetPath);

            // we analyze import settings on existing files
            PreviousImportSettings previousAnimationInfo = CollectPreviousImportSettings(basePath, assetName);

            if (AsepriteImporter.CreateSpriteAtlasAndMetaFile(_asepritePath, additionalCommandLineArguments, basePath, fileName, assetName, _sharedData.saveSpritesToSubfolder))
            {
                AssetDatabase.Refresh();
                return(ImportJSONAndCreateSprites(basePath, assetName, previousAnimationInfo));
            }

            return(null);
        }
        private PreviousImportSettings CollectPreviousImportSettings(AnimationImportJob importJob)
        {
            PreviousImportSettings previousImportSettings = new PreviousImportSettings();

            previousImportSettings.GetTextureImportSettings(importJob.imageAssetFilename);

            return(previousImportSettings);
        }
Exemple #3
0
        private PreviousImportSettings CollectPreviousImportSettings(string basePath, string name)
        {
            PreviousImportSettings previousImportSettings = new PreviousImportSettings();

            previousImportSettings.GetTextureImportSettings(GetImageAssetFilename(basePath, name));

            return(previousImportSettings);
        }
Exemple #4
0
        public ImportedAnimationInfo CreateAnimationsForAssetFile(string assetPath)
        {
            if (!IsValidAsset(assetPath))
            {
                return(null);
            }

            string fileName  = Path.GetFileName(assetPath);
            string assetName = Path.GetFileNameWithoutExtension(fileName);
            string basePath  = GetBasePath(assetPath);

            // we analyze import settings on existing files
            PreviousImportSettings previousAnimationInfo = CollectPreviousImportSettings(basePath, assetName);

            if (AsepriteImporter.CreateSpriteAtlasAndMetaFile(_asepritePath, basePath, fileName, assetName, _sharedData.saveSpritesToSubfolder))
            {
                AssetDatabase.Refresh();
                return(ImportJSONAndCreateAnimations(basePath, assetName, previousAnimationInfo));
            }

            return(null);
        }
Exemple #5
0
        private ImportedAnimationInfo ImportJSONAndCreateAnimations(string basePath, string name, PreviousImportSettings previousImportSettings)
        {
            string    imageAssetFilename = GetImageAssetFilename(basePath, name);
            string    textAssetFilename  = GetJSONAssetFilename(basePath, name);
            TextAsset textAsset          = AssetDatabase.LoadAssetAtPath <TextAsset>(textAssetFilename);

            if (textAsset != null)
            {
                // parse the JSON file
                JSONObject            jsonObject    = JSONObject.Parse(textAsset.ToString());
                ImportedAnimationInfo animationInfo = AsepriteImporter.GetAnimationInfo(jsonObject);

                if (animationInfo == null)
                {
                    return(null);
                }

                animationInfo.previousImportSettings = previousImportSettings;

                animationInfo.basePath             = basePath;
                animationInfo.name                 = name;
                animationInfo.nonLoopingAnimations = sharedData.animationNamesThatDoNotLoop;

                // delete JSON file afterwards
                AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(textAsset));

                CreateSprites(animationInfo, imageAssetFilename);

                CreateAnimations(animationInfo, imageAssetFilename);

                return(animationInfo);
            }
            else
            {
                Debug.LogWarning("Problem with JSON file: " + textAssetFilename);
            }

            return(null);
        }
        public void CreateAnimation(ImportedAnimation anim, string basePath, string masterName, AnimationTargetObjectType targetType)
        {
            AnimationClip clip;
            // attempt to ignore characters in master name after _ character
            string newName = masterName.Split('_')[0];

            ///NOTE - DDobyns experiment to fix animation naming issues
            ///string fileName = basePath + "/" + masterName + "_" + anim.name + ".anim";   // original behavior is this line
            //string fileName = basePath + "/" + newName + "_" + anim.name + ".anim";      // DDobyns original fix is this line
            string fileName = basePath + "/" + anim.name + ".anim";      // DDobyns Feb 26 2020 fix is this line
            // this results in naming animations with the Action and Angle only. e.g., Character_run45 will save a clip named run45
            bool isLooping = anim.isLooping;

            // check if animation file already exists
            clip = AssetDatabase.LoadAssetAtPath <AnimationClip>(fileName);
            if (clip != null)
            {
                // get previous animation settings
                targetType = PreviousImportSettings.GetAnimationTargetFromExistingClip(clip);
            }
            else
            {
                clip = new AnimationClip();
                AssetDatabase.CreateAsset(clip, fileName);
            }

            // change loop settings
            if (isLooping)
            {
                clip.wrapMode = WrapMode.Loop;
                clip.SetLoop(true);
            }
            else
            {
                clip.wrapMode = WrapMode.Clamp;
                clip.SetLoop(false);
            }

            ObjectReferenceKeyframe[] keyFrames = new ObjectReferenceKeyframe[anim.Count + 1];             // one more than sprites because we repeat the last sprite

            for (int i = 0; i < anim.Count; i++)
            {
                ObjectReferenceKeyframe keyFrame = new ObjectReferenceKeyframe {
                    time = anim.GetKeyFrameTime(i)
                };

                Sprite sprite = anim.frames[i].sprite;
                keyFrame.value = sprite;
                keyFrames[i]   = keyFrame;
            }

            // repeating the last frame at a point "just before the end" so the animation gets its correct length

            ObjectReferenceKeyframe lastKeyFrame = new ObjectReferenceKeyframe {
                time = anim.GetLastKeyFrameTime(clip.frameRate)
            };

            Sprite lastSprite = anim.frames[anim.Count - 1].sprite;

            lastKeyFrame.value    = lastSprite;
            keyFrames[anim.Count] = lastKeyFrame;

            // save curve into clip, either for SpriteRenderer, Image, or both
            if (targetType == AnimationTargetObjectType.SpriteRenderer)
            {
                AnimationUtility.SetObjectReferenceCurve(clip, AnimationClipUtility.spriteRendererCurveBinding, keyFrames);
                AnimationUtility.SetObjectReferenceCurve(clip, AnimationClipUtility.imageCurveBinding, null);
            }
            else if (targetType == AnimationTargetObjectType.Image)
            {
                AnimationUtility.SetObjectReferenceCurve(clip, AnimationClipUtility.spriteRendererCurveBinding, null);
                AnimationUtility.SetObjectReferenceCurve(clip, AnimationClipUtility.imageCurveBinding, keyFrames);
            }
            else if (targetType == AnimationTargetObjectType.SpriteRendererAndImage)
            {
                AnimationUtility.SetObjectReferenceCurve(clip, AnimationClipUtility.spriteRendererCurveBinding, keyFrames);
                AnimationUtility.SetObjectReferenceCurve(clip, AnimationClipUtility.imageCurveBinding, keyFrames);
            }

            EditorUtility.SetDirty(clip);
            anim.animationClip = clip;
        }
        public void CreateAnimation(ImportedAnimation anim, string basePath, string masterName, AnimationTargetObjectType targetType)
        {
            AnimationClip clip;
            string        fileName  = basePath + "/" + masterName + "_" + anim.name + ".anim";
            bool          isLooping = anim.isLooping;

            // check if animation file already exists
            clip = AssetDatabase.LoadAssetAtPath <AnimationClip>(fileName);
            if (clip != null)
            {
                // get previous animation settings
                targetType = PreviousImportSettings.GetAnimationTargetFromExistingClip(clip);
                isLooping  = clip.isLooping;
            }
            else
            {
                clip = new AnimationClip();
                AssetDatabase.CreateAsset(clip, fileName);
            }

            // change loop settings
            if (isLooping)
            {
                clip.wrapMode = WrapMode.Loop;
                clip.SetLoop(true);
            }
            else
            {
                clip.wrapMode = WrapMode.Clamp;
                clip.SetLoop(false);
            }

            ObjectReferenceKeyframe[] keyFrames = new ObjectReferenceKeyframe[anim.Count + 1];             // one more than sprites because we repeat the last sprite

            for (int i = 0; i < anim.Count; i++)
            {
                ObjectReferenceKeyframe keyFrame = new ObjectReferenceKeyframe {
                    time = anim.GetKeyFrameTime(i)
                };

                Sprite sprite = anim.frames[i].sprite;
                keyFrame.value = sprite;
                keyFrames[i]   = keyFrame;
            }

            // repeating the last frame at a point "just before the end" so the animation gets its correct length

            ObjectReferenceKeyframe lastKeyFrame = new ObjectReferenceKeyframe {
                time = anim.GetLastKeyFrameTime(clip.frameRate)
            };

            Sprite lastSprite = anim.frames[anim.Count - 1].sprite;

            lastKeyFrame.value    = lastSprite;
            keyFrames[anim.Count] = lastKeyFrame;

            // save curve into clip, either for SpriteRenderer, Image, or both
            if (targetType == AnimationTargetObjectType.SpriteRenderer)
            {
                AnimationUtility.SetObjectReferenceCurve(clip, AnimationClipUtility.spriteRendererCurveBinding, keyFrames);
                AnimationUtility.SetObjectReferenceCurve(clip, AnimationClipUtility.imageCurveBinding, null);
            }
            else if (targetType == AnimationTargetObjectType.Image)
            {
                AnimationUtility.SetObjectReferenceCurve(clip, AnimationClipUtility.spriteRendererCurveBinding, null);
                AnimationUtility.SetObjectReferenceCurve(clip, AnimationClipUtility.imageCurveBinding, keyFrames);
            }
            else if (targetType == AnimationTargetObjectType.SpriteRendererAndImage)
            {
                AnimationUtility.SetObjectReferenceCurve(clip, AnimationClipUtility.spriteRendererCurveBinding, keyFrames);
                AnimationUtility.SetObjectReferenceCurve(clip, AnimationClipUtility.imageCurveBinding, keyFrames);
            }

            EditorUtility.SetDirty(clip);
            anim.animationClip = clip;
        }
        public void CreateAnimation(ImportedAnimation anim, string basePath, string masterName, AnimationTargetObjectType targetType)
        {
            AnimationClip clip;
            string        fileName  = basePath + "/" + masterName + "_" + anim.name + ".anim";
            bool          isLooping = anim.isLooping;

            // check if animation file already exists
            clip = AssetDatabase.LoadAssetAtPath <AnimationClip>(fileName);
            if (clip != null)
            {
                // get previous animation settings
                targetType = PreviousImportSettings.GetAnimationTargetFromExistingClip(clip);
            }
            else
            {
                clip = new AnimationClip();
                AssetDatabase.CreateAsset(clip, fileName);
            }

            // change loop settings
            if (isLooping)
            {
                clip.wrapMode = WrapMode.Loop;
                clip.SetLoop(true);
            }
            else
            {
                clip.wrapMode = WrapMode.Clamp;
                clip.SetLoop(false);
            }

            // convert keyframes
            ImportedAnimationFrame[]  srcKeyframes = anim.ListFramesAccountingForPlaybackDirection().ToArray();
            ObjectReferenceKeyframe[] keyFrames    = new ObjectReferenceKeyframe[srcKeyframes.Length + 1];
            float timeOffset = 0f;

            for (int i = 0; i < srcKeyframes.Length; i++)
            {
                // first sprite will be set at the beginning (t=0) of the animation
                keyFrames[i] = new ObjectReferenceKeyframe
                {
                    time  = timeOffset,
                    value = srcKeyframes[i].sprite
                };

                // add duration of frame in seconds
                timeOffset += srcKeyframes[i].duration / 1000f;
            }

            // repeating the last frame at a point "just before the end" so the animation gets its correct length
            keyFrames[srcKeyframes.Length] = new ObjectReferenceKeyframe
            {
                time  = timeOffset - (1f / clip.frameRate),                // substract the duration of one frame
                value = srcKeyframes.Last().sprite
            };

            // save curve into clip, either for SpriteRenderer, Image, or both
            if (targetType == AnimationTargetObjectType.SpriteRenderer)
            {
                AnimationUtility.SetObjectReferenceCurve(clip, AnimationClipUtility.spriteRendererCurveBinding, keyFrames);
                AnimationUtility.SetObjectReferenceCurve(clip, AnimationClipUtility.imageCurveBinding, null);
            }
            else if (targetType == AnimationTargetObjectType.Image)
            {
                AnimationUtility.SetObjectReferenceCurve(clip, AnimationClipUtility.spriteRendererCurveBinding, null);
                AnimationUtility.SetObjectReferenceCurve(clip, AnimationClipUtility.imageCurveBinding, keyFrames);
            }
            else if (targetType == AnimationTargetObjectType.SpriteRendererAndImage)
            {
                AnimationUtility.SetObjectReferenceCurve(clip, AnimationClipUtility.spriteRendererCurveBinding, keyFrames);
                AnimationUtility.SetObjectReferenceCurve(clip, AnimationClipUtility.imageCurveBinding, keyFrames);
            }

            EditorUtility.SetDirty(clip);
            anim.animationClip = clip;
        }
Exemple #9
0
        private ImportedAnimationSheet ImportJSONAndCreateSprites(string basePath, string name, PreviousImportSettings previousImportSettings)
        {
            // parse the JSON file
            ImportedAnimationSheet animationSheet = CreateAnimationSheetFromJSON(basePath, name, previousImportSettings);

            CreateSprites(animationSheet);

            return(animationSheet);
        }