Skip to content

Instantly share code, notes, and snippets.

@MizoTake
Last active June 2, 2018 16:05
Show Gist options
  • Select an option

  • Save MizoTake/6ad9baa35eb7c4429fb4f9fb9aa90d7f to your computer and use it in GitHub Desktop.

Select an option

Save MizoTake/6ad9baa35eb7c4429fb4f9fb9aa90d7f to your computer and use it in GitHub Desktop.
Unity ECS to MonoBehaviour-ish
using System.Collections.Generic;
using UniRx;
using UniRx.Triggers;
using Unity.Entities;
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.UI;
public abstract partial class BaseComponentSystem<T> : ComponentSystem where T : struct
{
private Subject<ComponentGroupArray<T>> recetiveStart = new Subject<ComponentGroupArray<T>> ();
private Subject<ComponentGroupArray<T>> recetiveUpdate = new Subject<ComponentGroupArray<T>> ();
protected List<T> entityMonitor = new List<T> ();
protected CompositeDisposable dispose = new CompositeDisposable ();
public virtual void Start (T element) { }
public virtual void Update (T element) { }
public BaseComponentSystem ()
{
recetiveStart
.DistinctUntilChanged ()
.Subscribe (_ =>
{
var cnt = (entityMonitor.Count == 0) ? 0 : _.Length - entityMonitor.Count;
for (var i = cnt; i < _.Length; i++)
{
if (entityMonitor.IndexOf (_[i]) != -1) continue;
entityMonitor.Add (_[i]);
Start (_[i]);
}
entityMonitor.RemoveAll (element => element.Equals (null));
})
.AddTo (dispose);
recetiveUpdate
.Subscribe (_ =>
{
for (var i = 0; i < _.Length; i++)
{
Update (_[i]);
}
})
.AddTo (dispose);
}
~BaseComponentSystem ()
{
Dispose ();
}
protected sealed override void OnUpdate ()
{
recetiveStart.OnNext (GetEntities<T> ());
recetiveUpdate.OnNext (GetEntities<T> ());
}
}
public abstract partial class BaseComponentSystem<T> : System.IDisposable
{
public void Dispose ()
{
dispose.Clear ();
}
}
// using System.Collections;
// using System.Collections.Generic;
// using UnityEngine;
// namespace Sample
// {
// public struct Data
// {
// public Sample sample;
// }
// public class System : BaseComponentSystem<Data>
// {
// public override void Start (Data element)
// {
// }
// public override void Update (Data element)
// {
// }
// }
// }
fileFormatVersion: 2
guid: 8813c28150d0273458a78aaac90b754b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment