public void UpdateSpriteAnimations() { for (int index = 0; index < this.spriteAnimationList.Count; index++) { SpriteAnimationItem spriteAnimationItem = this.spriteAnimationList[index]; SpriteCollection.TextureData textureData = GetTextureDataDirection(spriteAnimationItem.sprite); if (textureData.framesCount > 1) { if (Time.realtimeSinceStartup - spriteAnimationItem.lastUpdatedTime >= 1.0f / textureData.fps) { spriteAnimationItem.lastUpdatedTime = Time.realtimeSinceStartup; spriteAnimationItem.currentFrame++; if (spriteAnimationItem.currentFrame >= textureData.framesCount) { spriteAnimationItem.currentFrame = 0; } } } else { spriteAnimationItem.currentFrame = 0; } } Repaint(); }
static int SortByDepth(SpriteAnimationItem sp1, SpriteAnimationItem sp2) { int sp1Depth = (int)sp1.sprite.renderingLayer + sp1.sprite.renderingOrder; int sp2Depth = (int)sp2.sprite.renderingLayer + sp2.sprite.renderingOrder; return(sp1Depth.CompareTo(sp2Depth)); }
public void InitSpriteAnimations() { if (selectedItemIndex == -1) { return; } ItemsCollection.ItemData itemData = itemsCollection.list[selectedItemIndex]; this.spriteAnimationList = new List <SpriteAnimationItem>(); foreach (int spriteId in itemData.GetSprites((Common.State)state)) { SpriteCollection.SpriteData sprite = spriteCollection.GetSprite(spriteId); SpriteAnimationItem spriteAnimationData = new SpriteAnimationItem(); spriteAnimationData.sprite = sprite; spriteAnimationData.lastUpdatedTime = Time.realtimeSinceStartup; SpriteCollection.TextureData textureData = GetTextureDataDirection(sprite); spriteAnimationData.numberOfColumns = textureData.numberOfColumns; spriteAnimationData.numberOfRows = textureData.numberOfRows; this.spriteAnimationList.Add(spriteAnimationData); } this.spriteAnimationList.Sort(SortByDepth); }
void RenderPreviewArea() { if (gridTexture == null) { gridTexture = Resources.Load("grid_4x4", typeof(Texture2D)) as Texture2D; } //DRAW GRID Vector2 centerPointOfPreviewArea = new Vector2((WidthOfLetPanel + position.width - WidthOfRightPanel) / 2, position.height / 2); GUI.Label(new Rect(centerPointOfPreviewArea.x - defaultGridSize.x / 2, centerPointOfPreviewArea.y - defaultGridSize.y / 2, defaultGridSize.x, defaultGridSize.y), gridTexture); //DRAW ITEM SPRITE if (selectedItemIndex != -1) { for (int index = 0; index < this.spriteAnimationList.Count; index++) { SpriteAnimationItem spriteAnimationItem = this.spriteAnimationList[index]; SpriteCollection.TextureData textureData = GetTextureDataDirection(spriteAnimationItem.sprite); Texture2D texture = textureData.texture; if (texture == null) { continue; } float scale = textureData.scale; int numberOfColumns = spriteAnimationItem.numberOfColumns; int numberOfRows = spriteAnimationItem.numberOfRows; float offsetX = textureData.offsetX; float offsetY = textureData.offsetY; int gridSize = spriteAnimationItem.sprite.gridSize; defaultImgSize.x = 256 * scale / 100; defaultImgSize.x = 256 * scale / 100; float heightFactor = ((float)texture.height / (float)texture.width) * ((float)numberOfColumns / numberOfRows); float upFactor = Mathf.Cos(Mathf.Deg2Rad * 45) * gridSize / 2.0f; float x = (centerPointOfPreviewArea.x - defaultImgSize.x / 2) + offsetX; float y = (centerPointOfPreviewArea.y - defaultImgSize.x * heightFactor / 2) - offsetY; float framePaddingX = defaultImgSize.x * (spriteAnimationItem.currentFrame % numberOfColumns); float framePaddingY = defaultImgSize.x * heightFactor * (spriteAnimationItem.currentFrame / numberOfColumns); Rect imgRect = new Rect(x, y, defaultImgSize.x, defaultImgSize.x * heightFactor); GUI.BeginGroup(imgRect); GUI.DrawTexture(new Rect(-framePaddingX, -framePaddingY, defaultImgSize.x * numberOfColumns, defaultImgSize.x * spriteAnimationItem.numberOfRows * heightFactor), texture); GUI.EndGroup(); } } }