Advertisement
Guest User

ecs prefab

a guest
Apr 7th, 2025
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. using Unity.Assertions;
  2. using Unity.Entities;
  3. using UnityEngine;
  4.  
  5. namespace ScriptsSandbox
  6. {
  7. [ExecuteAlways]
  8. public class PrefabAuthoring : MonoBehaviour
  9. {
  10. void OnEnable()
  11. {
  12. var entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
  13. var entityPrefab = entityManager.CreateEntity(
  14. typeof(Prefab),
  15. typeof(MyFunkyComponent));
  16.  
  17. Assert.IsTrue(entityPrefab != Entity.Null);
  18.  
  19.  
  20. var query = World.DefaultGameObjectInjectionWorld.EntityManager.CreateEntityQuery(
  21. ComponentType.ReadOnly<MyFunkyComponent>(),
  22. ComponentType.ReadOnly<Prefab>());
  23.  
  24. var prefabs = query.ToEntityArray(Unity.Collections.Allocator.Temp);
  25.  
  26. Debug.Log($"First Query found {prefabs.Length} prefabs");
  27. prefabs.Dispose();
  28.  
  29. var queryPrefab = World.DefaultGameObjectInjectionWorld.EntityManager.CreateEntityQuery(new EntityQueryDesc
  30. {
  31. All = new[]
  32. {
  33. ComponentType.ReadOnly<MyFunkyComponent>()
  34. },
  35. Options = EntityQueryOptions.Default | EntityQueryOptions.IncludePrefab
  36. });
  37.  
  38. var prefabs2 = queryPrefab.ToEntityArray(Unity.Collections.Allocator.Temp);
  39. Debug.Log($"Second Query found {prefabs2.Length} prefabs");
  40. prefabs2.Dispose();
  41.  
  42. entityManager.DestroyEntity(entityPrefab);
  43. }
  44. }
  45.  
  46. public struct MyFunkyComponent : IComponentData
  47. {
  48. public float Size;
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement