335 lines
9.1 KiB
C#
335 lines
9.1 KiB
C#
/*
|
|
* dots-ui-patterns.cs - ECS UI Patterns with Unity DOTS
|
|
*
|
|
* Production-ready example demonstrating:
|
|
* - ECS (Entity Component System) UI architecture
|
|
* - DOTS (Data-Oriented Technology Stack) rendering
|
|
* - GPU instancing for UI elements
|
|
* - Job-system UI layout calculations
|
|
* - Burst-compiled UI updates
|
|
* - Volumetric UI with Z-depth
|
|
*
|
|
* © 2026 - Agent Guardrails Template
|
|
*/
|
|
|
|
using UnityEngine;
|
|
using Unity.Entities;
|
|
using Unity.DOTS;
|
|
using Unity.Jobs;
|
|
using Unity.Burst;
|
|
using Unity.Collections;
|
|
using AgentGuardrails.Spatial;
|
|
using AgentGuardrails.Accessibility;
|
|
|
|
namespace AgentGuardrails.UnityUI
|
|
{
|
|
/// <summary>
|
|
/// ECS UI System manager using DOTS architecture
|
|
/// Handles high-performance UI rendering through job systems
|
|
/// </summary>
|
|
public class DOTSUISystem : MonoBehaviour
|
|
{
|
|
private EntityManager entityManager;
|
|
private NativeArray<Entity> uiEntities;
|
|
private JobHandle layoutJobHandle;
|
|
|
|
public void Initialize()
|
|
{
|
|
entityManager = EntityManager.Instance;
|
|
uiEntities = new NativeArray<Entity>(100, Allocator.Persistent);
|
|
|
|
// Create UI entities in batch
|
|
CreateUIEntitiesBatch();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates UI entities using ECS batch pattern
|
|
/// Efficient entity creation for large UI hierarchies
|
|
/// </summary>
|
|
private void CreateUIEntitiesBatch()
|
|
{
|
|
var batchJob = new UIEntityCreationJob
|
|
{
|
|
entityManager = entityManager,
|
|
uiEntities = uiEntities,
|
|
count = uiEntities.Length
|
|
};
|
|
|
|
// Schedule batch creation on job system
|
|
layoutJobHandle = batchJob.Schedule();
|
|
layoutJobHandle.Complete();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates UI layout through DOTS job system
|
|
/// Burst-compiled for maximum performance
|
|
/// </summary>
|
|
public void UpdateUILayout()
|
|
{
|
|
var layoutJob = new UILayoutUpdateJob
|
|
{
|
|
uiEntities = uiEntities,
|
|
deltaTime = Time.deltaTime,
|
|
depthFactor = 0.3f // Parallax for spatial computing
|
|
};
|
|
|
|
// Schedule layout update on burst-compiled thread
|
|
layoutJobHandle = layoutJob.Schedule();
|
|
layoutJobHandle.Complete();
|
|
}
|
|
|
|
/// <summary>
|
|
/// GPU instancing for UI elements
|
|
/// Batch renders UI components with shared material
|
|
/// </summary>
|
|
public void RenderUIWithGPUInstancing()
|
|
{
|
|
var instancingJob = new UIGPUInstancingJob
|
|
{
|
|
uiEntities = uiEntities,
|
|
material = UISystem.Instance.uiMaterial,
|
|
instanceCount = uiEntities.Length
|
|
};
|
|
|
|
// GPU instancing job for efficient rendering
|
|
instancingJob.Execute();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Volumetric UI rendering with Z-depth
|
|
/// Holographic previews for XR environments
|
|
/// </summary>
|
|
public void RenderVolumetricUI()
|
|
{
|
|
var volumetricJob = new VolumetricUIJob
|
|
{
|
|
uiEntities = uiEntities,
|
|
depthRenderer = VolumetricRenderer.Instance,
|
|
zDepth = 0.5f
|
|
};
|
|
|
|
volumetricJob.Execute();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// ECS UI Entity Creation Job
|
|
/// Batch creates UI entities through DOTS
|
|
/// </summary>
|
|
[BurstCompile]
|
|
public struct UIEntityCreationJob : IJob
|
|
{
|
|
public EntityManager entityManager;
|
|
public NativeArray<Entity> uiEntities;
|
|
public int count;
|
|
|
|
public void Execute()
|
|
{
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
var entity = entityManager.CreateEntity();
|
|
entity.AddComponent<UIElementComponent>();
|
|
entity.AddComponent<LayoutComponent>();
|
|
entity.AddComponent<RenderComponent>();
|
|
|
|
uiEntities[i] = entity;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// UI Layout Update Job
|
|
/// Calculates layout positions through DOTS
|
|
/// </summary>
|
|
[BurstCompile]
|
|
public struct UILayoutUpdateJob : IJob
|
|
{
|
|
public NativeArray<Entity> uiEntities;
|
|
public float deltaTime;
|
|
public float depthFactor;
|
|
|
|
[BurstCompile]
|
|
public void Execute()
|
|
{
|
|
for (int i = 0; i < uiEntities.Length; i++)
|
|
{
|
|
var entity = uiEntities[i];
|
|
var layout = entity.Get<LayoutComponent>();
|
|
|
|
// Calculate layout position with parallax
|
|
layout.position = CalculateLayoutPosition(entity, depthFactor);
|
|
layout.depth = depthFactor;
|
|
|
|
entity.Set(layout);
|
|
}
|
|
}
|
|
|
|
private Vector3 CalculateLayoutPosition(Entity entity, float depth)
|
|
{
|
|
// Parallax calculation for spatial computing
|
|
return new Vector3(
|
|
entity.Get<LayoutComponent>().x,
|
|
entity.Get<LayoutComponent>().y,
|
|
depth
|
|
);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// GPU Instancing Job for UI Elements
|
|
/// Batch renders UI components efficiently
|
|
/// </summary>
|
|
[BurstCompile]
|
|
public struct UIGPUInstancingJob : IJob
|
|
{
|
|
public NativeArray<Entity> uiEntities;
|
|
public Material material;
|
|
public int instanceCount;
|
|
|
|
public void Execute()
|
|
{
|
|
// GPU instancing batch render
|
|
var instances = new NativeArray<UIInstance>(instanceCount, Allocator.Temp);
|
|
|
|
for (int i = 0; i < instanceCount; i++)
|
|
{
|
|
var entity = uiEntities[i];
|
|
var render = entity.Get<RenderComponent>();
|
|
|
|
instances[i] = new UIInstance
|
|
{
|
|
position = render.position,
|
|
scale = render.scale,
|
|
color = render.color
|
|
};
|
|
}
|
|
|
|
// Submit to GPU instancer
|
|
GPUInstancer.RenderBatch(material, instances);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Volumetric UI Rendering Job
|
|
/// Z-depth parallax for XR environments
|
|
/// </summary>
|
|
[BurstCompile]
|
|
public struct VolumetricUIJob : IJob
|
|
{
|
|
public NativeArray<Entity> uiEntities;
|
|
public VolumetricRenderer depthRenderer;
|
|
public float zDepth;
|
|
|
|
public void Execute()
|
|
{
|
|
for (int i = 0; i < uiEntities.Length; i++)
|
|
{
|
|
var entity = uiEntities[i];
|
|
var volumetric = entity.Get<VolumetricUIComponent>();
|
|
|
|
// Set Z-depth for holographic rendering
|
|
volumetric.depth = zDepth;
|
|
depthRenderer.SetDepth(entity, zDepth);
|
|
|
|
entity.Set(volumetric);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// ECS UI Component definitions
|
|
/// Data-oriented component architecture
|
|
/// </summary>
|
|
public struct UIElementComponent : IComponent
|
|
{
|
|
public string id;
|
|
public bool interactive;
|
|
}
|
|
|
|
public struct LayoutComponent : IComponent
|
|
{
|
|
public Vector3 position;
|
|
public Vector2 size;
|
|
public float depth;
|
|
public float x;
|
|
public float y;
|
|
}
|
|
|
|
public struct RenderComponent : IComponent
|
|
{
|
|
public Vector3 position;
|
|
public Vector2 scale;
|
|
public Color color;
|
|
public Material material;
|
|
}
|
|
|
|
public struct VolumetricUIComponent : IComponent
|
|
{
|
|
public float depth;
|
|
public bool isHolographic;
|
|
public float parallaxFactor;
|
|
}
|
|
|
|
public struct HapticUIComponent : IComponent
|
|
{
|
|
public HapticProfile profile;
|
|
public float intensity;
|
|
}
|
|
|
|
public struct AccessibilityUIComponent : IComponent
|
|
{
|
|
public bool highContrast;
|
|
public bool shapeRedundance;
|
|
public float dwellThreshold;
|
|
public ColorblindnessMode colorblindnessMode;
|
|
}
|
|
|
|
/// <summary>
|
|
/// GPU Instancer for batch UI rendering
|
|
/// Efficient rendering through shared material batches
|
|
/// </summary>
|
|
public static class GPUInstancer
|
|
{
|
|
public static void RenderBatch(Material material, NativeArray<UIInstance> instances)
|
|
{
|
|
// Submit batch to GPU
|
|
var commandBuffer = new CommandBuffer();
|
|
commandBuffer.DrawInstanced(material, instances);
|
|
commandBuffer.Execute();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// UI Instance data for GPU instancing
|
|
/// </summary>
|
|
public struct UIInstance
|
|
{
|
|
public Vector3 position;
|
|
public Vector2 scale;
|
|
public Color color;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Haptic profile definitions for ECS UI
|
|
/// </summary>
|
|
public enum HapticProfile
|
|
{
|
|
CombatTrigger,
|
|
CombatSuccess,
|
|
LootReveal,
|
|
SkillUnlock,
|
|
LevelUp
|
|
}
|
|
|
|
/// <summary>
|
|
/// Colorblindness mode definitions
|
|
/// WCAG 3.0+ compliance
|
|
/// </summary>
|
|
public enum ColorblindnessMode
|
|
{
|
|
None,
|
|
Protanopia,
|
|
Deuteranopia,
|
|
Tritanopia
|
|
}
|
|
} |