// ================================================================================
        //  private methods
        // --------------------------------------------------------------------------------

        private void BuildIndex()
        {
            _animationDatabase = new Dictionary <string, AsepriteAnimation>();

            for (int i = 0; i < animations.Count; i++)
            {
                AsepriteAnimation anim = animations[i];
                _animationDatabase[animations[i].name] = anim;
            }
        }
        public AniMetaSpriteButton(string path)
        {
            ani = AsepriteAnimation.New(path, "hovered");
            ani.TryGetCurrentFrameRect("text", out var contentRect);
            var aniRect = new RectangleF(0, 0, ani.Width, ani.Height);

            if (SplitNine(aniRect, contentRect, out nineRectList) == false)
            {
                Log.Warnning("error nine button of " + path);
            }
        }
Beispiel #3
0
    void Start()
    {
        //Load the aseprite animation from JSON
        asepriteAnim = JsonUtility.FromJson <AsepriteAnimation>(SpriteJSON.text);

        //Generate animation objects from tags in aseprite json
        SpriteAnims    = asepriteAnim.meta.frameTags;
        spriteRenderer = GetComponent <SpriteRenderer>();

        //Set current animation to be first animation by default
        PlayAnim(0);
    }
        // ================================================================================
        //  public static methods
        // --------------------------------------------------------------------------------

        public static AsepriteAnimationInfo GetAnimationInfo(JSONObject root)
        {
            AsepriteAnimationInfo importedInfos = new AsepriteAnimationInfo();

            // meta animations

            var meta = root["meta"].Obj;

            var size = meta["size"].Obj;

            importedInfos.width  = (int)size["w"].Number;
            importedInfos.height = (int)size["h"].Number;

            var frameTags = meta["frameTags"].Array;

            foreach (var item in frameTags)
            {
                JSONObject        frameTag = item.Obj;
                AsepriteAnimation anim     = new AsepriteAnimation();
                anim.name = frameTag["name"].Str;
                anim.from = (int)(frameTag["from"].Number);
                anim.to   = (int)(frameTag["to"].Number);

                importedInfos.animations.Add(anim);
            }

            // sprites

            var list = root["frames"].Array;

            foreach (var item in list)
            {
                AsepriteFrame frame = new AsepriteFrame();
                frame.filename = item.Obj["filename"].Str;

                var frameValues = item.Obj["frame"].Obj;
                frame.width  = (int)frameValues["w"].Number;
                frame.height = (int)frameValues["h"].Number;
                frame.x      = (int)frameValues["x"].Number;
                frame.y      = importedInfos.height - (int)frameValues["y"].Number - frame.height;            // unity has a different coord system

                frame.duration = (int)item.Obj["duration"].Number;

                importedInfos.frames.Add(frame);
            }

            importedInfos.CalculateTimings();

            return(importedInfos);
        }
		// ================================================================================
		//  public static methods
		// --------------------------------------------------------------------------------

		public static AsepriteAnimationInfo GetAnimationInfo(JSONObject root)
		{
			AsepriteAnimationInfo importedInfos = new AsepriteAnimationInfo();

			// meta animations

			var meta = root["meta"].Obj;

			var size = meta["size"].Obj;
			importedInfos.width = (int)size["w"].Number;
			importedInfos.height = (int)size["h"].Number;

			var frameTags = meta["frameTags"].Array;
			foreach (var item in frameTags)
			{
				JSONObject frameTag = item.Obj;
				AsepriteAnimation anim = new AsepriteAnimation();
				anim.name = frameTag["name"].Str;
				anim.from = (int)(frameTag["from"].Number);
				anim.to = (int)(frameTag["to"].Number);

				importedInfos.animations.Add(anim);
			}

			// sprites

			var list = root["frames"].Array;
			foreach (var item in list)
			{
				AsepriteFrame frame = new AsepriteFrame();
				frame.filename = item.Obj["filename"].Str;

				var frameValues = item.Obj["frame"].Obj;
				frame.width = (int)frameValues["w"].Number;
				frame.height = (int)frameValues["h"].Number;
				frame.x = (int)frameValues["x"].Number;
				frame.y = importedInfos.height - (int)frameValues["y"].Number - frame.height; // unity has a different coord system

				frame.duration = (int)item.Obj["duration"].Number;

				importedInfos.frames.Add(frame);
			}

			importedInfos.CalculateTimings();

			return importedInfos;
		}
        private void CalculateTimings()
        {
            for (int i = 0; i < animations.Count; i++)
            {
                AsepriteAnimation anim = animations[i];

                anim.timings = new List <float>();
                float timeCount = 0;
                anim.timings.Add(timeCount);

                for (int k = 0; k < anim.Count; k++)
                {
                    timeCount += frames[k + anim.from].duration / 1000f;
                    anim.timings.Add(timeCount);
                }
            }
        }
