Unity HDRP Product Visualizer 1.0.0
Interactive 3D product showcase built with Unity 6 and HDRP
Loading...
Searching...
No Matches
ProductVariantController.cs
Go to the documentation of this file.
1using System.Collections.Generic;
2using UnityEngine;
3
8
15[System.Serializable]
16public class ProductVariant
17{
19 public string variantName = "Default";
20
22 public Color baseColor = Color.white;
23
27 [ColorUsage(false, true)]
28 public Color emissiveColor = Color.black;
29
31 [Range(0f, 1f)] public float metallic = 0.9f;
32
34 [Range(0f, 1f)] public float smoothness = 0.85f;
35}
36
51public class ProductVariantController : MonoBehaviour
52{
53 // ------------------------------------------------------------------
54 // Inspector fields
55 // ------------------------------------------------------------------
56
59 [Header("Body Renderers (color changes)")]
60 public List<Renderer> bodyRenderers = new();
61
63 [Header("Variants")]
64 public List<ProductVariant> variants = new();
65
66 // ------------------------------------------------------------------
67 // Events
68 // ------------------------------------------------------------------
69
71 public System.Action<int, ProductVariant> OnVariantChanged;
72
73 // ------------------------------------------------------------------
74 // Private state
75 // ------------------------------------------------------------------
76
77 private int _currentIndex;
78 private MaterialPropertyBlock _mpb;
79
80 // ------------------------------------------------------------------
81 // Unity lifecycle
82 // ------------------------------------------------------------------
83
84 void Awake() => _mpb = new MaterialPropertyBlock();
85
86 void Start()
87 {
88 if (variants.Count > 0) Apply(0);
89 }
90
91 // ------------------------------------------------------------------
92 // Public API
93 // ------------------------------------------------------------------
94
96 public void NextVariant()
97 {
98 _currentIndex = (_currentIndex + 1) % variants.Count;
99 Apply(_currentIndex);
100 OnVariantChanged?.Invoke(_currentIndex, variants[_currentIndex]);
101 }
102
104 public void PreviousVariant()
105 {
106 _currentIndex = (_currentIndex - 1 + variants.Count) % variants.Count;
107 Apply(_currentIndex);
108 OnVariantChanged?.Invoke(_currentIndex, variants[_currentIndex]);
109 }
110
113 public void SetVariantIndex(int index)
114 {
115 if (index < 0 || index >= variants.Count) return;
116 _currentIndex = index;
117 Apply(index);
118 OnVariantChanged?.Invoke(_currentIndex, variants[_currentIndex]);
119 }
120
122 public ProductVariant CurrentVariant => variants.Count > 0 ? variants[_currentIndex] : null;
123
125 public int CurrentIndex => _currentIndex;
126
128 public int VariantCount => variants.Count;
129
132
133 // ------------------------------------------------------------------
134 // Private helpers
135 // ------------------------------------------------------------------
136
139 void Apply(int index)
140 {
141 ProductVariant v = variants[index];
142 foreach (Renderer r in bodyRenderers)
143 {
144 if (r == null) continue;
145 r.GetPropertyBlock(_mpb);
146 _mpb.SetColor("_BaseColor", v.baseColor);
147 _mpb.SetFloat("_Metallic", v.metallic);
148 _mpb.SetFloat("_Smoothness", v.smoothness);
149 _mpb.SetColor("_EmissiveColor", v.emissiveColor);
150 r.SetPropertyBlock(_mpb);
151 }
152 }
153}
Applies ProductVariant presets to a set of Renderers using MaterialPropertyBlock.
ProductVariant CurrentVariant
The currently active ProductVariant, or null if the list is empty.
void NextVariant()
Advances to the next variant, wrapping around the list.
void PreviousVariant()
Steps back to the previous variant, wrapping around the list.
System.Action< int, ProductVariant > OnVariantChanged
Fired after a variant change with (newIndex, newVariant).
string CurrentVariantName
Display name of the active variant (empty string if none).
List< Renderer > bodyRenderers
Renderers whose material properties will be overridden.
void SetVariantIndex(int index)
Jumps directly to a variant by index.
int VariantCount
Total number of registered variants.
int CurrentIndex
Zero-based index of the active variant.
List< ProductVariant > variants
Ordered list of PBR colour presets available in the visualizer.
Serialisable PBR colour preset applied through MaterialPropertyBlock.
float metallic
Metallic value in [0, 1] mapped to _Metallic.
string variantName
Human-readable name displayed in the UI (e.g. "Ocean Blue").
Color emissiveColor
HDR emissive colour mapped to _EmissiveColor.
float smoothness
Smoothness (gloss) value in [0, 1] mapped to _Smoothness.
Color baseColor
Albedo colour mapped to the HDRP _BaseColor property.