123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- //======= Copyright (c) Valve Corporation, All rights reserved. ===============
- //
- // Purpose: An area that the player can teleport to
- //
- //=============================================================================
- using UnityEngine;
- #if UNITY_EDITOR
- using UnityEditor;
- #endif
- namespace Valve.VR.InteractionSystem
- {
- //-------------------------------------------------------------------------
- public class TeleportArea : TeleportMarkerBase
- {
- //Public properties
- public Bounds meshBounds { get; private set; }
- //Private data
- private MeshRenderer areaMesh;
- private int tintColorId = 0;
- private Color visibleTintColor = Color.clear;
- private Color highlightedTintColor = Color.clear;
- private Color lockedTintColor = Color.clear;
- private bool highlighted = false;
- //-------------------------------------------------
- public void Awake()
- {
- areaMesh = GetComponent<MeshRenderer>();
- tintColorId = Shader.PropertyToID( "_TintColor" );
- CalculateBounds();
- }
- //-------------------------------------------------
- public void Start()
- {
- visibleTintColor = Teleport.instance.areaVisibleMaterial.GetColor( tintColorId );
- highlightedTintColor = Teleport.instance.areaHighlightedMaterial.GetColor( tintColorId );
- lockedTintColor = Teleport.instance.areaLockedMaterial.GetColor( tintColorId );
- }
- //-------------------------------------------------
- public override bool ShouldActivate( Vector3 playerPosition )
- {
- return true;
- }
- //-------------------------------------------------
- public override bool ShouldMovePlayer()
- {
- return true;
- }
- //-------------------------------------------------
- public override void Highlight( bool highlight )
- {
- if ( !locked )
- {
- highlighted = highlight;
- if ( highlight )
- {
- areaMesh.material = Teleport.instance.areaHighlightedMaterial;
- }
- else
- {
- areaMesh.material = Teleport.instance.areaVisibleMaterial;
- }
- }
- }
- //-------------------------------------------------
- public override void SetAlpha( float tintAlpha, float alphaPercent )
- {
- Color tintedColor = GetTintColor();
- tintedColor.a *= alphaPercent;
- areaMesh.material.SetColor( tintColorId, tintedColor );
- }
- //-------------------------------------------------
- public override void UpdateVisuals()
- {
- if ( locked )
- {
- areaMesh.material = Teleport.instance.areaLockedMaterial;
- }
- else
- {
- areaMesh.material = Teleport.instance.areaVisibleMaterial;
- }
- }
- //-------------------------------------------------
- public void UpdateVisualsInEditor()
- {
- if (Teleport.instance == null)
- return;
- areaMesh = GetComponent<MeshRenderer>();
- if ( locked )
- {
- areaMesh.sharedMaterial = Teleport.instance.areaLockedMaterial;
- }
- else
- {
- areaMesh.sharedMaterial = Teleport.instance.areaVisibleMaterial;
- }
- }
- //-------------------------------------------------
- private bool CalculateBounds()
- {
- MeshFilter meshFilter = GetComponent<MeshFilter>();
- if ( meshFilter == null )
- {
- return false;
- }
- Mesh mesh = meshFilter.sharedMesh;
- if ( mesh == null )
- {
- return false;
- }
- meshBounds = mesh.bounds;
- return true;
- }
- //-------------------------------------------------
- private Color GetTintColor()
- {
- if ( locked )
- {
- return lockedTintColor;
- }
- else
- {
- if ( highlighted )
- {
- return highlightedTintColor;
- }
- else
- {
- return visibleTintColor;
- }
- }
- }
- }
- #if UNITY_EDITOR
- //-------------------------------------------------------------------------
- [CustomEditor( typeof( TeleportArea ) )]
- public class TeleportAreaEditor : Editor
- {
- //-------------------------------------------------
- void OnEnable()
- {
- if ( Selection.activeTransform != null )
- {
- TeleportArea teleportArea = Selection.activeTransform.GetComponent<TeleportArea>();
- if ( teleportArea != null )
- {
- teleportArea.UpdateVisualsInEditor();
- }
- }
- }
- //-------------------------------------------------
- public override void OnInspectorGUI()
- {
- DrawDefaultInspector();
- if ( Selection.activeTransform != null )
- {
- TeleportArea teleportArea = Selection.activeTransform.GetComponent<TeleportArea>();
- if ( GUI.changed && teleportArea != null )
- {
- teleportArea.UpdateVisualsInEditor();
- }
- }
- }
- }
- #endif
- }
|