Skip to content

Instantly share code, notes, and snippets.

@LordOkami
Created March 9, 2020 18:28
Show Gist options
  • Save LordOkami/e78c3fbbffd9560c99f3181d86df484c to your computer and use it in GitHub Desktop.
Save LordOkami/e78c3fbbffd9560c99f3181d86df484c to your computer and use it in GitHub Desktop.
Unity ECS - CodeMonkey 01
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Entities;
public struct LevelComponent : IComponentData
{
public float level;
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Entities;
public class LevelUpSystem : ComponentSystem
{
protected override void OnUpdate()
{
Entities.ForEach((ref LevelComponent levelComponent) => {
levelComponent.level += 1f * Time.DeltaTime;
Debug.Log(levelComponent.level);
});
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Entities;
using Unity.Transforms;
using Unity.Collections;
using Unity.Rendering;
public class Testing : MonoBehaviour
{
[SerializeField] private Mesh mesh;
[SerializeField] private Material material;
private void Start()
{
EntityManager entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
EntityArchetype entityArchetype = entityManager.CreateArchetype(
typeof(LevelComponent),
typeof(Translation),
typeof(RenderMesh)
);
NativeArray<Entity> entityArray = new NativeArray<Entity>(1, Allocator.Temp);
entityManager.CreateEntity(entityArchetype, entityArray);
for (int i = 0; i < entityArray.Length; i++)
{
Entity entity = entityArray[i];
entityManager.SetComponentData(entity, new LevelComponent { level = 10 });
entityManager.SetSharedComponentData(entity, new RenderMesh
{
mesh = this.mesh,
material = this.material
});
}
entityArray.Dispose();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment