AnthonyC

Anthony / NextRealityNews HowTos

Apr 7th, 2017
343
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2.  
  3. [RequireComponent(typeof(Renderer))]
  4. public class HotspotColorChanger : MonoBehaviour
  5. {
  6.     public Color activeColor = Color.red;
  7.  
  8.     private Color originalColor;
  9.     private Material cachedMaterial;
  10.  
  11.     private void Awake()
  12.     {
  13.         cachedMaterial = GetComponent<Renderer>().material;
  14.         originalColor = cachedMaterial.GetColor("_Color");
  15.     }
  16.  
  17.     private void OnEnable()
  18.     {
  19.         Hotspot.OnEntered += OnHotspotEnter;
  20.         Hotspot.OnExited += OnHotspotExit;
  21.     }
  22.  
  23.     private void OnDisable()
  24.     {
  25.         Hotspot.OnEntered -= OnHotspotEnter;
  26.         Hotspot.OnExited -= OnHotspotExit;
  27.     }
  28.  
  29.     private void OnHotspotEnter()
  30.     {
  31.         cachedMaterial.SetColor("_Color", activeColor);
  32.     }
  33.  
  34.     private void OnHotspotExit()
  35.     {
  36.         cachedMaterial.SetColor("_Color", originalColor);
  37.     }
  38.  
  39.     private void OnDestroy()
  40.     {
  41.         DestroyImmediate(cachedMaterial);
  42.     }
  43. }
RAW Paste Data