### Wrap Unity Components for ECS with C# Source: https://context7.com/dcfapixels/dragonecs-unity/llms.txt Illustrates how to use `UnityComponent` to wrap Unity Component references for use in ECS pools. It includes examples of accessing components directly and via implicit conversion, and defining custom component templates. Requires DCFApixels.DragonECS and UnityEngine. ```csharp using DCFApixels.DragonECS; using UnityEngine; // UnityComponent wraps Unity components for ECS storage public class TransformSystem : IEcsRunProcess, IEcsInject { private EcsDefaultWorld _world; public void Inject(EcsDefaultWorld world) => _world = world; public void Run() { foreach (var e in _world.Where(out Aspect a)) { // Access Unity Transform via UnityComponent ref var transformComponent = ref a.Transforms.Get(e); Transform transform = transformComponent.obj; // Explicit access // Or use implicit conversion Transform t = transformComponent; // Implicit conversion to Transform t.position += Vector3.up * Time.deltaTime; } } class Aspect : EcsAspect { public EcsPool> Transforms; public EcsPool> Rigidbodies; // Any Unity Component type } } // Create template for UnityComponent (auto-fills from GameObject) public class TransformComponentTemplate : UnityComponentTemplate { } public class RigidbodyComponentTemplate : UnityComponentTemplate { } ``` -------------------------------- ### Automatic Entity Creation in Unity with C# Source: https://context7.com/dcfapixels/dragonecs-unity/llms.txt Shows how to use `AutoEntityCreator` to automatically spawn entities connected to an `EcsEntityConnect` component. The example demonstrates a custom `PlayerEntityCreator` that adds specific components during entity creation and uses a custom world provider. Requires DCFApixels.DragonECS and UnityEngine. ```csharp using DCFApixels.DragonECS; using UnityEngine; // Attach AutoEntityCreator to prefab root // Configure via Inspector: // - _connect: Reference to EcsEntityConnect on same or child GameObject // - _world: World provider (defaults to EcsDefaultWorldSingletonProvider) // Custom AutoEntityCreator with specialized entity creation public class PlayerEntityCreator : AutoEntityCreator { [SerializeField] private int _playerIndex; protected override entlong CreateEntity(EcsWorld world) { // Create entity with additional setup entlong entity = world.NewEntityLong(); // Add player-specific components var pool = world.GetPool(); pool.Add(entity.ID) = new PlayerIndexComponent { index = _playerIndex }; return entity; } protected override EcsWorldProviderBase AutoGetWorldProvider() { // Use custom world provider return MyGameWorldProvider.Instance; } } [System.Serializable] public struct PlayerIndexComponent : IEcsComponent { public int index; } ``` -------------------------------- ### EcsRootUnity MonoBehaviour for Simplified ECS Pipeline Management Source: https://context7.com/dcfapixels/dragonecs-unity/llms.txt EcsRootUnity is a MonoBehaviour that simplifies ECS pipeline setup by managing templates and automatically handling Unity lifecycle methods. It can be configured via the Inspector and allows pipeline access from other scripts. Requires DragonECS and Unity. ```csharp using DCFApixels.DragonECS; using UnityEngine; // Attach EcsRootUnity to a GameObject in scene // Configure via Inspector: // - _pipelineProvider: Optional provider for pipeline access elsewhere // - _enablePipelineDebug: Show pipeline monitor in Editor // - _enableWorldDebug: Show world monitors in Editor // - _scriptableTemplates: ScriptablePipelineTemplate assets // - _monoTemplates: MonoPipelineTemplate components // Access pipeline from other scripts public class PipelineAccessExample : MonoBehaviour { [SerializeField] private EcsRootUnity _ecsRoot; private void Start() { // Wait for EcsRootUnity to initialize if (_ecsRoot.IsInit) { EcsPipeline pipeline = _ecsRoot.Pipeline; // Use pipeline... } } } // Manual pipeline setup alternative (without EcsRootUnity) public class ManualPipelineSetup : MonoBehaviour { private EcsPipeline _pipeline; private void Start() { _pipeline = EcsPipeline.New() .Add(new MySystem()) .BuildAndInit(); } private void Update() => _pipeline.Run(); private void LateUpdate() => _pipeline.LateRun(); private void FixedUpdate() => _pipeline.FixedRun(); private void OnDrawGizmos() => _pipeline?.DrawGizmos(); private void OnDestroy() => _pipeline.Destroy(); } public class MySystem : IEcsRunProcess { public void Run() { /* ... */ } } ``` -------------------------------- ### Manage ECS Worlds with EcsWorldProvider in Unity Source: https://context7.com/dcfapixels/dragonecs-unity/llms.txt Demonstrates how to use EcsWorldProvider, a ScriptableObject, to manage ECS world instances. It covers setting up a singleton provider, injecting worlds into systems, and implementing custom world providers. Dependencies include DCFApixels.DragonECS and UnityEngine. ```csharp using DCFApixels.DragonECS; using UnityEngine; // Use the built-in singleton provider for EcsDefaultWorld public class WorldProviderExample : MonoBehaviour { private EcsDefaultWorld _world; private EcsPipeline _pipeline; private void Start() { // Get singleton provider (auto-creates asset in Assets/Resources) EcsDefaultWorldSingletonProvider provider = EcsDefaultWorldSingletonProvider.Instance; // Set an existing world instance _world = new EcsDefaultWorld(); provider.Set(_world); // Or get/create world from provider _world = provider.Get(); // Creates world if provider is empty _pipeline = EcsPipeline.New() .Inject(_world) // Inject world into systems .BuildAndInit(); } } // Custom world provider implementation [CreateAssetMenu(fileName = "MyWorldProvider", menuName = "DragonECS/WorldProviders/MyWorldProvider")] public class EcsMyWorldProvider : EcsWorldProvider { protected override EcsMyWorld BuildWorld(ConfigContainer configs) { return new EcsMyWorld(configs, WorldName, WorldID); } protected override void OnWorldCreated(EcsMyWorld world) { Debug.Log($"World {world.Name} created with ID {world.ID}"); } } // Singleton version for custom world public class EcsMyWorldSingletonProvider : EcsWorldProvider { private static EcsMyWorldSingletonProvider _instance; public static EcsMyWorldSingletonProvider Instance { get { if (_instance == null) { _instance = FindOrCreateSingleton("SingletonMyWorld"); } return _instance; } } protected override EcsMyWorld BuildWorld(ConfigContainer configs) { return new EcsMyWorld(configs, WorldName, WorldID); } } public class EcsMyWorld : EcsWorld { /* Custom world implementation */ } ``` -------------------------------- ### Apply Entity Templates in Unity with C# Source: https://context7.com/dcfapixels/dragonecs-unity/llms.txt Demonstrates how to apply ScriptableEntityTemplateBase and MonoEntityTemplateBase assets to entities within a Unity MonoBehaviour. It shows entity creation, template application, and using extension methods for convenience. Dependencies include DCFApixels.DragonECS and UnityEngine. ```csharp using DCFApixels.DragonECS; using UnityEngine; public class EntityTemplateUsage : MonoBehaviour { // Assign via Inspector (ScriptableEntityTemplate assets) [SerializeField] private ScriptableEntityTemplateBase _playerTemplate; [SerializeField] private ScriptableEntityTemplateBase _enemyTemplate; // MonoEntityTemplate attached to same/child GameObject [SerializeField] private MonoEntityTemplateBase[] _additionalTemplates; private EcsDefaultWorld _world; private void Start() { _world = EcsDefaultWorldSingletonProvider.Instance.Get(); // Create entity and apply template entlong player = _world.NewEntityLong(); _playerTemplate.Apply(_world.ID, player.ID); // Apply multiple templates to same entity foreach (var template in _additionalTemplates) { template.Apply(_world.ID, player.ID); } // Create entity with template via extension entlong enemy = _world.NewEntityWithGameObject(_enemyTemplate, "Enemy", GameObjectIcon.Diamond_Red); } } // Apply templates in a system using foreach public class SpawnSystem : IEcsRunProcess, IEcsInject { private EcsDefaultWorld _world; private ITemplateNode _spawnTemplate; public void Inject(EcsDefaultWorld world) => _world = world; public void Run() { foreach (var e in _world.Where(out SpawnAspect a)) { // Apply template to spawned entities _spawnTemplate.Apply(_world.ID, e); } } class SpawnAspect : EcsAspect { /* ... */ } } ``` -------------------------------- ### Define Reusable ECS Components with Component Templates in Unity Source: https://context7.com/dcfapixels/dragonecs-unity/llms.txt Explains how to create reusable ECS components using Component Templates. It covers defining components (structs with [Serializable]), creating simple templates by inheriting from ComponentTemplate or TagComponentTemplate, and advanced templates with custom gizmos and validation. Dependencies include DCFApixels.DragonECS, System, and UnityEngine. ```csharp using DCFApixels.DragonECS; using System; using UnityEngine; // Define your ECS component (must have [Serializable] for templates) [Serializable] public struct HealthComponent : IEcsComponent { public int maxHealth; public int currentHealth; } [Serializable] public struct SpeedComponent : IEcsComponent { public float moveSpeed; public float rotationSpeed; } [Serializable] public struct EnemyTag : IEcsTagComponent { } // Create component templates (no additional code needed for simple cases) public class HealthComponentTemplate : ComponentTemplate { } public class SpeedComponentTemplate : ComponentTemplate { } public class EnemyTagTemplate : TagComponentTemplate { } // Advanced template with custom gizmos and validation [Serializable] public struct DetectionComponent : IEcsComponent { public float radius; public LayerMask targetLayers; } public class DetectionComponentTemplate : ComponentTemplate { public override void OnGizmos(Transform transform, GizmosMode mode) { if (mode == GizmosMode.Selected) { Gizmos.color = Color.yellow; Gizmos.DrawWireSphere(transform.position, component.radius); } } public override void OnValidate(UnityEngine.Object obj) { if (component.radius < 0) component.radius = 0; } } ``` -------------------------------- ### Connect Entities to GameObjects with Extension Methods (C#) Source: https://context7.com/dcfapixels/dragonecs-unity/llms.txt Demonstrates using `EcsConnect` extension methods to link ECS entities with Unity GameObjects, Components, or `EcsEntityConnect` instances. This facilitates bidirectional communication and data synchronization. ```csharp using DCFApixels.DragonECS; using UnityEngine; public class QuickConnectionExample : MonoBehaviour { private EcsDefaultWorld _world; private void Start() { _world = EcsDefaultWorldSingletonProvider.Instance.Get(); entlong entity = _world.NewEntityLong(); // Connect via GameObject extension gameObject.Connect(entity, applyTemplates: true); // Or connect via entity extension entlong anotherEntity = _world.NewEntityLong(); anotherEntity.Connect(this, applyTemplates: false); // Connect via Component // Connect to specific EcsEntityConnect var connect = GetComponent(); if (connect != null) { entity.Connect(connect, applyTemplates: true); } } } ``` -------------------------------- ### Use UnityDebugService in C# Source: https://context7.com/dcfapixels/dragonecs-unity/llms.txt This code snippet demonstrates the usage of `UnityDebugService` for debugging purposes within a Unity environment. It shows how to manually activate the service, print messages to the Unity console with color-coded tags, use profiler markers for performance monitoring, and pause play mode for debugging. ```csharp using DCFApixels.DragonECS; using UnityEngine; public class DebugExample : MonoBehaviour { private void Start() { // Manual activation (automatic in Editor) UnityDebugService.Activate(); // Print messages to Unity Console with color-coded tags EcsDebug.Print("Game started successfully"); EcsDebug.Print("pass", "All systems initialized"); // Green EcsDebug.Print("warning", "Low memory detected"); // Yellow EcsDebug.Print("error", "Failed to load resource"); // Red // Use profiler markers for performance monitoring var marker = new EcsProfilerMarker("HeavyOperation"); marker.Begin(); PerformHeavyCalculation(); marker.End(); // Shows timing in Unity Profiler // Pause play mode for debugging if (criticalErrorOccurred) { EcsDebug.Break(); } } private void PerformHeavyCalculation() { /* ... */ } private bool criticalErrorOccurred = false; } ``` -------------------------------- ### Connect GameObjects to Entities in C# Source: https://context7.com/dcfapixels/dragonecs-unity/llms.txt This code snippet demonstrates how to connect Unity GameObjects to ECS entities using the `EcsEntityConnect` MonoBehaviour. It shows how to create an entity, connect it to a GameObject, check the connection status, and disconnect when needed. It also covers the application of entity templates during the connection process. ```csharp using DCFApixels.DragonECS; using UnityEngine; public class EntitySpawner : MonoBehaviour { [SerializeField] private EcsEntityConnect _connect; private EcsDefaultWorld _world; private void Start() { _world = EcsDefaultWorldSingletonProvider.Instance.Get(); // Create entity and connect to GameObject with template application entlong entity = _world.NewEntityLong(); _connect.ConnectWith(entity, applyTemplates: true); // Check connection status if (_connect.IsConnected) { Debug.Log($"Connected to entity: {_connect.Entity}"); Debug.Log($"World: {_connect.World}"); } // Disconnect when needed (GameObjectConnect auto-removed) // _connect.Disconnect(); } private void OnDestroy() { // Entity can optionally be deleted with GameObject // Set _deleteEntityWithDestroy in inspector } } ``` -------------------------------- ### Integrate Debug Module in Unity with C# Source: https://context7.com/dcfapixels/dragonecs-unity/llms.txt This code snippet demonstrates how to integrate the debug module into the ECS pipeline within a Unity project. It shows how to initialize ECS worlds, create a pipeline, add systems, and enable Unity debugging for multiple worlds. The `AddUnityDebug` extension method connects the Unity debug service, enabling visual monitoring in the Unity Editor. ```csharp using DCFApixels.DragonECS; using UnityEngine; public class GameRoot : MonoBehaviour { private EcsDefaultWorld _world; private EcsEventWorld _eventWorld; private EcsPipeline _pipeline; private void Start() { _world = new EcsDefaultWorld(); _eventWorld = new EcsEventWorld(); _pipeline = EcsPipeline.New() .Add(new MovementSystem()) .Add(new RenderSystem()) // Enable Unity debugging for multiple worlds .AddUnityDebug(_world, _eventWorld) .BuildAndInit(); } private void Update() { _pipeline.Run(); } private void OnDestroy() { _pipeline.Destroy(); _world.Destroy(); _eventWorld.Destroy(); } } ``` -------------------------------- ### Create Entities with Associated GameObjects (C#) Source: https://context7.com/dcfapixels/dragonecs-unity/llms.txt Illustrates the `NewEntityWithGameObject` extension method for creating ECS entities and their corresponding Unity GameObjects simultaneously. It supports custom GameObject naming and optional editor icon customization. ```csharp using DCFApixels.DragonECS; using UnityEngine; public class GameObjectEntityCreator : MonoBehaviour { private EcsDefaultWorld _world; private void Start() { _world = EcsDefaultWorldSingletonProvider.Instance.Get(); // Create entity with new GameObject entlong player = _world.NewEntityWithGameObject("Player", GameObjectIcon.Circle_Blue); entlong enemy = _world.NewEntityWithGameObject("Enemy", GameObjectIcon.Diamond_Red); entlong collectible = _world.NewEntityWithGameObject("Coin", GameObjectIcon.Label_Yellow); // Available icon types: // GameObjectIcon.NONE - no icon // Label_Gray, Label_Blue, Label_Teal, Label_Green, Label_Yellow, Label_Orange, Label_Red, Label_Purple // Circle_Gray, Circle_Blue, Circle_Teal, Circle_Green, Circle_Yellow, Circle_Orange, Circle_Red, Circle_Purple // Diamond_Gray, Diamond_Blue, Diamond_Teal, Diamond_Green, Diamond_Yellow, Diamond_Orange, Diamond_Red, Diamond_Purple } } ``` -------------------------------- ### Unity Game Cycle System Integration with DragonECS Source: https://context7.com/dcfapixels/dragonecs-unity/llms.txt Integrates DragonECS systems with Unity's FixedUpdate, LateUpdate, and Gizmos drawing phases. Requires DragonECS and Unity packages. Systems hook into specific update phases using interfaces like IEcsFixedRunProcess, IEcsLateRunProcess, and IEcsGizmosProcess. ```csharp using DCFApixels.DragonECS; using UnityEngine; // System running in FixedUpdate (physics) public class PhysicsMovementSystem : IEcsFixedRunProcess, IEcsInject { private EcsDefaultWorld _world; public void Inject(EcsDefaultWorld world) => _world = world; public void FixedRun() { foreach (var e in _world.Where(out PhysicsAspect a)) { ref var rb = ref a.Rigidbodies.Get(e); ref readonly var velocity = ref a.Velocities.Read(e); rb.obj.velocity = velocity.value; } } class PhysicsAspect : EcsAspect { public EcsPool> Rigidbodies; public EcsPool Velocities; } } // System running in LateUpdate (camera follow, etc.) public class CameraFollowSystem : IEcsLateRunProcess, IEcsInject { private EcsDefaultWorld _world; private Camera _camera; public void Inject(EcsDefaultWorld world) => _world = world; public void LateRun() { foreach (var e in _world.Where(out CameraTargetAspect a)) { ref readonly var transform = ref a.Transforms.Read(e); _camera.transform.position = transform.obj.position + Vector3.back * 10f; } } class CameraTargetAspect : EcsAspect { public EcsPool> Transforms; public EcsPool Targets; } } // System for Editor gizmos drawing public class DebugGizmosSystem : IEcsGizmosProcess, IEcsInject { private EcsDefaultWorld _world; public void Inject(EcsDefaultWorld world) => _world = world; public void DrawGizmos() { foreach (var e in _world.Where(out GizmosAspect a)) { ref readonly var transform = ref a.Transforms.Read(e); ref readonly var detection = ref a.Detections.Read(e); Gizmos.color = Color.green; Gizmos.DrawWireSphere(transform.obj.position, detection.radius); } } class GizmosAspect : EcsAspect { public EcsPool> Transforms; public EcsPool Detections; } } [System.Serializable] public struct VelocityComponent : IEcsComponent { public Vector3 value; } public struct CameraTargetTag : IEcsTagComponent { } ``` -------------------------------- ### Access Unity GameObject from ECS via GameObjectConnect (C#) Source: https://context7.com/dcfapixels/dragonecs-unity/llms.txt Explains how the `GameObjectConnect` component, automatically added to entities connected to GameObjects, allows ECS systems to access the Unity `Transform` and other GameObject properties. This is crucial for systems that need to interact with the Unity scene. ```csharp using DCFApixels.DragonECS; using UnityEngine; public class GameObjectAccessSystem : IEcsRunProcess, IEcsInject { private EcsDefaultWorld _world; public void Inject(EcsDefaultWorld world) => _world = world; public void Run() { // Query entities with GameObjectConnect foreach (var e in _world.Where(out Aspect a)) { ref readonly var goConnect = ref a.GameObjects.Read(e); if (goConnect.IsConnected) { // Access the Unity GameObject through the connect EcsEntityConnect connect = goConnect.Connect; Transform transform = connect.transform; Debug.Log($"Entity {e} is at position {transform.position}"); } } } class Aspect : EcsAspect { public EcsPool GameObjects; } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.