TeleportArea.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. tintColorId = Shader.PropertyToID( "_TintColor" );
  29. CalculateBounds();
  30. }
  31. //-------------------------------------------------
  32. public void Start()
  33. {
  34. visibleTintColor = Teleport.instance.areaVisibleMaterial.GetColor( tintColorId );
  35. highlightedTintColor = Teleport.instance.areaHighlightedMaterial.GetColor( tintColorId );
  36. lockedTintColor = Teleport.instance.areaLockedMaterial.GetColor( tintColorId );
  37. }
  38. //-------------------------------------------------
  39. public override bool ShouldActivate( Vector3 playerPosition )
  40. {
  41. return true;
  42. }
  43. //-------------------------------------------------
  44. public override bool ShouldMovePlayer()
  45. {
  46. return true;
  47. }
  48. //-------------------------------------------------
  49. public override void Highlight( bool highlight )
  50. {
  51. if ( !locked )
  52. {
  53. highlighted = highlight;
  54. if ( highlight )
  55. {
  56. areaMesh.material = Teleport.instance.areaHighlightedMaterial;
  57. }
  58. else
  59. {
  60. areaMesh.material = Teleport.instance.areaVisibleMaterial;
  61. }
  62. }
  63. }
  64. //-------------------------------------------------
  65. public override void SetAlpha( float tintAlpha, float alphaPercent )
  66. {
  67. Color tintedColor = GetTintColor();
  68. tintedColor.a *= alphaPercent;
  69. areaMesh.material.SetColor( tintColorId, tintedColor );
  70. }
  71. //-------------------------------------------------
  72. public override void UpdateVisuals()
  73. {
  74. if ( locked )
  75. {
  76. areaMesh.material = Teleport.instance.areaLockedMaterial;
  77. }
  78. else
  79. {
  80. areaMesh.material = Teleport.instance.areaVisibleMaterial;
  81. }
  82. }
  83. //-------------------------------------------------
  84. public void UpdateVisualsInEditor()
  85. {
  86. if (Teleport.instance == null)
  87. return;
  88. areaMesh = GetComponent<MeshRenderer>();
  89. if ( locked )
  90. {
  91. areaMesh.sharedMaterial = Teleport.instance.areaLockedMaterial;
  92. }
  93. else
  94. {
  95. areaMesh.sharedMaterial = Teleport.instance.areaVisibleMaterial;
  96. }
  97. }
  98. //-------------------------------------------------
  99. private bool CalculateBounds()
  100. {
  101. MeshFilter meshFilter = GetComponent<MeshFilter>();
  102. if ( meshFilter == null )
  103. {
  104. return false;
  105. }
  106. Mesh mesh = meshFilter.sharedMesh;
  107. if ( mesh == null )
  108. {
  109. return false;
  110. }
  111. meshBounds = mesh.bounds;
  112. return true;
  113. }
  114. //-------------------------------------------------
  115. private Color GetTintColor()
  116. {
  117. if ( locked )
  118. {
  119. return lockedTintColor;
  120. }
  121. else
  122. {
  123. if ( highlighted )
  124. {
  125. return highlightedTintColor;
  126. }
  127. else
  128. {
  129. return visibleTintColor;
  130. }
  131. }
  132. }
  133. }
  134. #if UNITY_EDITOR
  135. //-------------------------------------------------------------------------
  136. [CustomEditor( typeof( TeleportArea ) )]
  137. public class TeleportAreaEditor : Editor
  138. {
  139. //-------------------------------------------------
  140. void OnEnable()
  141. {
  142. if ( Selection.activeTransform != null )
  143. {
  144. TeleportArea teleportArea = Selection.activeTransform.GetComponent<TeleportArea>();
  145. if ( teleportArea != null )
  146. {
  147. teleportArea.UpdateVisualsInEditor();
  148. }
  149. }
  150. }
  151. //-------------------------------------------------
  152. public override void OnInspectorGUI()
  153. {
  154. DrawDefaultInspector();
  155. if ( Selection.activeTransform != null )
  156. {
  157. TeleportArea teleportArea = Selection.activeTransform.GetComponent<TeleportArea>();
  158. if ( GUI.changed && teleportArea != null )
  159. {
  160. teleportArea.UpdateVisualsInEditor();
  161. }
  162. }
  163. }
  164. }
  165. #endif
  166. }