codfel4

New Code

Dec 3rd, 2020 (edited)
1,058
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using Homebrew;
  2. using UnityEngine;
  3.  
  4. [ExecuteInEditMode]
  5. public class LampScript : MonoBehaviour
  6. {
  7.     public bool isLampOn;
  8.    
  9.     [Foldout("Sources", true)]
  10.     public GameObject lampContainer;
  11.     public GameObject haloLightSource;
  12.     public GameObject emittedLightSource;
  13.     public GameObject lampOn;
  14.     public GameObject lampOff;
  15.    
  16.     private bool _currentLampState = false;
  17.     private Light _haloLight;
  18.     private Light _emittedLight;
  19.    
  20.     void Start()
  21.     {
  22.         _haloLight = haloLightSource.GetComponent<Light>();
  23.         _emittedLight = emittedLightSource.GetComponent<Light>();
  24.         if(LampChanged()) ChangeLamp();
  25.     }
  26.  
  27.     void Update() {
  28.         if(Application.isEditor && LampChanged()) ChangeLamp();
  29.     }
  30.  
  31.     bool LampChanged()
  32.     {
  33.         return isLampOn != _currentLampState;
  34.     }
  35.  
  36.     void ChangeLamp()
  37.     {
  38.         RemoveOldLamp();
  39.  
  40.         _haloLight.enabled = _emittedLight.enabled = isLampOn;
  41.  
  42.         Instantiate(isLampOn ? lampOn : lampOff, lampContainer.transform);
  43.  
  44.         _currentLampState = !_currentLampState;
  45.     }
  46.  
  47.     void RemoveOldLamp()  
  48.     {
  49.         foreach (Transform child in lampContainer.transform) {
  50.             DestroyImmediate(child.gameObject);
  51.         }
  52.     }
  53.  
  54. }
  55.  
RAW Paste Data