Game.Prefabs.ComponentBase
Assembly: Assembly-CSharp (typical for game/mod code)
Namespace: Game.Prefabs
Type: abstract class
Base: UnityEngine.ScriptableObject, implements IComponentBase, System.IComparable
Summary:
Base class for prefab components used by the game's prefab system. ComponentBase is a ScriptableObject that can be attached to a PrefabBase instance (via its prefab property). It provides basic lifecycle hooks, helper methods for querying other components on the same prefab, and contracts for exposing ECS component types (via abstract methods). It also contains a static logger used by derived types.
Fields
-
protected static ILog baseLog
Static logger instance used by ComponentBase and derived classes. It is initialized in OnEnable() via LogManager.GetLogger("SceneFlow"). Because it is static, it is shared across all ComponentBase instances. -
public bool active = true
Flag indicating whether this component is active. Commonly used by systems that enumerate components to skip inactive ones.
Properties
-
public virtual bool ignoreUnlockDependencies { get; }
Virtual read-only property (defaults to false). Derived classes can override to indicate that this component ignores unlock/dependency checks. -
public PrefabBase prefab { get; set; }
Reference to the owning PrefabBase. Many helper methods on ComponentBase use this property; it must be set before calling methods that query the prefab. If prefab is null when calling such methods, a NullReferenceException is thrown. -
public virtual IEnumerable<string> modTags { get; }
Virtual property returning mod tags associated with this component. Default implementation returns Enumerable.Empty(). Derived classes can override to advertise tags.
Constructors
public ComponentBase()
Implicit default constructor. As a ScriptableObject-derived type, instances are typically created/instantiated by Unity or the prefab system. No special construction logic is present in this class.
Methods
-
public virtual string GetDebugString()
Returns a simple debug string for the component. Default implementation returns the component's runtime type name (GetType().Name). Can be overridden to provide more detailed debug information. -
protected virtual void OnEnable()
Unity/ScriptableObject lifecycle callback invoked when the ScriptableObject is enabled. Default implementation initializes the static baseLog by calling LogManager.GetLogger("SceneFlow"). -
protected virtual void OnDisable()
Unity/ScriptableObject lifecycle callback invoked when the ScriptableObject is disabled. Default implementation does nothing; override to clean up resources. -
public T GetComponent<T>() where T : ComponentBase
Helper to fetch another component of type T from the same prefab. Throws NullReferenceException if prefab is null. Internally calls prefab.TryGet(out var component). Returns the component or null if not present. -
public ComponentBase GetComponentExactly(Type type)
Fetches a component from the prefab whose concrete type matches the given Type (exact match). Throws NullReferenceException if prefab is null. Uses prefab.TryGetExactly(type, out component). Returns the component or null if not present. -
public bool GetComponents<T>(List<T> list) where T : ComponentBase
Populates the provided list with components of type T found on the same prefab. Throws NullReferenceException if prefab is null. Returns the result of prefab.TryGet(list) (typically true if anything was added/operation succeeded). -
public virtual void GetDependencies(List<PrefabBase> prefabs)
Virtual hook for collecting dependent prefabs. Default does nothing. Derived components can add dependent prefabs to the supplied list. -
public virtual void Initialize(EntityManager entityManager, Entity entity)
Virtual ECS initialization hook invoked to set up entity components. Default implementation is empty. Derived components should add their data/components to the Entity if needed. -
public virtual void LateInitialize(EntityManager entityManager, Entity entity)
Virtual late-initialization hook for ECS, invoked after Initialize. Default implementation is empty. -
public abstract void GetPrefabComponents(HashSet<ComponentType> components)
Abstract method that derived classes must implement to declare which ECS component types the prefab will have (used when creating archetypes or for other ECS bookkeeping). -
public abstract void GetArchetypeComponents(HashSet<ComponentType> components)
Abstract method that derived classes must implement to declare which ECS component types should be present on archetypes that include this component. -
public int CompareTo(object obj)
Implements IComparable. Compares this ComponentBase to another object. If obj is null returns 1. If obj is a ComponentBase, comparison is based on base.name (the ScriptableObject name). If obj is not a ComponentBase, throws ArgumentException("Object is not a ComponentBase").
Usage Example
// Example of creating a concrete component type and using the base functionality.
[Serializable]
public class MyComponent : ComponentBase
{
protected override void OnEnable()
{
base.OnEnable(); // initializes baseLog
baseLog.Info("MyComponent enabled");
}
public override void GetPrefabComponents(HashSet<ComponentType> components)
{
// Add required ECS ComponentType(s) for this prefab
}
public override void GetArchetypeComponents(HashSet<ComponentType> components)
{
// Add ECS ComponentType(s) for archetype
}
}
// Example usage when you have a prefab reference:
var myComponent = prefab.TryGet<MyComponent>(out var comp) ? comp : null;
if (myComponent != null)
{
Debug.Log(myComponent.GetDebugString());
}
Notes: - Many methods rely on the prefab property being non-null. Callers and derived types should ensure prefab is set before invoking helpers like GetComponent or GetComponents. - Derived classes must implement GetPrefabComponents and GetArchetypeComponents to participate properly in the ECS/archetype creation process.