Game.Prefabs.ColorProperties
Assembly: Assembly-CSharp
Namespace: Game.Prefabs
Type: class ColorProperties
Base: ComponentBase
Summary:
Component used by rendering prefabs to describe color variation sets and how they map to mesh/material color channels. ColorProperties holds predefined color sets (VariationSet), per-channel binding information, and optional variation groups that influence randomness, synchronization, and alpha ranges. During prefab initialization it populates an Entity DynamicBuffer
Fields
-
public List<VariationSet> m_ColorVariations
Holds the list of color variation entries. Each VariationSet contains an array of up to 3 Colors (m_Colors) and a string m_VariationGroup to assign it to a named variation group. -
public List<ColorChannelBinding> m_ChannelsBinding
Fixed-length list mapping the component's logical channels (0..2) to color indices used by each VariationSet. Each ColorChannelBinding contains m_ChannelId (sbyte) and m_CanBeModifiedByExternal (bool). The field is pre-populated with bindings for channels 0,1,2. -
public List<VariationGroup> m_VariationGroups
Optional list of VariationGroup objects. Each group can override randomness ranges, probability, and mesh sync mode and is used to assign group IDs to ColorVariation entries created in Initialize. -
public int3 m_VariationRanges
Default hue/saturation/value randomness ranges (x = hue, y = saturation, z = value). Values clamped to 0..100 when applied. -
public int3 m_AlphaRanges
Default alpha ranges for the three channels. Values clamped to 0..100 when applied. -
public ColorSourceType m_ExternalColorSource
Specifies how external color sources are interpreted (stored to colorVariation.m_ColorSourceType for the ColorVariation buffer).
Nested type fields (summarized):
- VariationSet.m_Colors (Color[]) — Array of 3 colors for a variation.
- VariationSet.m_VariationGroup (string) — Name of the variation group this set belongs to.
- ColorChannelBinding.m_ChannelId (sbyte) — Index into VariationSet.m_Colors for this logical channel.
- ColorChannelBinding.m_CanBeModifiedByExternal (bool) — Whether the channel can be overridden by external sources.
- VariationGroup.m_Name (string) — Group name used to get a color group ID from MeshColorSystem.
- VariationGroup.m_Probability (int) — Group probability (0..100).
- VariationGroup.m_MeshSyncMode (ColorSyncFlags) — Synchronization flags for the group.
- VariationGroup.m_OverrideRandomness (bool) — If true, group's ranges override the component defaults.
- VariationGroup.m_VariationRanges (int3) — Per-group HSV randomness ranges.
- VariationGroup.m_AlphaRanges (int3) — Per-group alpha ranges.
Properties
- None (no public C# properties declared on this type)
Constructors
public ColorProperties()
Default parameterless constructor (Unity/MonoBehaviour-style component construction). Initialization work is performed in Initialize when the prefab is converted to an Entity.
Methods
-
public bool SanityCheck(sbyte channel)
Checks whether the provided channel index is valid (m_ChannelsBinding exists and channel >= 0 and less than m_ChannelsBinding.Count). Returns false for invalid channel indices. -
public bool CanBeModifiedByExternal(sbyte channel)
Returns whether the bound channel allows external modification. If the channel is invalid, the method returns true by default (conservative behavior). -
public Color GetColor(int index, sbyte channel)
Get the Color for the given variation index and logical channel. If bindings and variations are valid, index is modulo'd by m_ColorVariations.Count and the color at the bound m_ChannelId is returned. Otherwise returns Color.white. -
public int GetAlpha(int3 alphas, sbyte channel, int def)
Returns the integer alpha for the given channel, using the provided int3 (per-index alpha) and channel-to-index binding; returns def if the channel is invalid. -
public float GetAlpha(float3 alphas, sbyte channel, float def)
Same as above but for float3 alpha inputs. -
public override void GetArchetypeComponents(HashSet<ComponentType> components)
Empty override; this component does not add archetype components itself during conversion. -
public override void GetPrefabComponents(HashSet<ComponentType> components)
Adds ComponentType.ReadWrite() to the set of prefab components so that the converted entity has a ColorVariation dynamic buffer. -
public override void Initialize(EntityManager entityManager, Entity entity)
Main conversion/initialization logic: obtains MeshColorSystem, creates a prototype ColorVariation populated with defaults (group ID, sync flags, color source, probability, per-channel external index mapping), clamps and applies m_VariationRanges and m_AlphaRanges, resizes the entity's ColorVariation DynamicBuffer to the number of color variations and fills it. If m_VariationGroups is present, it iterates groups, gets group-specific group IDs, sync flags, probability and optionally overrides randomness ranges, then writes ColorVariation entries for each VariationSet that belongs to that group. If a group has an empty name, the method will early-return (the code sets a flag and returns if any group has an empty name). Finally it writes any VariationSets with empty m_VariationGroup using the default colorVariation settings.
Notes on behavior: - Channel binding: for each logical channel 0..2, Initialize calls CanBeModifiedByExternal and sets external channel indices in ColorVariation accordingly (or -1 if not allowed). - Uses math.clamp on ranges to keep values between 0 and 100. - Populates ColorVariation.m_ColorSet[0..2] using GetColor for each variation.
Usage Example
// Example: reading colors and alphas from a ColorProperties instance
using Unity.Mathematics;
using UnityEngine;
using Game.Prefabs;
void Example(ColorProperties cp)
{
// Get color for variation index 0 and logical channel 1
Color c = cp.GetColor(0, (sbyte)1);
// Get integer alpha for channel 2 using an int3 of alpha ranges
int a = cp.GetAlpha(new int3(10, 20, 30), (sbyte)2, 255);
// Get float alpha for channel 0 using float3
float af = cp.GetAlpha(new float3(0.5f, 0.0f, 1.0f), (sbyte)0, 1.0f);
}
Additional notes: - ColorProperties is intended to be used on render prefabs and is converted to ECS entities where the ColorVariation dynamic buffer drives runtime per-instance coloring via MeshColorSystem. - Configure m_ChannelsBinding in the prefab to map logical channels to specific color indices in your VariationSet.m_Colors.