Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. /**********************************************************************
  2. * Unity ECS - Test/Sample Code
  3. * Mitchell Pell
  4. * 2019.07.26
  5. * v0.1
  6. *********************************************************************/
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using UnityEngine;
  10. using Unity.Entities;
  11. using Unity.Burst;
  12. using Unity.Collections;
  13. using Unity.Jobs;
  14. using Unity.Transforms;
  15. using Unity.Mathematics;
  16. using Unity.Rendering;
  17. //---------------------------------------------------------------------
  18. public class GameHandlerSpriteSheetRenderer : MonoBehaviour
  19. {
  20. //Enable/Disable sprite sheet animation test
  21. [SerializeField] private bool createAnimatedSpriteSheet;
  22. [SerializeField] private float animationFrameTime;
  23.  
  24. //Lazy code to get the quad mesh and sprite sheet material.
  25. private static GameHandlerSpriteSheetRenderer s_instance;
  26. public static GameHandlerSpriteSheetRenderer GetInstance() { return s_instance; }
  27.  
  28. //Mesh assigned in the editor that the SpriteSheetRenderer grabs.
  29. public Mesh quadMesh;
  30. //Sprite sheet material assigned in the editor that the SpriteSheetRenderer grabs.
  31. public Material spriteSheetMaterial;
  32.  
  33. private void Awake()
  34. {
  35. /* Lazy code setting the instance of this class with quadMesh
  36. * and spriteSheetMaterial defined. */
  37. s_instance = this;
  38.  
  39. /* Get the Entity Manager and create a new Translation Entity
  40. that the SpriteSheetRenderer will use to draw the Sprite. */
  41. EntityManager entityManager = World.Active.EntityManager;
  42.  
  43. if (createAnimatedSpriteSheet)
  44. {
  45. AnimatedInstancedSpriteSheetTest(entityManager);
  46. }
  47. }
  48.  
  49. ///
  50. /// Animated Sprite Sprite Test using Instanced UV and the ECS system.
  51. ///
  52. private void AnimatedInstancedSpriteSheetTest(EntityManager entityManager)
  53. {
  54. EntityArchetype spriteArchType = entityManager.CreateArchetype(
  55. // SpriteSheetRendere
  56. typeof(Translation),
  57. // SpriteSheetAnimationSystem
  58. typeof(SpriteSheetAnimation_Data)
  59. );
  60. Entity sprite = entityManager.CreateEntity(spriteArchType);
  61.  
  62. //Set
  63. entityManager.SetComponentData(sprite,
  64. new Translation
  65. {
  66. Value = new float3(0, 0, 0)
  67. }
  68. );
  69.  
  70. //Set
  71. if (animationFrameTime <= 0) animationFrameTime = 0.2f;
  72. entityManager.SetComponentData(sprite,
  73. new SpriteSheetAnimation_Data
  74. {
  75. m_frameTimer = 0f,
  76. m_frameTimerMax = animationFrameTime,
  77. m_startFrameX = 0,
  78. m_startFrameY = 0,
  79. m_endFrameX = 0,
  80. m_endFrameY = 24,
  81. m_currentFrameX = 0,
  82. m_currentFrameY = 0,
  83. }
  84. );
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement