Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**********************************************************************
- * Unity ECS - Test/Sample Code
- * Mitchell Pell
- * 2019.07.26
- * v0.1
- *********************************************************************/
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Unity.Entities;
- using Unity.Burst;
- using Unity.Collections;
- using Unity.Jobs;
- using Unity.Transforms;
- using Unity.Mathematics;
- using Unity.Rendering;
- //---------------------------------------------------------------------
- public class GameHandlerSpriteSheetRenderer : MonoBehaviour
- {
- //Enable/Disable sprite sheet animation test
- [SerializeField] private bool createAnimatedSpriteSheet;
- [SerializeField] private float animationFrameTime;
- //Lazy code to get the quad mesh and sprite sheet material.
- private static GameHandlerSpriteSheetRenderer s_instance;
- public static GameHandlerSpriteSheetRenderer GetInstance() { return s_instance; }
- //Mesh assigned in the editor that the SpriteSheetRenderer grabs.
- public Mesh quadMesh;
- //Sprite sheet material assigned in the editor that the SpriteSheetRenderer grabs.
- public Material spriteSheetMaterial;
- private void Awake()
- {
- /* Lazy code setting the instance of this class with quadMesh
- * and spriteSheetMaterial defined. */
- s_instance = this;
- /* Get the Entity Manager and create a new Translation Entity
- that the SpriteSheetRenderer will use to draw the Sprite. */
- EntityManager entityManager = World.Active.EntityManager;
- if (createAnimatedSpriteSheet)
- {
- AnimatedInstancedSpriteSheetTest(entityManager);
- }
- }
- ///
- /// Animated Sprite Sprite Test using Instanced UV and the ECS system.
- ///
- private void AnimatedInstancedSpriteSheetTest(EntityManager entityManager)
- {
- EntityArchetype spriteArchType = entityManager.CreateArchetype(
- // SpriteSheetRendere
- typeof(Translation),
- // SpriteSheetAnimationSystem
- typeof(SpriteSheetAnimation_Data)
- );
- Entity sprite = entityManager.CreateEntity(spriteArchType);
- //Set
- entityManager.SetComponentData(sprite,
- new Translation
- {
- Value = new float3(0, 0, 0)
- }
- );
- //Set
- if (animationFrameTime <= 0) animationFrameTime = 0.2f;
- entityManager.SetComponentData(sprite,
- new SpriteSheetAnimation_Data
- {
- m_frameTimer = 0f,
- m_frameTimerMax = animationFrameTime,
- m_startFrameX = 0,
- m_startFrameY = 0,
- m_endFrameX = 0,
- m_endFrameY = 24,
- m_currentFrameX = 0,
- m_currentFrameY = 0,
- }
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement