CompassProFogVolume.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. namespace CompassNavigatorPro {
  5. [ExecuteInEditMode]
  6. public class CompassProFogVolume : MonoBehaviour {
  7. [Range (0, 1), Tooltip ("Transparency of the fog of war. A value of 1 means fully opaque fog.")]
  8. public float alpha;
  9. [Range (0, 1), Tooltip ("Controls the hardness of the border.")]
  10. public float border;
  11. [Tooltip ("Fog volumes are rendered in ascending order.")]
  12. public int order;
  13. Vector3 oldPos, oldScale;
  14. float oldAlpha, oldBorder;
  15. int oldOrder;
  16. void OnEnable () {
  17. ShowFogArea (true);
  18. }
  19. void OnDisable () {
  20. ShowFogArea (false);
  21. }
  22. void Update () {
  23. if (order != oldOrder || alpha != oldAlpha || border != oldBorder || transform.position != oldPos || transform.localScale != oldScale) {
  24. NotifyChanges ();
  25. }
  26. }
  27. void NotifyChanges () {
  28. oldPos = transform.position;
  29. oldScale = transform.localScale;
  30. oldAlpha = alpha;
  31. oldBorder = border;
  32. oldOrder = order;
  33. CompassPro compass = CompassPro.instance;
  34. if (compass != null) {
  35. compass.UpdateFogOfWar ();
  36. }
  37. }
  38. void ShowFogArea (bool state) {
  39. CompassPro compass = CompassPro.instance;
  40. if (compass != null) {
  41. Bounds bounds = GetComponent<BoxCollider> ().bounds;
  42. float alpha = state ? this.alpha : 0f;
  43. compass.SetFogOfWarAlpha (bounds, alpha, border);
  44. }
  45. }
  46. }
  47. }