Game.Prefabs.PrefabBase
Assembly:
Namespace: Game.Prefabs
Type: abstract class
Base: ComponentBase, ISerializationCallbackReceiver, IPrefabBase
Summary:
PrefabBase is the common abstract base for game prefabs. It manages a list of ComponentBase instances that define the prefab's behavior, provides serialization callbacks, helper methods to add/remove/lookup components, cloning, and some metadata (thumbnail URL, builtin detection via AssetDatabase, PrefabAsset reference). It also exposes a lightweight dirty flag used to mark changes that require saving or refresh.
Fields
-
public List<ComponentBase> components = new List<ComponentBase>();
This list holds the ComponentBase instances owned by the prefab. Components are created as ScriptableObject instances, stored here, and are used to compose prefab behavior. The list is cleaned up on enable to remove null entries. -
[NonSerialized] public bool isDirty = true;
Non-serialized flag indicating the prefab has been modified and may require saving or reprocessing. Many mutating operations set this to true.
Properties
-
public string thumbnailUrl { get; private set }
URL string used to fetch the prefab's thumbnail (constructed on enable). It is publicly readable but only set internally. -
public bool builtin { get; }
Determines whether this prefab is a built-in asset. Checks AssetDatabase.global.resources.prefabsMap for a GUID and falls back to checking asset.database type to infer built-in status. -
public PrefabAsset asset { get; set; }
Reference to the PrefabAsset metadata object associated with this prefab (can be null for some runtime-created prefabs). -
public virtual bool canIgnoreUnlockDependencies => true
Virtual property (default true) indicating whether this prefab can ignore unlock dependencies—can be overridden by derived prefabs. -
public virtual string uiTag => GetPrefabID().ToString()
Virtual property returning a UI tag string for the prefab; by default it uses the prefab ID.
Constructors
public PrefabBase()
No explicit constructor is defined in the class; the default parameterless constructor is used. Initialization of fields is done inline (e.g., components list) and in OnEnable/serialization callbacks.
Methods
-
public void OnBeforeSerialize()
Serialization callback (ISerializationCallbackReceiver). Present but empty — hook for pre-serialization logic in derived classes. -
public virtual void OnAfterDeserialize()
Serialization callback invoked after deserialization. Ensures base.prefab is set to this instance. -
protected override void OnEnable()
Called when the ScriptableObject is enabled. Sets base.prefab to this, verifies and fixes component.prefab references, removes null components, and constructs thumbnailUrl as "thumbnail://ThumbnailCamera/{Type}/{Name}". Logs errors if components reference other prefabs or are null. -
public virtual void Reset()
Marks the prefab as dirty. Can be extended to reset additional state. -
public T AddOrGetComponent<T>() where T : ComponentBase
Returns an existing component of type T if present (exact match), otherwise creates and adds a new component of that type. -
public ComponentBase AddOrGetComponent(Type type)
Non-generic version of AddOrGetComponent; operates with a System.Type. -
public T AddComponent<T>() where T : ComponentBase
Creates and adds a new component of type T. Throws if a component of that exact type already exists. -
public T AddComponentFrom<T>(T from) where T : ComponentBase
Adds (or gets) a component of type T and copies data from the provided instance using JsonUtility (serializes the source to JSON then overwrites the new component). -
public ComponentBase AddComponentFrom(ComponentBase from)
Non-generic version: ensures a component of the same concrete type as 'from' exists on the prefab and overwrites it with from's serialized data. -
public ComponentBase AddComponent(Type type)
Creates a new component ScriptableObject instance of the given type, sets its name and prefab, adds it to components, marks prefab dirty, and returns it. Throws InvalidOperationException if the exact type already exists. -
public ComponentBase ReplaceComponentWith(ComponentBase target, Type type)
Replaces a component instance in the components list (by index) with a newly created instance of the specified type, sets prefab on the new instance and marks the prefab dirty. Returns the new component. -
public void Remove<T>() where T : ComponentBase
Removes the first component matching the exact type T if present and marks the prefab dirty. -
public void Remove(Type type)
Removes the first component matching the exact System.Type if present and marks the prefab dirty. -
public bool Has<T>() where T : ComponentBase
Checks whether the prefab or any of its components contains an exact match for type T. -
public bool Has(Type type)
Checks whether the prefab itself or any component has an exact match for the specified type. -
public bool HasSubclassOf(Type type)
Checks whether the prefab type or any component type is a subclass of the provided type (useful to detect derived component types). -
public bool TryGet<T>(out T component) where T : ComponentBase
Attempts to find a component that is either of exact type T or a subclass; includes the prefab itself if it matches. Returns true and sets out parameter on success. -
public bool TryGet(Type type, out ComponentBase component)
Non-generic TryGet by System.Type: checks the prefab type and components for matches or subclasses. -
public bool TryGetExactly<T>(out T component) where T : ComponentBase
Attempts to find a component that is exactly of type T (no subclassing). Includes the prefab itself. -
public bool TryGetExactly(Type type, out ComponentBase component)
Non-generic exact-type lookup. -
public bool TryGet<T>(List<T> result)
Adds all active components (and the prefab itself if it matches) of type T (including subclass matches) to the provided list and returns whether any were added. -
public override void GetPrefabComponents(HashSet<ComponentType> components)
Populates the provided set with component types required on an entity that represents this prefab at runtime. Adds PrefabData and LoadedIndex read/write components. -
public override void GetArchetypeComponents(HashSet<ComponentType> components)
Adds archetype-level components required for entities using this prefab (adds PrefabRef read/write). -
public PrefabID GetPrefabID()
Returns a PrefabID struct constructed from this prefab instance. -
public PrefabBase Clone(string newName = null)
Creates a deep-ish clone of the prefab: creates a new instance of the same concrete type, serializes this prefab to JSON (using Colossal JSON proxy object to remove components and name override keys), then overwrites into the new instance. It then copies each component using AddComponentFrom. Sets the new name (optionally provided) and returns the cloned PrefabBase.
Usage Example
// Example derived prefab demonstrating common usage:
[Preserve]
public class MyPrefab : PrefabBase
{
protected override void OnEnable()
{
base.OnEnable();
// Ensure a component of MyComponent exists and configure it
var myComp = AddOrGetComponent<MyComponent>();
myComp.someValue = 42;
// Mark prefab dirty if you made changes that should be saved
isDirty = true;
}
public void DuplicatePrefab()
{
// Clone the prefab (creates a copy with components)
var copy = Clone(name + " (copy)");
// work with the copy...
}
}