Unity Entities Tips

初めに

Unity Entities (ECSとも言われる)に関する情報が溜まってきたので書きます。

内容

EntityManager.DestroyEntity

EntityManager.DestroyEntity には EntityQuery を渡すこともできるが、実行時エラーになる。 ToEntityArray で取り出したリストを渡す。

Entity と Particle System

Entityにパーティクルは置けるが、動的な操作は難しいらしく、調べたが解決出来なかった。

https://discussions.unity.com/t/particle-system-on-entity/772431/15

マネージドで位置同期するか、変更が無い設計にする。

EntityManager.CreateEntity その1

コード上で

var entity = entityManager.CreateEntity();

で Entity を作った場合、 LocalTransform を始め何も付いていない。

後から追加するには AddComponentData を使うが、大量に作る時はアーキタイプを指定する事で効率が上がる。

var entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
var archetype = entityManager.CreateArchetype(typeof(LocalTransform), typeof(MyData.ComponentData));
for(var i = 0; i < 10000; i++)
{
    var entity = entityManager.CreateEntity(archetype);
}

EntityManager.CreateEntity その2

EntityManager.CreateEntity で作成した Entity に子を追加する場合、子の位置がおかしくなる。

親である作成 Entity に LocalToWorld を付ける事で正しくなる。

https://discussions.unity.com/t/creating-entities-at-runtime-any-entity-i-make-a-child-immediately-unparents-the-following-frame/947433/9

Entity でマテリアル操作

リンクのみ

https://docs.unity3d.com/Packages/com.unity.entities.graphics@1.4/manual/runtime-entity-creation.html

Entity が親子関係を持っている時の、子の World 座標

Entityが親子関係を持っている場合、子のワールド座標を得るには、

var pos = entityManaer.GetComponentData<LocalTransform>(entity).Position;

ではなく、

var pos = entityManaer.GetComponentData<LocalToWorld>(entity).Position;

になる。

https://docs.unity3d.com/Packages/com.unity.entities@1.4/api/Unity.Transforms.LocalToWorld.html

ただし定期的に計算されるものである為精度が低い。

動かないオブジェクトに対して使用するか、以下を参考に自分で計算する

https://docs.unity3d.com/Packages/com.unity.entities@1.4/manual/transforms-helpers.html#computeworldtransformmatrix

TransformUsageFlags の意味

Bake する時に呼ぶ

var entity = GetEntity(TransformUsageFlags.Dynamic);

での TransformUsageFlags は、 MonoBehaviour から Entity に変換する際に MonoBehaviour から何を引き継ぐかを設定するものである。

時間経過を制御する

https://docs.unity3d.com/Packages/com.unity.entities@1.4/manual/systems-time.html

https://docs.unity3d.com/Packages/com.unity.entities@1.4/api/Unity.Entities.FixedStepSimulationSystemGroup.Timestep.html#Unity_Entities_FixedStepSimulationSystemGroup_Timestep

変更方法

https://github.com/needle-mirror/com.unity.entities/blob/0783fbd7a95eaa8cc0e425d3925760ec9564ab0a/Unity.Entities/DefaultWorld.cs#L329

var system = World.DefaultGameObjectInjectionWorld.GetExistingSystemManaged<FixedStepSimulationSystemGroup>();
system.Timestep = 0.1f;

Entities サブシーンを動的ロード

リンクのみ

https://discussions.unity.com/t/generate-sub-scenes-programmatically/785860/21

動的に GameObject を Entity に変換

リンクのみ

https://note.com/upfrontier/n/nda5f57e9a1e3

EditorWindow でプレファブを Entity として配置

var targetSubScene = rootVisualElement.Q<ObjectField>("subScene").value as SubScene;

var obj = PrefabUtility.InstantiatePrefab(MyPrefab) as GameObject;
EditorSceneManager.MoveGameObjectToScene(obj, targetSubScene.EditingScene);

DynamicBuffer

DynamicBuffer は、シングルトンとすると直接アクセスできる関数が用意されている。

var builder = new EntityQueryBuilder(Allocator.Temp).WithOptions(EntityQueryOptions.IncludeSystems);
using var query = entityManager.CreateEntityQuery(builder);

if (!query.TryGetSingletonBuffer<MyData>(out var buffer))
{
    var entity = entityManager.CreateSingletonBuffer<MyData>();
    buffer = query.GetSingletonBuffer<MyData>();
}
buffer.CopyFrom(map.Points.Cast<MyData>().ToArray());

Entities の System を外から切り替え

https://discussions.unity.com/t/changing-systemstate-enabled-has-no-effect/924656/6

public static void SetEnable()
{
    var systemTypeIndex = TypeManager.GetSystemTypeIndex<MySystem>();
    var _handle = World.DefaultGameObjectInjectionWorld.GetExistingSystem(systemTypeIndex);

    ref var state = ref World.DefaultGameObjectInjectionWorld.Unmanaged.ResolveSystemStateRef(_handle);
    state.Enabled = true;
}

Entities と NavMesh

https://discussions.unity.com/t/getting-navigation-meshes-navmesh-to-work-with-entities-1-0/907090

実験的APIを使用。2026/01/03現在で正式になっていない。 https://docs.unity3d.com/6000.3/Documentation/ScriptReference/Experimental.AI.NavMeshQuery.html

https://discussions.unity.com/t/navagent-and-pathfinding-in-ecs/798264

エラーが出た時

Loading Entity Scene failed because the entity header file couldn't be resolved.

編集 → 環境設定。Entities → Clear Entity Cache ボタンを押す。

例外が出ている場合は対処

https://forum.unity.com/threads/solved-loading-entity-scene-failed-because-the-entity-header-file-couldnt-be-resolved.1018339/#post-8672625

最後に

Unity 6.4 から Entities のバージョンが上がるようで、1.4 から 6.4 になる

https://docs.unity3d.com/Packages/com.unity.entities@6.4/manual/index.html

大きくバージョンが上がるのは、基礎への統合に向けた動きとみられる。

Entities.ForEach と Aspects が非推奨化。使ってなかったので良いが、割と影響が大きそう。

top

その他の投稿
20251231-01 Unity UI Toolkit カスタムコントロールサンプル
20250913-01 Unity アプリで Android 16KB ページサイズ確認
20250912-01 Unity 6 で Editor がちらつく問題
20250906-01 Win10サポート終了 ChromeOS Flex を入れてみてつまづいた所
20250812-02 Unity UI Toolkit でのタップ操作検出
20250812-01 Unity UI Toolkit でマップ画面 with RenderTexture
20250811-01 Unity UI Toolkit での座標変換
20250721-01 Unityでカメラが平行投影の場合にScreenToWorldPointがズレる
20250712-01 Unity既存プロジェクトにURP追加
20250320-01 Unity のナビゲーションシステム 追記