Wanted to write up a quick blog post about a development philosophy that I've taken up during this project. Since I'm working with a preview package (not meant for release) of Unity DOTS, I have found multiple instances of features that I would have expect to work but still do not.
For example, the physics engine currently has a way to detect TriggerEvents when two triggers intersect, but not when they stop intersecting. I need this for drawing and undrawing a bow. Since on the MonoBehaviour side we have OnTriggerEnter and OnTriggerExit, I'd expect this to eventually exist in DOTS. Now, it wouldn't be too difficult to implement my own trigger system in DOTS, but it would be more work. And since this is something that I hope is included in the release version, it would be unnecessary extra work. So what to do?
Well, you don't. Instead I've created an abstract class EntityBridge:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public abstract class EntityBridge : MonoBehaviour { protected bool isInitialized = false; protected Entity entity; protected EntityManager entityManager; protected virtual void Awake() { entityManager = World.DefaultGameObjectInjectionWorld.EntityManager; } public void Initialize(Entity entity) { this.entity = entity; isInitialized = true; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | private void OnTriggerEnter(Collider other) { if (other.tag == "UndrawnArrow") { _audioSource.PlayOneShot( _handleAudioClips[Random.Range(0, _handleAudioClips.Length)]); entityManager.SetComponentData( entity, new BowDrawAreaTriggerComponent { IsArrowInBowDrawArea = true }); } } private void OnTriggerExit(Collider other) { if (other.tag == "UndrawnArrow") { entityManager.SetComponentData( entity, new BowDrawAreaTriggerComponent { IsArrowInBowDrawArea = false }); _isBowDrawnAudioClipPlaying = false; } } |
No comments:
Post a Comment