123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352 |
- //======= Copyright (c) Valve Corporation, All rights reserved. ===============
- //
- // Purpose: Single location that the player can teleport to
- //
- //=============================================================================
- using UnityEngine;
- using UnityEngine.UI;
- #if UNITY_EDITOR
- using UnityEditor;
- #endif
- namespace Valve.VR.InteractionSystem
- {
- //-------------------------------------------------------------------------
- public class TeleportPoint : TeleportMarkerBase
- {
- public enum TeleportPointType
- {
- MoveToLocation,
- SwitchToNewScene
- };
- //Public variables
- public TeleportPointType teleportType = TeleportPointType.MoveToLocation;
- public string title;
- public string switchToScene;
- public Color titleVisibleColor;
- public Color titleHighlightedColor;
- public Color titleLockedColor;
- public bool playerSpawnPoint = false;
- //Private data
- private bool gotReleventComponents = false;
- private MeshRenderer markerMesh;
- private MeshRenderer switchSceneIcon;
- private MeshRenderer moveLocationIcon;
- private MeshRenderer lockedIcon;
- private MeshRenderer pointIcon;
- private Transform lookAtJointTransform;
- private new Animation animation;
- private Text titleText;
- private Player player;
- private Vector3 lookAtPosition = Vector3.zero;
- private int tintColorID = 0;
- private Color tintColor = Color.clear;
- private Color titleColor = Color.clear;
- private float fullTitleAlpha = 0.0f;
- //Constants
- private const string switchSceneAnimation = "switch_scenes_idle";
- private const string moveLocationAnimation = "move_location_idle";
- private const string lockedAnimation = "locked_idle";
- //-------------------------------------------------
- public override bool showReticle
- {
- get
- {
- return false;
- }
- }
- //-------------------------------------------------
- void Awake()
- {
- GetRelevantComponents();
- animation = GetComponent<Animation>();
- #if UNITY_URP
- tintColorID = Shader.PropertyToID( "_BaseColor" );
- #else
- tintColorID = Shader.PropertyToID("_TintColor");
- #endif
- moveLocationIcon.gameObject.SetActive( false );
- switchSceneIcon.gameObject.SetActive( false );
- lockedIcon.gameObject.SetActive( false );
- UpdateVisuals();
- }
- //-------------------------------------------------
- void Start()
- {
- player = Player.instance;
- }
- //-------------------------------------------------
- void Update()
- {
- if ( Application.isPlaying )
- {
- lookAtPosition.x = player.hmdTransform.position.x;
- lookAtPosition.y = lookAtJointTransform.position.y;
- lookAtPosition.z = player.hmdTransform.position.z;
- lookAtJointTransform.LookAt( lookAtPosition );
- }
- }
- //-------------------------------------------------
- public override bool ShouldActivate( Vector3 playerPosition )
- {
- return ( Vector3.Distance( transform.position, playerPosition ) > 1.0f );
- }
- //-------------------------------------------------
- public override bool ShouldMovePlayer()
- {
- return true;
- }
- //-------------------------------------------------
- public override void Highlight( bool highlight )
- {
- if ( !locked )
- {
- if ( highlight )
- {
- SetMeshMaterials( Teleport.instance.pointHighlightedMaterial, titleHighlightedColor );
- }
- else
- {
- SetMeshMaterials( Teleport.instance.pointVisibleMaterial, titleVisibleColor );
- }
- }
- if ( highlight )
- {
- pointIcon.gameObject.SetActive( true );
- animation.Play();
- }
- else
- {
- pointIcon.gameObject.SetActive( false );
- animation.Stop();
- }
- }
- //-------------------------------------------------
- public override void UpdateVisuals()
- {
- if ( !gotReleventComponents )
- {
- return;
- }
- if ( locked )
- {
- SetMeshMaterials( Teleport.instance.pointLockedMaterial, titleLockedColor );
- pointIcon = lockedIcon;
- animation.clip = animation.GetClip( lockedAnimation );
- }
- else
- {
- SetMeshMaterials( Teleport.instance.pointVisibleMaterial, titleVisibleColor );
- switch ( teleportType )
- {
- case TeleportPointType.MoveToLocation:
- {
- pointIcon = moveLocationIcon;
- animation.clip = animation.GetClip( moveLocationAnimation );
- }
- break;
- case TeleportPointType.SwitchToNewScene:
- {
- pointIcon = switchSceneIcon;
- animation.clip = animation.GetClip( switchSceneAnimation );
- }
- break;
- }
- }
- titleText.text = title;
- }
- //-------------------------------------------------
- public override void SetAlpha( float tintAlpha, float alphaPercent )
- {
- tintColor = markerMesh.material.GetColor( tintColorID );
- tintColor.a = tintAlpha;
- markerMesh.material.SetColor( tintColorID, tintColor );
- switchSceneIcon.material.SetColor( tintColorID, tintColor );
- moveLocationIcon.material.SetColor( tintColorID, tintColor );
- lockedIcon.material.SetColor( tintColorID, tintColor );
- titleColor.a = fullTitleAlpha * alphaPercent;
- titleText.color = titleColor;
- }
- //-------------------------------------------------
- public void SetMeshMaterials( Material material, Color textColor )
- {
- markerMesh.material = material;
- switchSceneIcon.material = material;
- moveLocationIcon.material = material;
- lockedIcon.material = material;
- titleColor = textColor;
- fullTitleAlpha = textColor.a;
- titleText.color = titleColor;
- }
- //-------------------------------------------------
- public void TeleportToScene()
- {
- if ( !string.IsNullOrEmpty( switchToScene ) )
- {
- Debug.Log("<b>[SteamVR Interaction]</b> TeleportPoint: Hook up your level loading logic to switch to new scene: " + switchToScene, this);
- }
- else
- {
- Debug.LogError("<b>[SteamVR Interaction]</b> TeleportPoint: Invalid scene name to switch to: " + switchToScene, this);
- }
- }
- //-------------------------------------------------
- public void GetRelevantComponents()
- {
- markerMesh = transform.Find( "teleport_marker_mesh" ).GetComponent<MeshRenderer>();
- switchSceneIcon = transform.Find( "teleport_marker_lookat_joint/teleport_marker_icons/switch_scenes_icon" ).GetComponent<MeshRenderer>();
- moveLocationIcon = transform.Find( "teleport_marker_lookat_joint/teleport_marker_icons/move_location_icon" ).GetComponent<MeshRenderer>();
- lockedIcon = transform.Find( "teleport_marker_lookat_joint/teleport_marker_icons/locked_icon" ).GetComponent<MeshRenderer>();
- lookAtJointTransform = transform.Find( "teleport_marker_lookat_joint" );
- titleText = transform.Find( "teleport_marker_lookat_joint/teleport_marker_canvas/teleport_marker_canvas_text" ).GetComponent<Text>();
- gotReleventComponents = true;
- }
- //-------------------------------------------------
- public void ReleaseRelevantComponents()
- {
- markerMesh = null;
- switchSceneIcon = null;
- moveLocationIcon = null;
- lockedIcon = null;
- lookAtJointTransform = null;
- titleText = null;
- }
- //-------------------------------------------------
- public void UpdateVisualsInEditor()
- {
- if ( Application.isPlaying )
- {
- return;
- }
- GetRelevantComponents();
- if ( locked )
- {
- lockedIcon.gameObject.SetActive( true );
- moveLocationIcon.gameObject.SetActive( false );
- switchSceneIcon.gameObject.SetActive( false );
- markerMesh.sharedMaterial = Teleport.instance.pointLockedMaterial;
- lockedIcon.sharedMaterial = Teleport.instance.pointLockedMaterial;
- titleText.color = titleLockedColor;
- }
- else
- {
- lockedIcon.gameObject.SetActive( false );
- markerMesh.sharedMaterial = Teleport.instance.pointVisibleMaterial;
- switchSceneIcon.sharedMaterial = Teleport.instance.pointVisibleMaterial;
- moveLocationIcon.sharedMaterial = Teleport.instance.pointVisibleMaterial;
- titleText.color = titleVisibleColor;
- switch ( teleportType )
- {
- case TeleportPointType.MoveToLocation:
- {
- moveLocationIcon.gameObject.SetActive( true );
- switchSceneIcon.gameObject.SetActive( false );
- }
- break;
- case TeleportPointType.SwitchToNewScene:
- {
- moveLocationIcon.gameObject.SetActive( false );
- switchSceneIcon.gameObject.SetActive( true );
- }
- break;
- }
- }
- titleText.text = title;
- ReleaseRelevantComponents();
- }
- }
- #if UNITY_EDITOR
- //-------------------------------------------------------------------------
- [CustomEditor( typeof( TeleportPoint ) )]
- public class TeleportPointEditor : Editor
- {
- //-------------------------------------------------
- void OnEnable()
- {
- if ( Selection.activeTransform )
- {
- TeleportPoint teleportPoint = Selection.activeTransform.GetComponent<TeleportPoint>();
- if (teleportPoint != null)
- teleportPoint.UpdateVisualsInEditor();
- }
- }
- //-------------------------------------------------
- public override void OnInspectorGUI()
- {
- DrawDefaultInspector();
- if ( Selection.activeTransform )
- {
- TeleportPoint teleportPoint = Selection.activeTransform.GetComponent<TeleportPoint>();
- if ( GUI.changed )
- {
- teleportPoint.UpdateVisualsInEditor();
- }
- }
- }
- }
- #endif
- }
|