### ECS World Initialization and Debugging Setup in C# Source: https://context7.com/felid-force-studios/staticecs-unity/llms.txt Initializes the Static ECS world and enables runtime debugging features. This C# script demonstrates creating a world, adding debug support with custom entity naming functions, registering components and events, and setting up systems. It also handles world destruction on object destruction. ```csharp using FFS.Libraries.StaticEcs; using FFS.Libraries.StaticEcs.Unity; using UnityEngine; public class EcsInitializer : MonoBehaviour { void Start() { // Create world World.Create(WorldConfig.Default()); // Enable debug with custom entity naming EcsDebug.AddWorld( eventHistoryCount: 2048, windowEntityNameFunction: entity => { if (entity.Has()) return $"Unit_{entity.Id()}"; return $"Entity_{entity.Id()}"; } ); // Register components World.RegisterComponent(); World.RegisterComponent(); World.RegisterEvent(); World.Initialize(); // Setup systems World.Systems.Create(); World.Systems.Initialize(); EcsDebug.AddSystem(); } void OnDestroy() { World.Destroy(); } } ``` -------------------------------- ### Configure Entities in Unity Scene with StaticEcsEntityProvider Source: https://context7.com/felid-force-studios/staticecs-unity/llms.txt The StaticEcsEntityProvider MonoBehaviour facilitates configuring ECS entities directly within the Unity scene. It supports automatic or manual entity creation, prefab references, component data setup, and tags. This allows for visual entity management and simplifies the process of defining entity structures. ```csharp using FFS.Libraries.StaticEcs; using FFS.Libraries.StaticEcs.Unity; using UnityEngine; // Define components public struct Position : IComponent { [StaticEcsEditorTableValue] // Display in entity table public Vector3 Val; } public struct Health : IComponent { [StaticEcsEditorTableValue(columnWidth: 80f)] public float Current; public float Max; } public struct PlayerTag : ITag { } // Manual entity creation from provider public class EntitySpawner : MonoBehaviour { [SerializeField] private StaticEcsEntityProvider entityProvider; void SpawnEntity() { // Set provider to manual mode in inspector, then call: if (entityProvider.CreateEntity()) { // Access the created entity IEntity entity = entityProvider.Entity; EntityGID gid = entityProvider.EntityGid; Debug.Log($"Created entity with GID: {gid}"); } } // Attach provider to existing entity void AttachToEntity(IEntity existingEntity, GameObject targetObject) { StaticEcsEntityProvider.AttachEntityProvider(targetObject, existingEntity); } } // IOnProvideComponent interface for initialization callbacks public struct GameObjectRef : IComponent, IOnProvideComponent { public GameObject GameObject; public void OnProvide(GameObject go) { GameObject = go; // Automatically set when entity is created } } ``` -------------------------------- ### Burst-Compatible System for Optimized Iterations in C# Source: https://github.com/felid-force-studios/staticecs-unity/blob/master/README.md Defines a Burst-compatible system for highly optimized entity iterations. It requires Burst installation and enabling unsafe code. The system includes methods for processing single entities (`InvokeOne`) and blocks of entities (`InvokeBlock`), with configurable parameters for filtering and parallel execution. ```csharp public unsafe struct UpdateEntitiesBurstSystem : IUpdateSystem { private const EntityStatusType entityStatusType = EntityStatusType.Enabled; private const ComponentStatus componentStatus = ComponentStatus.Enabled; private static readonly ushort[] clusters = Array.Empty(); // Empty == all clusters private static readonly WithNothing with = default; private static bool parallel; private static uint minEntitiesPerThread; // default - 64 private static uint workersLimit; // default - max threads [MethodImpl(AggressiveInlining)] public void BeforeUpdate() { } [MethodImpl(AggressiveInlining)] public void AfterUpdate() { } [MethodImpl(AggressiveInlining)] private void InvokeOne(ref Position position, ref Direction direction, int worker) { // TODO Write a function for processing a single entity here } [MethodImpl(AggressiveInlining)] private void InvokeBlock(Position* positions, Direction* directions, int worker, uint dataOffest) { // Here, custom optimization of the entire entity block is possible. (SIMD, unroll, etc.) for (var i = 0; i < Const.ENTITIES_IN_BLOCK; i++) { var dIdx = i + dataOffest; InvokeOne(ref positions[dIdx], ref directions[dIdx], worker); } } #region GENERATED // ... #endregion } ``` -------------------------------- ### Ignore Event in Editor (C#) Source: https://github.com/felid-force-studios/staticecs-unity/blob/master/README.md Use the `StaticEcsIgnoreEvent` attribute to exclude specific events from being displayed or processed in the editor. This is useful for events that do not require visual representation or interaction in the editor. The example ignores `DamageEvent`. ```csharp [StaticEcsIgnoreEvent] public struct DamageEvent : IEvent { } ``` -------------------------------- ### Display Event Data in Editor Table (C#) Source: https://github.com/felid-force-studios/staticecs-unity/blob/master/README.md Use the `StaticEcsEditorTableValue` attribute to display event data in the editor table. This attribute is applied to a property or field within an event struct. The example shows how to display damage information. ```csharp public struct DamageEvent : IEvent { public float Val; [StaticEcsEditorTableValue] public string ShowData => $"Damage {Val}"; } ``` -------------------------------- ### Set Event Color in Editor (C#) Source: https://github.com/felid-force-studios/staticecs-unity/blob/master/README.md The `StaticEcsEditorColor` attribute allows you to define a custom color for events displayed in the editor. Colors can be specified using RGB or HEX values. The example sets a red color for `DamageEvent`. ```csharp [StaticEcsEditorColor("f7796a")] public struct DamageEvent : IEvent { } ``` -------------------------------- ### Automatic Component Registration with AutoRegister in StaticECS Unity Source: https://context7.com/felid-force-studios/staticecs-unity/llms.txt Explains the use of `AutoRegister` for automatically discovering and invoking static methods marked with `StaticEcsAutoRegistration`. This facilitates modular registration of components and systems across different assemblies or files, simplifying startup logic. ```csharp using FFS.Libraries.StaticEcs; using FFS.Libraries.StaticEcs.Unity; public struct GameWorldType : IWorldType { } // In separate files/assemblies, mark registration methods public static class PlayerComponents { [StaticEcsAutoRegistration(typeof(GameWorldType))] public static void RegisterComponents() { World.RegisterComponent(); World.RegisterComponent(); World.RegisterComponent(); } } public static class EnemyComponents { [StaticEcsAutoRegistration(typeof(GameWorldType))] public static void RegisterEnemyComponents() { World.RegisterComponent(); World.RegisterComponent(); } } // At startup, call Apply to invoke all registered methods public class GameBootstrap : MonoBehaviour { void Awake() { World.Create(WorldConfig.Default()); // Automatically calls all methods marked with [StaticEcsAutoRegistration(typeof(GameWorldType))] AutoRegister.Apply(); World.Initialize(); } } ``` -------------------------------- ### Configure and Send Events with StaticEcsEventProvider in Unity Source: https://context7.com/felid-force-studios/staticecs-unity/llms.txt Demonstrates how to define custom events (e.g., DamageEvent) and trigger them from scene objects using StaticEcsEventProvider. It shows manual event configuration, sending events, and accessing runtime status. Events can be ignored by editor tracking using [StaticEcsIgnoreEvent]. ```csharp using FFS.Libraries.StaticEcs; using FFS.Libraries.StaticEcs.Unity; using UnityEngine; // Define an event [StaticEcsEditorColor("f7796a")] // Orange color in editor public struct DamageEvent : IEvent { public float Amount; public int TargetId; [StaticEcsEditorTableValue] public string DisplayText => $"Damage: {Amount} to {TargetId}"; } // Trigger event provider manually public class DamageTrigger : MonoBehaviour { [SerializeField] private StaticEcsEventProvider eventProvider; void OnTriggerEnter(Collider other) { // Configure event data before sending if (eventProvider.EventTemplate is DamageEvent damageEvent) { damageEvent.Amount = 25f; damageEvent.TargetId = other.GetInstanceID(); eventProvider.EventTemplate = damageEvent; } // Send the event if (eventProvider.SendEvent()) { // Access event status RuntimeEvent runtimeEvent = eventProvider.RuntimeEvent; Debug.Log($"Event sent, status: {runtimeEvent.Status}"); } } } // Event ignored by editor tracking [StaticEcsIgnoreEvent] public struct InternalTickEvent : IEvent { public float DeltaTime; } ``` -------------------------------- ### Implement Burst System for Entity Processing in Unity Source: https://context7.com/felid-force-studios/staticecs-unity/llms.txt This C# code demonstrates how to create a Burst-compiled system for processing entities in Unity using Static ECS. It includes methods for single-entity processing (`InvokeOne`) and optional SIMD-optimized block processing (`InvokeBlock`). Ensure components are unmanaged for Burst compatibility. ```csharp using System; using System.Runtime.CompilerServices; using FFS.Libraries.StaticEcs; using UnityEngine; using static System.Runtime.CompilerServices.MethodImplOptions; // Components must be unmanaged for Burst compatibility public struct Position : IComponent { public Vector3 Value; } public struct Velocity : IComponent { public Vector3 Value; } // Burst-compatible movement system public unsafe struct MovementBurstSystem : IUpdateSystem { // Entity filtering configuration private const EntityStatusType entityStatusType = EntityStatusType.Enabled; private const ComponentStatus componentStatus = ComponentStatus.Enabled; private static readonly ushort[] clusters = Array.Empty(); // Empty = all clusters private static readonly WithNothing with = default; // Parallel execution settings private static bool parallel = true; private static uint minEntitiesPerThread = 64; private static uint workersLimit = 0; // 0 = use all available threads [MethodImpl(AggressiveInlining)] public void BeforeUpdate() { // Called before iteration begins } [MethodImpl(AggressiveInlining)] public void AfterUpdate() { // Called after iteration completes } // Process single entity - called for each matching entity [MethodImpl(AggressiveInlining)] private void InvokeOne(ref Position position, ref Velocity velocity, int worker) { position.Value += velocity.Value * Time.deltaTime; } // Optional: Custom block processing for SIMD optimization [MethodImpl(AggressiveInlining)] private void InvokeBlock(Position* positions, Velocity* velocities, int worker, uint dataOffset) { // Process entities in blocks of 64 for cache efficiency for (var i = 0; i < Const.ENTITIES_IN_BLOCK; i++) { var idx = i + dataOffset; InvokeOne(ref positions[idx], ref velocities[idx], worker); } } // Generated region would go here (generated by template) } ``` -------------------------------- ### Integrate ECS Debugging with Unity Editor Source: https://context7.com/felid-force-studios/staticecs-unity/llms.txt Connect your ECS worlds and systems to Unity's editor debugging tools using the EcsDebug class. Call AddWorld() before world initialization and AddSystem() after system initialization to enable runtime monitoring. This integration allows for visual inspection and debugging of ECS data within the Unity editor. ```csharp using FFS.Libraries.StaticEcs; using FFS.Libraries.StaticEcs.Unity; // Define your world type public struct ClientWorldType : IWorldType { } // Define your systems type public struct ClientSystemsType : ISystemsType { } public class GameStartup : MonoBehaviour { void Start() { // Create and configure the world World.Create(WorldConfig.Default()); // Connect world to editor BEFORE initialization // Optional: eventHistoryCount controls how many events are tracked (default 8192) // Optional: windowEntityNameFunction provides custom entity naming in the editor EcsDebug.AddWorld( eventHistoryCount: 4096, windowEntityNameFunction: entity => $"Entity_{entity.Id()}" ); // Initialize the world World.Initialize(); // Create and initialize systems World.Systems.Create(); World.Systems.Initialize(); // Connect systems to editor AFTER initialization EcsDebug.AddSystem(); } } ``` -------------------------------- ### Connect ECS Worlds and Systems to Unity Editor Debugger Source: https://github.com/felid-force-studios/staticecs-unity/blob/master/README.md This C# code demonstrates how to connect ECS worlds and systems to the Unity editor's debugging window. It involves calling specific methods during the initialization phase of worlds and systems to enable runtime monitoring and management. ```csharp ClientWorld.Create(WorldConfig.Default()); //... EcsDebug.AddWorld(); // Between creation and initialization //... ClientWorld.Initialize(); ClientSystems.Create(); //... ClientSystems.Initialize(); EcsDebug.AddSystem(); // After initialization ``` -------------------------------- ### Customize Component Display with StaticECS Unity Editor Attributes Source: https://context7.com/felid-force-studios/staticecs-unity/llms.txt Illustrates how to use StaticECS Unity editor attributes to customize the appearance and behavior of components in the Unity editor. This includes setting display names, colors, controlling visibility of fields, and defining table value properties like column width. ```csharp using FFS.Libraries.StaticEcs; using FFS.Libraries.StaticEcs.Unity; using UnityEngine; // Custom display name for component [StaticEcsEditorName("Player Velocity", "Components.Movement.Velocity")] // Custom color (RGB 0-255, float 0-1, or hex string) [StaticEcsEditorColor(100, 200, 150)] public struct Velocity : IComponent { // Show in entity table with custom column width [StaticEcsEditorTableValue(columnWidth: 120f)] public Vector3 Direction; [StaticEcsEditorTableValue] public float Speed; // Show private field in editor [StaticEcsEditorShow] private float _internalTimer; // Hide public field from editor [StaticEcsEditorHide] public int CacheValue; // Read-only during play mode [StaticEcsEditorRuntimeReadOnly] public int InitialSeed; } // Hex color format [StaticEcsEditorColor("#4A90D9")] public struct Shield : IComponent { [StaticEcsEditorTableValue] public float Amount; } // Compact view for simple components [StaticEcsEditorCompactView] public struct SimpleFlag : IComponent { public bool IsActive; } ``` -------------------------------- ### Create Custom Editor Drawer for Static ECS Types in Unity Source: https://context7.com/felid-force-studios/staticecs-unity/llms.txt This C# code shows how to implement `IStaticEcsValueDrawer` to create custom inspectors for specific data types within the Unity editor using Static ECS. Place the drawer script in an 'Editor' folder for automatic registration. It handles drawing the value in the inspector and in table views. ```csharp using System.Globalization; using FFS.Libraries.StaticEcs.Unity.Editor; using UnityEditor; using UnityEngine; // Custom drawer for a game-specific type public struct ResourceAmount { public int Gold; public int Wood; public int Stone; } // Editor drawer (place in Editor folder) public sealed class ResourceAmountDrawer : IStaticEcsValueDrawer { public override bool DrawValue(ref DrawContext ctx, string label, ref ResourceAmount value) { EditorGUILayout.BeginVertical(GUI.skin.box); EditorGUILayout.LabelField(label, EditorStyles.boldLabel); EditorGUILayout.BeginHorizontal(); EditorGUILayout.PrefixLabel("Gold"); int newGold = EditorGUILayout.IntField(value.Gold); EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.PrefixLabel("Wood"); int newWood = EditorGUILayout.IntField(value.Wood); EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.PrefixLabel("Stone"); int newStone = EditorGUILayout.IntField(value.Stone); EditorGUILayout.EndHorizontal(); EditorGUILayout.EndVertical(); bool changed = newGold != value.Gold || newWood != value.Wood || newStone != value.Stone; if (changed) { value = new ResourceAmount { Gold = newGold, Wood = newWood, Stone = newStone }; } return changed; } public override void DrawTableValue(ref ResourceAmount value, GUIStyle style, GUILayoutOption[] layoutOptions) { string display = $"G:{value.Gold} W:{value.Wood} S:{value.Stone}"; EditorGUILayout.SelectableLabel(display, style, layoutOptions); } } ``` -------------------------------- ### Custom Type Drawing Method (C#) Source: https://github.com/felid-force-studios/staticecs-unity/blob/master/README.md Implement custom drawing methods for specific types or components by creating a class that inherits from `IStaticEcsValueDrawer` or `IStaticEcsValueDrawer` within your project's Editor folder. The `DrawValue` method handles display in viewers, and `DrawTableValue` handles display in tables. ```csharp public sealed class IntDrawer : IStaticEcsValueDrawer { public override bool DrawValue(ref DrawContext ctx, string label, ref int value) { BeginHorizontal(); PrefixLabel(label); var newValue = IntField(GUIContent.none, value); EndHorizontal(); if (newValue == value) { return false; } value = newValue; return true; } public override void DrawTableValue(ref int value, GUIStyle style, GUILayoutOption[] layoutOptions) { SelectableLabel(value.ToString(CultureInfo.InvariantCulture), style, layoutOptions); } } ``` -------------------------------- ### Displaying Component Data in Static ECS Entity Table (C#) Source: https://github.com/felid-force-studios/staticecs-unity/blob/master/README.md Demonstrates how to configure components to display their data within the Static ECS entity table view. The `StaticEcsEditorTableValue` attribute is used on fields or properties to make them visible in the table. ```csharp public struct Position : IComponent { [StaticEcsEditorTableValue] public Vector3 Val; } ``` -------------------------------- ### ECS Component and Event Definition in C# Source: https://context7.com/felid-force-studios/staticecs-unity/llms.txt Defines custom components (TransformComponent, HealthComponent) and events (SpawnEvent) for the Static ECS framework. Uses attributes like [StaticEcsEditorName], [StaticEcsEditorColor], and [StaticEcsEditorTableValue] to control their appearance and behavior in the editor's debugging window. These structures implement IComponent or IEvent interfaces. ```csharp using FFS.Libraries.StaticEcs; using FFS.Libraries.StaticEcs.Unity; using UnityEngine; // Example component setup for optimal editor display [StaticEcsEditorName("Transform")] [StaticEcsEditorColor(66, 133, 244)] public struct TransformComponent : IComponent { [StaticEcsEditorTableValue] public Vector3 Position; [StaticEcsEditorTableValue] public Quaternion Rotation; public Vector3 Scale; } [StaticEcsEditorName("Health")] [StaticEcsEditorColor(234, 67, 53)] public struct HealthComponent : IComponent { [StaticEcsEditorTableValue] public float Current; [StaticEcsEditorTableValue] public float Max; [StaticEcsEditorTableValue] public string Percent => $"{(Current / Max * 100f):F0}%"; } // Event with table display [StaticEcsEditorColor(251, 188, 5)] public struct SpawnEvent : IEvent { public Vector3 Position; public int EntityType; [StaticEcsEditorTableValue] public string Info => $"Spawn type {EntityType} at {Position}"; } ``` -------------------------------- ### Setting Component Colors in Static ECS Entity Table (C#) Source: https://github.com/felid-force-studios/staticecs-unity/blob/master/README.md Illustrates how to set a specific color for a component's display in the Static ECS entity table. The `StaticEcsEditorColor` attribute can be used with RGB or HEX color values to visually distinguish components. ```csharp [StaticEcsEditorColor("f7796a")] public struct Velocity : IComponent { [StaticEcsEditorTableValue] public float Val; } ``` -------------------------------- ### Controlling Field Visibility and Editability in Static ECS Entity Viewer (C#) Source: https://github.com/felid-force-studios/staticecs-unity/blob/master/README.md Explains how to manage the visibility and editability of fields within the Static ECS entity viewer window. Attributes like `StaticEcsEditorShow` (for private fields), `StaticEcsEditorHide` (for public fields), and `StaticEcsEditorRuntimeReadOnly` (to disable editing in play mode) are used. ```csharp public struct SomeComponent : IComponent { [StaticEcsEditorShow] [StaticEcsEditorRuntimeReadOnly] private int _showData; [StaticEcsEditorHide] public int HideData; } ``` -------------------------------- ### Customizing Component Names in Static ECS Entity Table (C#) Source: https://github.com/felid-force-studios/staticecs-unity/blob/master/README.md Shows how to assign a custom display name to a component in the Static ECS entity table. The `StaticEcsEditorName` attribute is applied to the component struct, allowing for more descriptive labels than the default struct name. ```csharp [StaticEcsEditorName("My velocity")] public struct Velocity : IComponent { [StaticEcsEditorTableValue] public float Val; } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.