Game.Prefabs.RenderPrefab
Assembly: Assembly-CSharp
Namespace: Game.Prefabs
Type: class
Base: RenderPrefabBase
Summary:
RenderPrefab is a Unity component/prefab class that links geometry (meshes) and surface assets (materials/textures) for rendering. It holds serialized references to a GeometryAsset and a list of SurfaceAsset references, stores bounding and mesh/statistics data (index/vertex/mesh counts and surface area), and provides runtime helpers to obtain/release Mesh and Material instances. It also exposes metadata flags (isImpostor, manualVTRequired) that affect ECS component composition via GetPrefabComponents. The class caches loaded Material instances in a runtime container to avoid repeated loads and logs operations via ComponentBase.baseLog.
Fields
-
private AssetReference<GeometryAsset> m_GeometryAsset
Reference to the geometry asset that provides the Mesh[] for this prefab. Used by ObtainMeshes/ObtainMesh and ReleaseMeshes. May be null if no geometry is assigned. -
private AssetReference<SurfaceAsset>[] m_SurfaceAssets
Array of references to surface assets. Each surface asset provides Material/texture data for a material slot. Serialized and used by ObtainMaterials/ObtainMaterial. -
private Bounds3 m_Bounds
Axis-aligned bounding box (Bounds3) for the prefab geometry. -
private float m_SurfaceArea
Surface area value for the prefab (float), likely used for LOD/streaming heuristics or stats. -
private int m_IndexCount
Index count of the geometry (serialized metadata). -
private int m_VertexCount
Vertex count of the geometry (serialized metadata). -
private int m_MeshCount
Number of meshes/sub-mesh groups in the geometry. -
private bool m_IsImpostor
Flag indicating this prefab should be treated as an impostor (billboard or simplified representation). Affects which ECS components are added in GetPrefabComponents. -
private bool m_ManualVTRequired
Flag indicating manual handling of virtual texturing is required for this prefab. -
private Material[] m_MaterialsContainer
Runtime cache for Material instances loaded from the SurfaceAssets. Allocated on demand in ObtainMaterials to avoid repeated loads.
Properties
-
public bool hasGeometryAsset { get; }
True if a geometry asset reference is present (m_GeometryAsset != null). Read-only. -
public GeometryAsset geometryAsset { get; set; }
Get or set the GeometryAsset reference. Setting writes the AssetReference stored in m_GeometryAsset. -
public IEnumerable<SurfaceAsset> surfaceAssets { get; set; }
Enumerable view over the surface assets. Getter yields SurfaceAsset instances from the stored AssetReference array. Setter accepts a sequence of SurfaceAsset and converts them to AssetReferenceby id. -
public Bounds3 bounds { get; set; }
Get/set the prefab Bounds3 (m_Bounds). -
public float surfaceArea { get; set; }
Get/set the surface area (m_SurfaceArea). -
public int indexCount { get; set; }
Get/set index count metadata (m_IndexCount). -
public int vertexCount { get; set; }
Get/set vertex count metadata (m_VertexCount). -
public int meshCount { get; set; }
Get/set mesh count metadata (m_MeshCount). -
public bool isImpostor { get; set; }
Get/set the impostor flag (m_IsImpostor). -
public bool manualVTRequired { get; set; }
Get/set the manual virtual-texturing requirement flag (m_ManualVTRequired). -
public int materialCount { get; }
Computed property returning the number of surface assets (m_SurfaceAssets.Length). Useful to size arrays and validate material indices.
Constructors
public RenderPrefab()
Default constructor (implicit). Initializes the MonoBehaviour-derived component using Unity's default construction; no special constructor logic is defined in the source.
Methods
-
public SurfaceAsset GetSurfaceAsset(int index)
Returns the SurfaceAsset at the given index from m_SurfaceAssets. Index must be within bounds of the array. -
public void SetSurfaceAsset(int index, SurfaceAsset value)
Sets the surface asset reference at the specified index. Stores the provided SurfaceAsset into the m_SurfaceAssets array (via AssetReference assignment). -
public Mesh[] ObtainMeshes()
Loads/returns the Mesh[] from the assigned GeometryAsset. Calls geometryAsset?.ObtainMeshes() and logs the operation. Returns null if no geometry asset is assigned or if the asset returns null. -
public Mesh ObtainMesh(int materialIndex, out int subMeshIndex)
Returns the Mesh that contains the requested materialIndex and sets subMeshIndex to the sub-mesh index within that Mesh. If the prefab has a geometry asset, it iterates meshes and their subMeshCount to resolve which Mesh holds the materialIndex. If not found, returns null. The method logs the operation. This is useful when material slots map across multiple Mesh objects. -
public void ReleaseMeshes()
Releases meshes loaded from the geometry asset by calling geometryAsset?.ReleaseMeshes() and logs the operation. -
public Material[] ObtainMaterials(bool useVT = true)
Ensures the runtime Material cache (m_MaterialsContainer) is allocated and loads Material instances from each SurfaceAsset via surfaceAsset.Load(...). The load uses loadTextures: true and respects the useVT parameter for virtual texturing. Returns the cached array of Materials. Logs the operation. -
public Material ObtainMaterial(int i, bool useVT = true)
Convenience accessor that returns the i-th Material from the cached material array (via ObtainMaterials). Throws IndexOutOfRangeException with a descriptive message if i is out of range. -
public void ReleaseMaterials()
Placeholder for releasing any loaded Material instances. In the current implementation this method logs the release but contains no explicit release logic (no-op). Materials are cached in m_MaterialsContainer; if material unloading is required, this method would be the hook to do so. -
public void Release()
Convenience method that calls ReleaseMeshes() and ReleaseMaterials() to release loaded runtime resources associated with this prefab. -
public override void GetPrefabComponents(HashSet<ComponentType> components)
Overrides the base to add ECS ComponentType requirements for the prefab. Adds MeshData, SharedMeshData, and BatchGroup (all ReadWrite). If isImpostor is true, also adds ImpostorData. This determines which ECS components are included when converting/instantiating this render prefab in an ECS context.
Notes: Methods include tracing via ComponentBase.baseLog.TraceFormat(this, "...", base.name) to help debugging/load traces.
Usage Example
// Assume 'renderPrefab' is an instance of Game.Prefabs.RenderPrefab (from an asset or component)
RenderPrefab renderPrefab = /* obtain reference */;
// Obtain all meshes (may load or pin them in the GeometryAsset)
Mesh[] meshes = renderPrefab.ObtainMeshes();
if (meshes != null)
{
// Use meshes...
}
// Obtain a specific mesh and subMesh index for material slot 2
int subMeshIndex;
Mesh meshForMaterial = renderPrefab.ObtainMesh(2, out subMeshIndex);
if (meshForMaterial != null)
{
// Render using meshForMaterial and subMeshIndex
}
// Obtain materials (loads textures and materials; useVT toggles virtual-texturing use)
Material[] materials = renderPrefab.ObtainMaterials(useVT: true);
Material firstMat = renderPrefab.ObtainMaterial(0);
// After you're done with runtime resources, release them
renderPrefab.Release(); // calls ReleaseMeshes() and ReleaseMaterials()
Note: ReleaseMaterials() is currently a no-op in this implementation, while ReleaseMeshes() calls into the GeometryAsset to release meshes. Always check the specific asset implementations if explicit unloading is required.