TeleportArea.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. //
  3. // Purpose: An area that the player can teleport to
  4. //
  5. //=============================================================================
  6. using UnityEngine;
  7. #if UNITY_EDITOR
  8. using UnityEditor;
  9. #endif
  10. namespace Valve.VR.InteractionSystem
  11. {
  12. //-------------------------------------------------------------------------
  13. public class TeleportArea : TeleportMarkerBase
  14. {
  15. //Public properties
  16. public Bounds meshBounds { get; private set; }
  17. //Private data
  18. private MeshRenderer areaMesh;
  19. private int tintColorId = 0;
  20. private Color visibleTintColor = Color.clear;
  21. private Color highlightedTintColor = Color.clear;
  22. private Color lockedTintColor = Color.clear;
  23. private bool highlighted = false;
  24. //-------------------------------------------------
  25. public void Awake()
  26. {
  27. areaMesh = GetComponent<MeshRenderer>();
  28. #if UNITY_URP
  29. tintColorId = Shader.PropertyToID( "_BaseColor" );
  30. #else
  31. tintColorId = Shader.PropertyToID("_TintColor");
  32. #endif
  33. CalculateBounds();
  34. }
  35. //-------------------------------------------------
  36. public void Start()
  37. {
  38. visibleTintColor = Teleport.instance.areaVisibleMaterial.GetColor( tintColorId );
  39. highlightedTintColor = Teleport.instance.areaHighlightedMaterial.GetColor( tintColorId );
  40. lockedTintColor = Teleport.instance.areaLockedMaterial.GetColor( tintColorId );
  41. }
  42. //-------------------------------------------------
  43. public override bool ShouldActivate( Vector3 playerPosition )
  44. {
  45. return true;
  46. }
  47. //-------------------------------------------------
  48. public override bool ShouldMovePlayer()
  49. {
  50. return true;
  51. }
  52. //-------------------------------------------------
  53. public override void Highlight( bool highlight )
  54. {
  55. if ( !locked )
  56. {
  57. highlighted = highlight;
  58. if ( highlight )
  59. {
  60. areaMesh.material = Teleport.instance.areaHighlightedMaterial;
  61. }
  62. else
  63. {
  64. areaMesh.material = Teleport.instance.areaVisibleMaterial;
  65. }
  66. }
  67. }
  68. //-------------------------------------------------
  69. public override void SetAlpha( float tintAlpha, float alphaPercent )
  70. {
  71. Color tintedColor = GetTintColor();
  72. tintedColor.a *= alphaPercent;
  73. areaMesh.material.SetColor( tintColorId, tintedColor );
  74. }
  75. //-------------------------------------------------
  76. public override void UpdateVisuals()
  77. {
  78. if ( locked )
  79. {
  80. areaMesh.material = Teleport.instance.areaLockedMaterial;
  81. }
  82. else
  83. {
  84. areaMesh.material = Teleport.instance.areaVisibleMaterial;
  85. }
  86. }
  87. //-------------------------------------------------
  88. public void UpdateVisualsInEditor()
  89. {
  90. if (Teleport.instance == null)
  91. return;
  92. areaMesh = GetComponent<MeshRenderer>();
  93. if ( locked )
  94. {
  95. areaMesh.sharedMaterial = Teleport.instance.areaLockedMaterial;
  96. }
  97. else
  98. {
  99. areaMesh.sharedMaterial = Teleport.instance.areaVisibleMaterial;
  100. }
  101. }
  102. //-------------------------------------------------
  103. private bool CalculateBounds()
  104. {
  105. MeshFilter meshFilter = GetComponent<MeshFilter>();
  106. if ( meshFilter == null )
  107. {
  108. return false;
  109. }
  110. Mesh mesh = meshFilter.sharedMesh;
  111. if ( mesh == null )
  112. {
  113. return false;
  114. }
  115. meshBounds = mesh.bounds;
  116. return true;
  117. }
  118. //-------------------------------------------------
  119. private Color GetTintColor()
  120. {
  121. if ( locked )
  122. {
  123. return lockedTintColor;
  124. }
  125. else
  126. {
  127. if ( highlighted )
  128. {
  129. return highlightedTintColor;
  130. }
  131. else
  132. {
  133. return visibleTintColor;
  134. }
  135. }
  136. }
  137. }
  138. #if UNITY_EDITOR
  139. //-------------------------------------------------------------------------
  140. [CustomEditor( typeof( TeleportArea ) )]
  141. public class TeleportAreaEditor : Editor
  142. {
  143. //-------------------------------------------------
  144. void OnEnable()
  145. {
  146. if ( Selection.activeTransform != null )
  147. {
  148. TeleportArea teleportArea = Selection.activeTransform.GetComponent<TeleportArea>();
  149. if ( teleportArea != null )
  150. {
  151. teleportArea.UpdateVisualsInEditor();
  152. }
  153. }
  154. }
  155. //-------------------------------------------------
  156. public override void OnInspectorGUI()
  157. {
  158. DrawDefaultInspector();
  159. if ( Selection.activeTransform != null )
  160. {
  161. TeleportArea teleportArea = Selection.activeTransform.GetComponent<TeleportArea>();
  162. if ( GUI.changed && teleportArea != null )
  163. {
  164. teleportArea.UpdateVisualsInEditor();
  165. }
  166. }
  167. }
  168. }
  169. #endif
  170. }