void Update() { if (Time.time > nextUpdate) { // Set texture scale offset accordingly SpriteDataList spriteAnim = animationList[animationIndex]; SpriteData spriteInfo = spriteAnim.spriteList[spriteIndex++]; //Debug.Log(System.DateTime.Now + " : " + transform.parent.parent.name + "/" + transform.parent.name + "/" + this.name + " > Set Sprite Coords " + spriteInfo.coords); if (!myMaterial.GetTextureScale("_MainTex").Equals(spriteInfo.scale)) { myMaterial.SetTextureScale("_MainTex", spriteInfo.scale); } myMaterial.SetTextureOffset("_MainTex", spriteInfo.offset); if (spriteIndex >= animationList[animationIndex].spriteList.Length) { if (animationList[animationIndex].loop) { spriteIndex = 0; } else { spriteIndex--; rate = 1; // or enabled = false; but change the FPS for the whole game } } nextUpdate = Time.time + rate; } }
public void ReadLevelData(int levelId) { if (levelId >= maxLevels) { return; } string path = Application.dataPath + "/level_" + levelId.ToString() + ".txt"; if (!File.Exists(path)) { WriteEmptyLevelData(levelId); } spritesList = JsonUtility.FromJson <SpriteDataList>(File.ReadAllText(path)); }
public void SetAnimation(string name) { string upperName = name.ToUpper(); int newIndex = animationIndex; for (int i = 0; i < animationList.Length; i++) { if (animationList[i].name.ToUpper().Equals(upperName)) { newIndex = i; } } if (newIndex != animationIndex) { //Debug.Log(transform.parent.parent.name + "/" + transform.parent.name + "/" + this.name + " > Set Animation to " + name); // Update offset and scale and current animation rate spriteIndex = 0; animationIndex = newIndex; SpriteDataList spriteAnim = animationList[animationIndex]; if (spriteAnim.fps <= 0) { rate = 10; } else { rate = 1 / spriteAnim.fps; } for (int i = 0; i < spriteAnim.spriteList.Length; i++) { SpriteData spriteInfo = spriteAnim.spriteList[i]; // Use texture size to determine sprite offsets (coords / texture size) and texture scale (sprite size / texture size) spriteInfo.scale = new Vector2(spriteInfo.size.x / textureSize.x, spriteInfo.size.y / textureSize.y); spriteInfo.offset = new Vector2(spriteInfo.coords.x / textureSize.x, 1 - (spriteInfo.coords.y + spriteInfo.size.y) / textureSize.y); } // Immediate update nextUpdate = Time.time; } }