TeleportPoint.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. tintColorID = Shader.PropertyToID( "_TintColor" );
  63. moveLocationIcon.gameObject.SetActive( false );
  64. switchSceneIcon.gameObject.SetActive( false );
  65. lockedIcon.gameObject.SetActive( false );
  66. UpdateVisuals();
  67. }
  68. //-------------------------------------------------
  69. void Start()
  70. {
  71. player = Player.instance;
  72. }
  73. //-------------------------------------------------
  74. void Update()
  75. {
  76. if ( Application.isPlaying )
  77. {
  78. lookAtPosition.x = player.hmdTransform.position.x;
  79. lookAtPosition.y = lookAtJointTransform.position.y;
  80. lookAtPosition.z = player.hmdTransform.position.z;
  81. lookAtJointTransform.LookAt( lookAtPosition );
  82. }
  83. }
  84. //-------------------------------------------------
  85. public override bool ShouldActivate( Vector3 playerPosition )
  86. {
  87. return ( Vector3.Distance( transform.position, playerPosition ) > 1.0f );
  88. }
  89. //-------------------------------------------------
  90. public override bool ShouldMovePlayer()
  91. {
  92. return true;
  93. }
  94. //-------------------------------------------------
  95. public override void Highlight( bool highlight )
  96. {
  97. if ( !locked )
  98. {
  99. if ( highlight )
  100. {
  101. SetMeshMaterials( Teleport.instance.pointHighlightedMaterial, titleHighlightedColor );
  102. }
  103. else
  104. {
  105. SetMeshMaterials( Teleport.instance.pointVisibleMaterial, titleVisibleColor );
  106. }
  107. }
  108. if ( highlight )
  109. {
  110. pointIcon.gameObject.SetActive( true );
  111. animation.Play();
  112. }
  113. else
  114. {
  115. pointIcon.gameObject.SetActive( false );
  116. animation.Stop();
  117. }
  118. }
  119. //-------------------------------------------------
  120. public override void UpdateVisuals()
  121. {
  122. if ( !gotReleventComponents )
  123. {
  124. return;
  125. }
  126. if ( locked )
  127. {
  128. SetMeshMaterials( Teleport.instance.pointLockedMaterial, titleLockedColor );
  129. pointIcon = lockedIcon;
  130. animation.clip = animation.GetClip( lockedAnimation );
  131. }
  132. else
  133. {
  134. SetMeshMaterials( Teleport.instance.pointVisibleMaterial, titleVisibleColor );
  135. switch ( teleportType )
  136. {
  137. case TeleportPointType.MoveToLocation:
  138. {
  139. pointIcon = moveLocationIcon;
  140. animation.clip = animation.GetClip( moveLocationAnimation );
  141. }
  142. break;
  143. case TeleportPointType.SwitchToNewScene:
  144. {
  145. pointIcon = switchSceneIcon;
  146. animation.clip = animation.GetClip( switchSceneAnimation );
  147. }
  148. break;
  149. }
  150. }
  151. titleText.text = title;
  152. }
  153. //-------------------------------------------------
  154. public override void SetAlpha( float tintAlpha, float alphaPercent )
  155. {
  156. tintColor = markerMesh.material.GetColor( tintColorID );
  157. tintColor.a = tintAlpha;
  158. markerMesh.material.SetColor( tintColorID, tintColor );
  159. switchSceneIcon.material.SetColor( tintColorID, tintColor );
  160. moveLocationIcon.material.SetColor( tintColorID, tintColor );
  161. lockedIcon.material.SetColor( tintColorID, tintColor );
  162. titleColor.a = fullTitleAlpha * alphaPercent;
  163. titleText.color = titleColor;
  164. }
  165. //-------------------------------------------------
  166. public void SetMeshMaterials( Material material, Color textColor )
  167. {
  168. markerMesh.material = material;
  169. switchSceneIcon.material = material;
  170. moveLocationIcon.material = material;
  171. lockedIcon.material = material;
  172. titleColor = textColor;
  173. fullTitleAlpha = textColor.a;
  174. titleText.color = titleColor;
  175. }
  176. //-------------------------------------------------
  177. public void TeleportToScene()
  178. {
  179. if ( !string.IsNullOrEmpty( switchToScene ) )
  180. {
  181. Debug.Log( "TeleportPoint: Hook up your level loading logic to switch to new scene: " + switchToScene );
  182. }
  183. else
  184. {
  185. Debug.LogError( "TeleportPoint: Invalid scene name to switch to: " + switchToScene );
  186. }
  187. }
  188. //-------------------------------------------------
  189. public void GetRelevantComponents()
  190. {
  191. markerMesh = transform.Find( "teleport_marker_mesh" ).GetComponent<MeshRenderer>();
  192. switchSceneIcon = transform.Find( "teleport_marker_lookat_joint/teleport_marker_icons/switch_scenes_icon" ).GetComponent<MeshRenderer>();
  193. moveLocationIcon = transform.Find( "teleport_marker_lookat_joint/teleport_marker_icons/move_location_icon" ).GetComponent<MeshRenderer>();
  194. lockedIcon = transform.Find( "teleport_marker_lookat_joint/teleport_marker_icons/locked_icon" ).GetComponent<MeshRenderer>();
  195. lookAtJointTransform = transform.Find( "teleport_marker_lookat_joint" );
  196. titleText = transform.Find( "teleport_marker_lookat_joint/teleport_marker_canvas/teleport_marker_canvas_text" ).GetComponent<Text>();
  197. gotReleventComponents = true;
  198. }
  199. //-------------------------------------------------
  200. public void ReleaseRelevantComponents()
  201. {
  202. markerMesh = null;
  203. switchSceneIcon = null;
  204. moveLocationIcon = null;
  205. lockedIcon = null;
  206. lookAtJointTransform = null;
  207. titleText = null;
  208. }
  209. //-------------------------------------------------
  210. public void UpdateVisualsInEditor()
  211. {
  212. if ( Application.isPlaying )
  213. {
  214. return;
  215. }
  216. GetRelevantComponents();
  217. if ( locked )
  218. {
  219. lockedIcon.gameObject.SetActive( true );
  220. moveLocationIcon.gameObject.SetActive( false );
  221. switchSceneIcon.gameObject.SetActive( false );
  222. markerMesh.sharedMaterial = Teleport.instance.pointLockedMaterial;
  223. lockedIcon.sharedMaterial = Teleport.instance.pointLockedMaterial;
  224. titleText.color = titleLockedColor;
  225. }
  226. else
  227. {
  228. lockedIcon.gameObject.SetActive( false );
  229. markerMesh.sharedMaterial = Teleport.instance.pointVisibleMaterial;
  230. switchSceneIcon.sharedMaterial = Teleport.instance.pointVisibleMaterial;
  231. moveLocationIcon.sharedMaterial = Teleport.instance.pointVisibleMaterial;
  232. titleText.color = titleVisibleColor;
  233. switch ( teleportType )
  234. {
  235. case TeleportPointType.MoveToLocation:
  236. {
  237. moveLocationIcon.gameObject.SetActive( true );
  238. switchSceneIcon.gameObject.SetActive( false );
  239. }
  240. break;
  241. case TeleportPointType.SwitchToNewScene:
  242. {
  243. moveLocationIcon.gameObject.SetActive( false );
  244. switchSceneIcon.gameObject.SetActive( true );
  245. }
  246. break;
  247. }
  248. }
  249. titleText.text = title;
  250. ReleaseRelevantComponents();
  251. }
  252. }
  253. #if UNITY_EDITOR
  254. //-------------------------------------------------------------------------
  255. [CustomEditor( typeof( TeleportPoint ) )]
  256. public class TeleportPointEditor : Editor
  257. {
  258. //-------------------------------------------------
  259. void OnEnable()
  260. {
  261. if ( Selection.activeTransform )
  262. {
  263. TeleportPoint teleportPoint = Selection.activeTransform.GetComponent<TeleportPoint>();
  264. teleportPoint.UpdateVisualsInEditor();
  265. }
  266. }
  267. //-------------------------------------------------
  268. public override void OnInspectorGUI()
  269. {
  270. DrawDefaultInspector();
  271. if ( Selection.activeTransform )
  272. {
  273. TeleportPoint teleportPoint = Selection.activeTransform.GetComponent<TeleportPoint>();
  274. if ( GUI.changed )
  275. {
  276. teleportPoint.UpdateVisualsInEditor();
  277. }
  278. }
  279. }
  280. }
  281. #endif
  282. }