Skip to content

Instantly share code, notes, and snippets.

@jeffvella
Created August 17, 2018 16:13
Show Gist options
  • Save jeffvella/7e5f6daf75a5347a2c03ff9a3cfcd2c7 to your computer and use it in GitHub Desktop.
Save jeffvella/7e5f6daf75a5347a2c03ff9a3cfcd2c7 to your computer and use it in GitHub Desktop.
Monobehavior for use with Svelto ECS and Odin Inspectors to explore entity data.
using System;
using System.Collections;
using System.Collections.Generic;
using Sirenix.OdinInspector;
using Svelto.ECS;
using Svelto.ECS.Example.Survive;
using Svelto.ECS.Internal;
using UnityEngine;
public class EntitiesViewer : MonoBehaviour
{
private MainContext _context;
private Dictionary<int, Dictionary<Type, ITypeSafeDictionary>> _db;
void Start ()
{
_context = GetComponent<MainContext>();
_db = _context._applicationRoot._enginesRoot._groupEntityDB;
}
[ShowInInspector]
public Dictionary<int, Dictionary<int, List<IEntityStruct>>> Data = new Dictionary<int, Dictionary<int, List<IEntityStruct>>>();
void Update ()
{
foreach (var group in _db)
{
var gid = group.Key;
var entityTypeContainers = group.Value;
var groupDictionary = new Dictionary<int, List<IEntityStruct>>();
Data[gid] = groupDictionary;
foreach (var entity in entityTypeContainers)
{
var components = entity.Value.GetEntityStructs();
foreach (var component in components)
{
if (!groupDictionary.ContainsKey(component.Key))
{
groupDictionary[component.Key] = new List<IEntityStruct>();
}
groupDictionary[component.Key].Add(component.Value);
}
}
}
}
}
@jeffvella
Copy link
Author

jeffvella commented Aug 17, 2018

Requires Changes to TypeSafeDictionary.cs

ITypeSafeDictionary

 IEnumerable<KeyValuePair<int, IEntityStruct>> GetEntityStructs();

TypeSafeDictionary

        public IEnumerable<KeyValuePair<int, IEntityStruct>> GetEntityStructs()
        {
            return this.AsEnumerable().Select(o => new KeyValuePair<int, IEntityStruct>(o.Key, o.Value));
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment