Created
March 9, 2020 18:28
-
-
Save LordOkami/e78c3fbbffd9560c99f3181d86df484c to your computer and use it in GitHub Desktop.
Unity ECS - CodeMonkey 01
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using Unity.Entities; | |
public struct LevelComponent : IComponentData | |
{ | |
public float level; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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