暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AnimatedTile.cs 4.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. namespace UnityEngine.Tilemaps
  3. {
  4. /// <summary>
  5. /// Animated Tiles are tiles which run through and display a list of sprites in sequence.
  6. /// </summary>
  7. [Serializable]
  8. [HelpURL("https://docs.unity3d.com/Packages/com.unity.2d.tilemap.extras@latest/index.html?subfolder=/manual/AnimatedTile.html")]
  9. public class AnimatedTile : TileBase
  10. {
  11. /// <summary>
  12. /// The List of Sprites set for the Animated Tile.
  13. /// This will be played in sequence.
  14. /// </summary>
  15. public Sprite[] m_AnimatedSprites;
  16. /// <summary>
  17. /// The minimum possible speed at which the Animation of the Tile will be played.
  18. /// A speed value will be randomly chosen between the minimum and maximum speed.
  19. /// </summary>
  20. public float m_MinSpeed = 1f;
  21. /// <summary>
  22. /// The maximum possible speed at which the Animation of the Tile will be played.
  23. /// A speed value will be randomly chosen between the minimum and maximum speed.
  24. /// </summary>
  25. public float m_MaxSpeed = 1f;
  26. /// <summary>
  27. /// The starting time of this Animated Tile.
  28. /// This allows you to start the Animation from time in the list of Animated Sprites depending on the
  29. /// Tilemap's Animation Frame Rate.
  30. /// </summary>
  31. public float m_AnimationStartTime;
  32. /// <summary>
  33. /// The starting frame of this Animated Tile.
  34. /// This allows you to start the Animation from a particular Sprite in the list of Animated Sprites.
  35. /// If this is set, this overrides m_AnimationStartTime.
  36. /// </summary>
  37. public int m_AnimationStartFrame = 0;
  38. /// <summary>
  39. /// The Collider Shape generated by the Tile.
  40. /// </summary>
  41. public Tile.ColliderType m_TileColliderType;
  42. /// <summary>
  43. /// Flags for controlling the Tile Animation.
  44. /// </summary>
  45. public TileAnimationFlags m_TileAnimationFlags;
  46. /// <summary>
  47. /// Retrieves any tile rendering data from the scripted tile.
  48. /// </summary>
  49. /// <param name="position">Position of the Tile on the Tilemap.</param>
  50. /// <param name="tilemap">The Tilemap the tile is present on.</param>
  51. /// <param name="tileData">Data to render the tile.</param>
  52. public override void GetTileData(Vector3Int position, ITilemap tilemap, ref TileData tileData)
  53. {
  54. tileData.transform = Matrix4x4.identity;
  55. tileData.color = Color.white;
  56. if (m_AnimatedSprites != null && m_AnimatedSprites.Length > 0)
  57. {
  58. tileData.sprite = m_AnimatedSprites[m_AnimatedSprites.Length - 1];
  59. tileData.colliderType = m_TileColliderType;
  60. }
  61. }
  62. /// <summary>
  63. /// Retrieves any tile animation data from the scripted tile.
  64. /// </summary>
  65. /// <param name="position">Position of the Tile on the Tilemap.</param>
  66. /// <param name="tilemap">The Tilemap the tile is present on.</param>
  67. /// <param name="tileAnimationData">Data to run an animation on the tile.</param>
  68. /// <returns>Whether the call was successful.</returns>
  69. public override bool GetTileAnimationData(Vector3Int position, ITilemap tilemap, ref TileAnimationData tileAnimationData)
  70. {
  71. if (m_AnimatedSprites.Length > 0)
  72. {
  73. tileAnimationData.animatedSprites = m_AnimatedSprites;
  74. tileAnimationData.animationSpeed = Random.Range(m_MinSpeed, m_MaxSpeed);
  75. tileAnimationData.animationStartTime = m_AnimationStartTime;
  76. tileAnimationData.flags = m_TileAnimationFlags;
  77. if (0 < m_AnimationStartFrame && m_AnimationStartFrame <= m_AnimatedSprites.Length)
  78. {
  79. var tilemapComponent = tilemap.GetComponent<Tilemap>();
  80. if (tilemapComponent != null && tilemapComponent.animationFrameRate > 0)
  81. tileAnimationData.animationStartTime = (m_AnimationStartFrame - 1) / tilemapComponent.animationFrameRate;
  82. }
  83. return true;
  84. }
  85. return false;
  86. }
  87. }
  88. }