0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Unity]ECS(0.17.0) 子の要素に指定のIComponentData(それ以外も可能)が含まれているか

Last updated at Posted at 2021-03-15

Unity v.2020.2
Entity v.0.17.0

なんやかんや新しくなったら検索阻害防止のため、予告なく削除します。

<重要>
・PrefabのGameobjectからEntityへ変換されると
 ChidとかLinkedEntityGroupが勝手につけられて関連性が持たれるようです。

デバッグ機能で確認。(公式ドキュメントとか知りませんが動きます)
EntityManagerの拡張メソッドにしました。

こんなやり方で取れるよっていう参考程度に。
(どうせみんないなくなる)

例えばEntityのLODのマテリアルをまとめて変更したいときとか。

コードは以下の通りです。

EntityManagerExtends.cs

    using Unity.Entities;
    public static class EntityManagerExtends
    {
        public static Entity FindChild<T>(this in EntityManager manager, in Entity t)
        {
            if (!manager.HasComponent<LinkedEntityGroup>(t)) return Entity.Null;
            foreach (var linkentity in manager.GetBuffer<LinkedEntityGroup>(t))
                if (manager.HasComponent<T>(linkentity.Value)) return linkentity.Value;
            return Entity.Null;
        }
        public static Entity[] FindChildAll<T>(this in EntityManager manager, in Entity t)
        {
            using (var entites = new Unity.Collections.NativeList<Entity>(Unity.Collections.Allocator.Temp))
            {
                if (!manager.HasComponent<LinkedEntityGroup>(t)) return new Entity[0];
                var linkGroupList = manager.GetBuffer<LinkedEntityGroup>(t);
                foreach (var linkentity in linkGroupList) if (manager.HasComponent<T>(linkentity.Value)) entites.Add(linkentity.Value);
                return entites.ToArray();
            }
        }
    }

HowToUse.cs
 var entities = EntityManager.FindChildAll<Unity.Rendering.RenderMesh>(targetEntity);
 if (entities.Length > 0) Debug.Log("Hit!");
 else Debug.Log("Nothing");
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?