TeleportPoint.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. //
  3. // Purpose: Single location that the player can teleport to
  4. //
  5. //=============================================================================
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. #if UNITY_EDITOR
  9. using UnityEditor;
  10. #endif
  11. namespace Valve.VR.InteractionSystem
  12. {
  13. //-------------------------------------------------------------------------
  14. public class TeleportPoint : TeleportMarkerBase
  15. {
  16. public enum TeleportPointType
  17. {
  18. MoveToLocation,
  19. SwitchToNewScene
  20. };
  21. //Public variables
  22. public TeleportPointType teleportType = TeleportPointType.MoveToLocation;
  23. public string title;
  24. public string switchToScene;
  25. public Color titleVisibleColor;
  26. public Color titleHighlightedColor;
  27. public Color titleLockedColor;
  28. public bool playerSpawnPoint = false;
  29. //Private data
  30. private bool gotReleventComponents = false;
  31. private MeshRenderer markerMesh;
  32. private MeshRenderer switchSceneIcon;
  33. private MeshRenderer moveLocationIcon;
  34. private MeshRenderer lockedIcon;
  35. private MeshRenderer pointIcon;
  36. private Transform lookAtJointTransform;
  37. private new Animation animation;
  38. private Text titleText;
  39. private Player player;
  40. private Vector3 lookAtPosition = Vector3.zero;
  41. private int tintColorID = 0;
  42. private Color tintColor = Color.clear;
  43. private Color titleColor = Color.clear;
  44. private float fullTitleAlpha = 0.0f;
  45. //Constants
  46. private const string switchSceneAnimation = "switch_scenes_idle";
  47. private const string moveLocationAnimation = "move_location_idle";
  48. private const string lockedAnimation = "locked_idle";
  49. //-------------------------------------------------
  50. public override bool showReticle
  51. {
  52. get
  53. {
  54. return false;
  55. }
  56. }
  57. //-------------------------------------------------
  58. void Awake()
  59. {
  60. GetRelevantComponents();
  61. animation = GetComponent<Animation>();
  62. #if UNITY_URP
  63. tintColorID = Shader.PropertyToID( "_BaseColor" );
  64. #else
  65. tintColorID = Shader.PropertyToID("_TintColor");
  66. #endif
  67. moveLocationIcon.gameObject.SetActive( false );
  68. switchSceneIcon.gameObject.SetActive( false );
  69. lockedIcon.gameObject.SetActive( false );
  70. UpdateVisuals();
  71. }
  72. //-------------------------------------------------
  73. void Start()
  74. {
  75. player = Player.instance;
  76. }
  77. //-------------------------------------------------
  78. void Update()
  79. {
  80. if ( Application.isPlaying )
  81. {
  82. lookAtPosition.x = player.hmdTransform.position.x;
  83. lookAtPosition.y = lookAtJointTransform.position.y;
  84. lookAtPosition.z = player.hmdTransform.position.z;
  85. lookAtJointTransform.LookAt( lookAtPosition );
  86. }
  87. }
  88. //-------------------------------------------------
  89. public override bool ShouldActivate( Vector3 playerPosition )
  90. {
  91. return ( Vector3.Distance( transform.position, playerPosition ) > 1.0f );
  92. }
  93. //-------------------------------------------------
  94. public override bool ShouldMovePlayer()
  95. {
  96. return true;
  97. }
  98. //-------------------------------------------------
  99. public override void Highlight( bool highlight )
  100. {
  101. if ( !locked )
  102. {
  103. if ( highlight )
  104. {
  105. SetMeshMaterials( Teleport.instance.pointHighlightedMaterial, titleHighlightedColor );
  106. }
  107. else
  108. {
  109. SetMeshMaterials( Teleport.instance.pointVisibleMaterial, titleVisibleColor );
  110. }
  111. }
  112. if ( highlight )
  113. {
  114. pointIcon.gameObject.SetActive( true );
  115. animation.Play();
  116. }
  117. else
  118. {
  119. pointIcon.gameObject.SetActive( false );
  120. animation.Stop();
  121. }
  122. }
  123. //-------------------------------------------------
  124. public override void UpdateVisuals()
  125. {
  126. if ( !gotReleventComponents )
  127. {
  128. return;
  129. }
  130. if ( locked )
  131. {
  132. SetMeshMaterials( Teleport.instance.pointLockedMaterial, titleLockedColor );
  133. pointIcon = lockedIcon;
  134. animation.clip = animation.GetClip( lockedAnimation );
  135. }
  136. else
  137. {
  138. SetMeshMaterials( Teleport.instance.pointVisibleMaterial, titleVisibleColor );
  139. switch ( teleportType )
  140. {
  141. case TeleportPointType.MoveToLocation:
  142. {
  143. pointIcon = moveLocationIcon;
  144. animation.clip = animation.GetClip( moveLocationAnimation );
  145. }
  146. break;
  147. case TeleportPointType.SwitchToNewScene:
  148. {
  149. pointIcon = switchSceneIcon;
  150. animation.clip = animation.GetClip( switchSceneAnimation );
  151. }
  152. break;
  153. }
  154. }
  155. titleText.text = title;
  156. }
  157. //-------------------------------------------------
  158. public override void SetAlpha( float tintAlpha, float alphaPercent )
  159. {
  160. tintColor = markerMesh.material.GetColor( tintColorID );
  161. tintColor.a = tintAlpha;
  162. markerMesh.material.SetColor( tintColorID, tintColor );
  163. switchSceneIcon.material.SetColor( tintColorID, tintColor );
  164. moveLocationIcon.material.SetColor( tintColorID, tintColor );
  165. lockedIcon.material.SetColor( tintColorID, tintColor );
  166. titleColor.a = fullTitleAlpha * alphaPercent;
  167. titleText.color = titleColor;
  168. }
  169. //-------------------------------------------------
  170. public void SetMeshMaterials( Material material, Color textColor )
  171. {
  172. markerMesh.material = material;
  173. switchSceneIcon.material = material;
  174. moveLocationIcon.material = material;
  175. lockedIcon.material = material;
  176. titleColor = textColor;
  177. fullTitleAlpha = textColor.a;
  178. titleText.color = titleColor;
  179. }
  180. //-------------------------------------------------
  181. public void TeleportToScene()
  182. {
  183. if ( !string.IsNullOrEmpty( switchToScene ) )
  184. {
  185. Debug.Log("<b>[SteamVR Interaction]</b> TeleportPoint: Hook up your level loading logic to switch to new scene: " + switchToScene, this);
  186. }
  187. else
  188. {
  189. Debug.LogError("<b>[SteamVR Interaction]</b> TeleportPoint: Invalid scene name to switch to: " + switchToScene, this);
  190. }
  191. }
  192. //-------------------------------------------------
  193. public void GetRelevantComponents()
  194. {
  195. markerMesh = transform.Find( "teleport_marker_mesh" ).GetComponent<MeshRenderer>();
  196. switchSceneIcon = transform.Find( "teleport_marker_lookat_joint/teleport_marker_icons/switch_scenes_icon" ).GetComponent<MeshRenderer>();
  197. moveLocationIcon = transform.Find( "teleport_marker_lookat_joint/teleport_marker_icons/move_location_icon" ).GetComponent<MeshRenderer>();
  198. lockedIcon = transform.Find( "teleport_marker_lookat_joint/teleport_marker_icons/locked_icon" ).GetComponent<MeshRenderer>();
  199. lookAtJointTransform = transform.Find( "teleport_marker_lookat_joint" );
  200. titleText = transform.Find( "teleport_marker_lookat_joint/teleport_marker_canvas/teleport_marker_canvas_text" ).GetComponent<Text>();
  201. gotReleventComponents = true;
  202. }
  203. //-------------------------------------------------
  204. public void ReleaseRelevantComponents()
  205. {
  206. markerMesh = null;
  207. switchSceneIcon = null;
  208. moveLocationIcon = null;
  209. lockedIcon = null;
  210. lookAtJointTransform = null;
  211. titleText = null;
  212. }
  213. //-------------------------------------------------
  214. public void UpdateVisualsInEditor()
  215. {
  216. if ( Application.isPlaying )
  217. {
  218. return;
  219. }
  220. GetRelevantComponents();
  221. if ( locked )
  222. {
  223. lockedIcon.gameObject.SetActive( true );
  224. moveLocationIcon.gameObject.SetActive( false );
  225. switchSceneIcon.gameObject.SetActive( false );
  226. markerMesh.sharedMaterial = Teleport.instance.pointLockedMaterial;
  227. lockedIcon.sharedMaterial = Teleport.instance.pointLockedMaterial;
  228. titleText.color = titleLockedColor;
  229. }
  230. else
  231. {
  232. lockedIcon.gameObject.SetActive( false );
  233. markerMesh.sharedMaterial = Teleport.instance.pointVisibleMaterial;
  234. switchSceneIcon.sharedMaterial = Teleport.instance.pointVisibleMaterial;
  235. moveLocationIcon.sharedMaterial = Teleport.instance.pointVisibleMaterial;
  236. titleText.color = titleVisibleColor;
  237. switch ( teleportType )
  238. {
  239. case TeleportPointType.MoveToLocation:
  240. {
  241. moveLocationIcon.gameObject.SetActive( true );
  242. switchSceneIcon.gameObject.SetActive( false );
  243. }
  244. break;
  245. case TeleportPointType.SwitchToNewScene:
  246. {
  247. moveLocationIcon.gameObject.SetActive( false );
  248. switchSceneIcon.gameObject.SetActive( true );
  249. }
  250. break;
  251. }
  252. }
  253. titleText.text = title;
  254. ReleaseRelevantComponents();
  255. }
  256. }
  257. #if UNITY_EDITOR
  258. //-------------------------------------------------------------------------
  259. [CustomEditor( typeof( TeleportPoint ) )]
  260. public class TeleportPointEditor : Editor
  261. {
  262. //-------------------------------------------------
  263. void OnEnable()
  264. {
  265. if ( Selection.activeTransform )
  266. {
  267. TeleportPoint teleportPoint = Selection.activeTransform.GetComponent<TeleportPoint>();
  268. if (teleportPoint != null)
  269. teleportPoint.UpdateVisualsInEditor();
  270. }
  271. }
  272. //-------------------------------------------------
  273. public override void OnInspectorGUI()
  274. {
  275. DrawDefaultInspector();
  276. if ( Selection.activeTransform )
  277. {
  278. TeleportPoint teleportPoint = Selection.activeTransform.GetComponent<TeleportPoint>();
  279. if ( GUI.changed )
  280. {
  281. teleportPoint.UpdateVisualsInEditor();
  282. }
  283. }
  284. }
  285. }
  286. #endif
  287. }