Beispiel #7
0
        public override void Start()
        {
            this.Sprite = this.Content.Load <Texture2D>("Sprites/player/prototype_character");
            this.AsepriteDefinitions = this.Content.Load <AsepriteDefinitions>("Sprites/player/prototype_character_json");
            this.AsepriteAnimation   = new AsepriteAnimation(this.AsepriteDefinitions);

            this.SpriteColor = Color.Red;

            this.TextureSize = new Vector2(64, 64);
            this._bodySize   = new Vector2(10, 32);

            this.SetBoxCollision(this.World);
            this.CBody.Tag      = "Knight";
            this.CBody.BodyType = BodyType.Dynamic;
            this.CBody.SetCollidesWith(Category.Cat1);
            this.CBody.SetFriction(1);

            this.DontCollisionWithTag.Add("Player");
            this.DontCollisionWithTag.Add("Knight");
            this.DontCollisionWithTag.Add("Troll");

            this.VigiliantModeActive = true;
        }
        public static void Draw(AsepriteAnimation ani, Vector2 pos)
        {
            Graphics.SetColor(Color.White);
            ani.Draw(pos.X, pos.Y);

            Graphics.SetColor(Color.Green);
            int r = 10;

            DrawCrossCircle(pos, r);

            foreach (var kv in ani.CurrentFrameRectDict) // rect
            {
                var pp_rect = kv.Value;
                pp_rect.Location += pos;
                Graphics.Rectangle(DrawMode.Line, pp_rect);
            }

            foreach (var kv in ani.CurrentFrameTransDict) // trans
            {
                var pps = pos + kv.Value;                 // ani.CurrentFrameTransToPos(pos, kv.Value);
                DrawCrossCircle(pps, 5);
                Graphics.Print(kv.Key, pps.X, pps.Y);
            }
        }
        public void Reset(string path)
        {
            ani       = AsepriteAnimation.New(path, null);
            aniCloned = ani.Clone();

            ani.FrameBegin += (index) =>
            {
                Console.WriteLine($"{path} - b - [{index}] / { ani.FrameCount - 1}");

                foreach (var evtName in ani.GetFrameEvent(index))
                {
                    Console.WriteLine($"event:   " + evtName);
                }
            };
            ani.FrameEnd += (index) =>
            {
                Console.WriteLine($"{path} - e - [{index}] / { ani.FrameCount - 1}");
            };
            tagNameList.Clear();
            tagNameList.AddRange(ani.TagNameIter);
            tagNameIndex = -1;
            ToNextTag();
            Window.SetTitle(path);
        }
Beispiel #10
0
        public override void Start()
        {
            this.Sprite = this.Content.Load <Texture2D>("Sprites/Enemies/troll");
            this.AsepriteDefinitions = this.Content.Load <AsepriteDefinitions>("Sprites/Enemies/troll_info");
            this.AsepriteAnimation   = new AsepriteAnimation(this.AsepriteDefinitions);

            this.SpriteColor = Color.Red;

            this.TextureSize = new Vector2(64, 64);
            this._bodySize   = new Vector2(10, 32);

            this.SetBoxCollision(this.World);
            this.CBody.Tag      = "Troll";
            this.CBody.BodyType = BodyType.Dynamic;
            this.CBody.SetCollidesWith(Category.Cat1);
            this.CBody.SetFriction(1);

            this.DontCollisionWithTag.Add("Player");
            this.DontCollisionWithTag.Add("Knight");
            this.DontCollisionWithTag.Add("Troll");

            _right = false;
            this.CurrentAnimation = AnimationType.BORN;
        }
		public void CreateAnimation(string path, string masterName, AsepriteAnimation anim, List<Sprite> sprites)
		{
			AnimationClip clip;
            string fileName = path + "/" + masterName + "_" + anim.name + ".anim";

			// check if animation file already exists
			clip = AssetDatabase.LoadAssetAtPath<AnimationClip>(fileName);
			if (clip == null)
			{
				clip = new AnimationClip();
				AssetDatabase.CreateAsset(clip, fileName);
			}

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

			EditorCurveBinding curveBinding = new EditorCurveBinding
			{
				type = typeof(SpriteRenderer),
				propertyName = "m_Sprite"
			};

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

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

				Sprite sprite = sprites[i + anim.from];
				keyFrame.value = sprite;
				keyFrames[i] = keyFrame;
			}

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

			timePoint = anim.GetTimePoint(anim.Count);
			ObjectReferenceKeyframe lastKeyFrame = new ObjectReferenceKeyframe { time = timePoint - (1f / clip.frameRate) };
			Sprite lastSprite = sprites[anim.Count - 1 + anim.from];
			lastKeyFrame.value = lastSprite;
			keyFrames[anim.Count] = lastKeyFrame;

			// save animation clip values
			AnimationUtility.SetObjectReferenceCurve(clip, curveBinding, keyFrames);
			EditorUtility.SetDirty(clip);
			anim.animationClip = clip;
		}
        public void CreateAnimation(string path, string masterName, AsepriteAnimation anim, List <Sprite> sprites)
        {
            AnimationClip clip;
            string        fileName = path + "/" + masterName + "_" + anim.name + ".anim";

            // check if animation file already exists
            clip = AssetDatabase.LoadAssetAtPath <AnimationClip>(fileName);
            if (clip == null)
            {
                clip = new AnimationClip();
                AssetDatabase.CreateAsset(clip, fileName);
            }

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

            EditorCurveBinding curveBinding = new EditorCurveBinding
            {
                type         = typeof(SpriteRenderer),
                propertyName = "m_Sprite"
            };

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

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

                Sprite sprite = sprites[i + anim.from];
                keyFrame.value = sprite;
                keyFrames[i]   = keyFrame;
            }

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

            timePoint = anim.GetTimePoint(anim.Count);
            ObjectReferenceKeyframe lastKeyFrame = new ObjectReferenceKeyframe {
                time = timePoint - (1f / clip.frameRate)
            };
            Sprite lastSprite = sprites[anim.Count - 1 + anim.from];

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

            // save animation clip values
            AnimationUtility.SetObjectReferenceCurve(clip, curveBinding, keyFrames);
            EditorUtility.SetDirty(clip);
            anim.animationClip = clip;
        }