TeleportArea.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. areaMesh = GetComponent<MeshRenderer>();
  87. if ( locked )
  88. {
  89. areaMesh.sharedMaterial = Teleport.instance.areaLockedMaterial;
  90. }
  91. else
  92. {
  93. areaMesh.sharedMaterial = Teleport.instance.areaVisibleMaterial;
  94. }
  95. }
  96. //-------------------------------------------------
  97. private bool CalculateBounds()
  98. {
  99. MeshFilter meshFilter = GetComponent<MeshFilter>();
  100. if ( meshFilter == null )
  101. {
  102. return false;
  103. }
  104. Mesh mesh = meshFilter.sharedMesh;
  105. if ( mesh == null )
  106. {
  107. return false;
  108. }
  109. meshBounds = mesh.bounds;
  110. return true;
  111. }
  112. //-------------------------------------------------
  113. private Color GetTintColor()
  114. {
  115. if ( locked )
  116. {
  117. return lockedTintColor;
  118. }
  119. else
  120. {
  121. if ( highlighted )
  122. {
  123. return highlightedTintColor;
  124. }
  125. else
  126. {
  127. return visibleTintColor;
  128. }
  129. }
  130. }
  131. }
  132. #if UNITY_EDITOR
  133. //-------------------------------------------------------------------------
  134. [CustomEditor( typeof( TeleportArea ) )]
  135. public class TeleportAreaEditor : Editor
  136. {
  137. //-------------------------------------------------
  138. void OnEnable()
  139. {
  140. if ( Selection.activeTransform != null )
  141. {
  142. TeleportArea teleportArea = Selection.activeTransform.GetComponent<TeleportArea>();
  143. if ( teleportArea != null )
  144. {
  145. teleportArea.UpdateVisualsInEditor();
  146. }
  147. }
  148. }
  149. //-------------------------------------------------
  150. public override void OnInspectorGUI()
  151. {
  152. DrawDefaultInspector();
  153. if ( Selection.activeTransform != null )
  154. {
  155. TeleportArea teleportArea = Selection.activeTransform.GetComponent<TeleportArea>();
  156. if ( GUI.changed && teleportArea != null )
  157. {
  158. teleportArea.UpdateVisualsInEditor();
  159. }
  160. }
  161. }
  162. }
  163. #endif
  164. }