11 [RequireComponent(typeof(CompoundButton))]
14 [Header(
"Icon Settings")]
17 private MeshRenderer targetIconRenderer = null;
19 [Tooltip(
"Turns off the icon entirely")]
20 public bool DisableIcon =
false;
22 [Tooltip(
"Disregard the icon in the profile and use Icon Override instead")]
23 public bool OverrideIcon =
false;
27 private string iconName;
30 [ShowIfBoolValue(
"OverrideIcon")]
31 [Tooltip(
"Icon to use for override")]
32 private Texture2D iconOverride = null;
35 [Tooltip(
"Alpha value for the text mesh component")]
36 private float alpha = 1f;
38 private Material instantiatedMaterial;
39 private Mesh instantiatedMesh;
40 private bool updatingAlpha =
false;
41 private float alphaTarget = 1f;
43 private const float AlphaThreshold = 0.01f;
57 if (alphaTarget != value)
60 if (Application.isPlaying)
62 if (Mathf.Abs (alpha - alphaTarget) < AlphaThreshold)
72 if (gameObject.activeSelf && gameObject.activeInHierarchy)
76 StartCoroutine(UpdateAlpha());
95 public MeshFilter IconMeshFilter
99 return targetIconRenderer != null ? targetIconRenderer.GetComponent<MeshFilter>() : null;
104 public void OnWillSaveScene()
110 ClearInstancedAssets();
112 SetIconName(iconName);
117 public string IconName
129 private void SetIconName(
string newName)
132 if (Profile == null) {
136 if (targetIconRenderer == null) {
141 targetIconRenderer.enabled =
false;
145 if (Profile.IconMaterial == null || Profile.IconMesh == null) {
150 if (instantiatedMaterial == null)
152 instantiatedMaterial =
new Material(Profile.IconMaterial);
153 instantiatedMaterial.name = Profile.IconMaterial.name;
155 targetIconRenderer.sharedMaterial = instantiatedMaterial;
158 if (instantiatedMesh == null)
160 instantiatedMesh = Instantiate(Profile.IconMesh);
161 instantiatedMesh.name = Profile.IconMesh.name;
163 IconMeshFilter.sharedMesh = instantiatedMesh;
168 targetIconRenderer.enabled =
true;
169 IconMeshFilter.sharedMesh = Profile.IconMesh;
170 IconMeshFilter.transform.localScale = Vector3.one;
171 instantiatedMaterial.mainTexture = iconOverride;
176 if (
string.IsNullOrEmpty(newName))
178 targetIconRenderer.enabled =
false;
184 if (!Profile.GetIcon(newName, targetIconRenderer, IconMeshFilter,
true))
186 targetIconRenderer.enabled =
false;
192 targetIconRenderer.enabled =
true;
196 private void OnDisable()
198 ClearInstancedAssets();
201 private void OnEnable()
203 if (Application.isPlaying)
205 ClearInstancedAssets();
208 SetIconName(iconName);
213 SetIconName(iconName);
216 private void RefreshAlpha()
218 string alphaColorProperty =
string.IsNullOrEmpty(Profile.AlphaColorProperty) ?
"_Color" : Profile.AlphaColorProperty;
219 if (instantiatedMaterial != null)
221 Color c = instantiatedMaterial.GetColor(alphaColorProperty);
223 instantiatedMaterial.SetColor(alphaColorProperty, c);
227 private void ClearInstancedAssets()
230 if (instantiatedMaterial != null)
232 if (Application.isPlaying)
234 Destroy(instantiatedMaterial);
238 DestroyImmediate(instantiatedMaterial);
241 instantiatedMaterial = null;
243 if (instantiatedMesh != null)
245 if (Application.isPlaying)
247 Destroy(instantiatedMesh);
251 DestroyImmediate(instantiatedMesh);
254 instantiatedMesh = null;
260 if (targetIconRenderer != null)
263 targetIconRenderer.sharedMaterial = Profile.IconMaterial;
265 if (IconMeshFilter != null)
267 IconMeshFilter.sharedMesh = Profile.IconMesh;
274 private IEnumerator UpdateAlpha()
276 float startTime = Time.time;
277 Color color = Color.white;
278 string alphaColorProperty =
string.IsNullOrEmpty(Profile.AlphaColorProperty) ?
"_Color" : Profile.AlphaColorProperty;
280 if (instantiatedMaterial != null)
282 color = instantiatedMaterial.GetColor(alphaColorProperty);
286 while (Time.time < startTime + Profile.AlphaTransitionSpeed)
288 alpha = Mathf.Lerp(alpha, alphaTarget, (Time.time - startTime) / Profile.AlphaTransitionSpeed);
289 if (instantiatedMaterial != null && !
string.IsNullOrEmpty(alphaColorProperty))
292 instantiatedMaterial.SetColor(alphaColorProperty, color);
299 updatingAlpha =
false;
304 public class CustomEditor : MRTKEditor {
305 protected override void DrawCustomFooter() {
307 if (iconButton != null && iconButton.
Profile != null)
309 iconButton.
IconName = iconButton.
Profile.DrawIconSelectField(iconButton.iconName);