Skip to content

Instantly share code, notes, and snippets.

@sarkahn
Created January 3, 2021 09:16
Show Gist options
  • Save sarkahn/78ca665a858ddae16287f6d2047857bb to your computer and use it in GitHub Desktop.
Save sarkahn/78ca665a858ddae16287f6d2047857bb to your computer and use it in GitHub Desktop.
ECS with UGUI Example
using Unity.Entities;
using UnityEngine;
using UnityEngine.UI;
public class PlayerHealthUIAuthoring : MonoBehaviour, IConvertGameObjectToEntity
{
[SerializeField]
TMPro.TMP_Text _uiText;
public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
{
dstManager.AddComponentData(entity, new PlayerHealthUI
{
Text = _uiText
});
}
}
public class PlayerHealthUI : IComponentData
{
public TMPro.TMP_Text Text;
}
public struct Health : IComponentData
{
public int Value;
}
public class UpdateHealthUISystem : SystemBase
{
protected override void OnUpdate()
{
Entities
.WithoutBurst()
.WithChangeFilter<Health>()
.ForEach((PlayerHealthUI healthUI,
in Health health) =>
{
healthUI.Text.text = health.Value.ToString();
}).Run();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment