Skip to content

Game.UI.InGame.IUniqueAssetTrackingSystem

Assembly: Assembly-CSharp
Namespace: Game.UI.InGame

Type: interface

Base: None

Summary:
Interface for tracking "unique" assets (Entities that must be unique/limited in the world) and for notifying listeners when the placement status of such an asset changes. Intended to be implemented by an ECS system or manager that owns a NativeParallelHashSet of placed unique asset Entities and exposes an event to notify about add/remove (or place/unplace) status changes.


Fields

  • This interface declares no backing fields. Implementations will typically have an internal NativeParallelHashSet and any necessary private state.

Properties

  • NativeParallelHashSet<Entity> placedUniqueAssets { get; }
    Returns the set of Entities currently considered "placed" unique assets. The property type is a NativeParallelHashSet (a value-type collection from Unity.Collections). Implementations are responsible for creating, managing, and disposing the set. Consumers should be aware that this returns the NativeParallelHashSet value; do not Dispose() it unless you own it.

  • Action<Entity, bool> EventUniqueAssetStatusChanged { get; set; }
    An event-like action invoked when a unique asset's placed status changes. Signature parameters:

  • Entity: the entity representing the unique asset.
  • bool: true when the asset was placed/activated, false when it was removed/deactivated. Implementers should invoke this action after successfully adding or removing from placedUniqueAssets. Subscribers can add or remove handlers to respond to changes.

Constructors

  • Interfaces do not define constructors. Implementations should initialize the NativeParallelHashSet (for example, in OnCreate) and dispose it when no longer needed (for example, in OnDestroy).

Methods

  • This interface defines no methods. Implementations typically provide methods to add/remove/check assets (and trigger EventUniqueAssetStatusChanged) in addition to complying with the property contract.

Usage Example

using System;
using Unity.Collections;
using Unity.Entities;

public class UniqueAssetTrackingSystem : SystemBase, Game.UI.InGame.IUniqueAssetTrackingSystem
{
    public NativeParallelHashSet<Entity> placedUniqueAssets { get; private set; }
    public Action<Entity, bool> EventUniqueAssetStatusChanged { get; set; }

    protected override void OnCreate()
    {
        base.OnCreate();
        // Initial capacity example; choose appropriate value for your mod
        placedUniqueAssets = new NativeParallelHashSet<Entity>(16, Allocator.Persistent);
    }

    protected override void OnDestroy()
    {
        if (placedUniqueAssets.IsCreated)
            placedUniqueAssets.Dispose();
        base.OnDestroy();
    }

    protected override void OnUpdate()
    {
        // Example usage inside system update: no-op here
    }

    // Convenience API for other code to call
    public void RegisterPlaced(Entity assetEntity)
    {
        if (!placedUniqueAssets.IsCreated)
            return;

        if (placedUniqueAssets.Add(assetEntity))
        {
            EventUniqueAssetStatusChanged?.Invoke(assetEntity, true);
        }
    }

    public void UnregisterPlaced(Entity assetEntity)
    {
        if (!placedUniqueAssets.IsCreated)
            return;

        if (placedUniqueAssets.Remove(assetEntity))
        {
            EventUniqueAssetStatusChanged?.Invoke(assetEntity, false);
        }
    }
}

// Example subscriber
public class SomeOtherSystem
{
    private Game.UI.InGame.IUniqueAssetTrackingSystem _tracker;

    public SomeOtherSystem(Game.UI.InGame.IUniqueAssetTrackingSystem tracker)
    {
        _tracker = tracker;
        _tracker.EventUniqueAssetStatusChanged += OnUniqueAssetStatusChanged;
    }

    private void OnUniqueAssetStatusChanged(Entity e, bool isPlaced)
    {
        // React to placement/removal
    }

    public void Dispose()
    {
        if (_tracker != null)
            _tracker.EventUniqueAssetStatusChanged -= OnUniqueAssetStatusChanged;
    }
}

Notes and best practices: - NativeParallelHashSet is a native container and must be disposed by the owner (use Allocator.Persistent for long-lived containers). Check IsCreated before accessing or disposing. - NativeParallelHashSet is a value type; returning it from a property returns the struct instance. Consumers should not assume ownership (avoid disposing it unless you created it). - If accessing or mutating the set from jobs, ensure proper thread-safety and use appropriate safety handles or perform operations inside Jobs that accept NativeParallelHashSet via the job system. Many Unity container methods are not thread-safe from managed threads without safety checks. - The bool parameter in EventUniqueAssetStatusChanged typically means "isPlaced" (true = placed/added, false = removed/unplaced).