美文网首页VRTK3.3.0版本脚本简析
VRTK_DestinationMarker脚本简析(VRTK_

VRTK_DestinationMarker脚本简析(VRTK_

作者: HMY轩园 | 来源:发表于2019-04-10 18:12 被阅读0次

    指针父类:提供所有目标标记可以继承的基础
    这是一个抽象类,它将继承到提供对象控制操作功能的具体类,因此不应直接使用此脚本。
    将VRTK_DestinationMarker_UnityEvents组件添加到VRTK_DestinationMarker对象允许访问UnityEvents它将对类事件做出相同的反应。
    所有C#委托事件都映射到带有On前缀的Unity事件。例如MyEvent- > OnMyEvent。

    结构体

    DestinationMarkerEventArgs
    float distance : 原点和碰撞目的地之间的距离。
    Transform target : 碰撞目标对象的变换。
    RaycastHit raycastHit:射线碰撞时生成的可选RaycastHit。
    Vector3 destinationPosition : 目标标记的世界位置。
    Quaternion? destinationRotation : 目标标记的世界旋转。
    bool forceDestinationPosition : 如果为true,那么消耗有效负载的任何东西都不应改变给定的目标位置。
    bool enableTeleport : 目的地设定事件是否应触发传送。
    VRTK_ControllerReference controllerReference : 控制目标标记的控制器的可选参考。

    委托

    public delegate void DestinationMarkerEventHandler(object sender, DestinationMarkerEventArgs e);

    字段

    public bool enableTeleport = true;//如果选中此选项,则目标设置事件中的teleport标志设置为true,因此teleport脚本将知道是否要操作新目标。
    public VRTK_PolicyList targetListPolicy;用于确定目标目标是有效还是无效的指定VRTK_PolicyList。

    事件

        public event DestinationMarkerEventHandler DestinationMarkerEnter;//当第一次发生与另一台对撞机的碰撞时发出
        public event DestinationMarkerEventHandler DestinationMarkerExit;  //当与其他对撞机的碰撞结束时发出。     
        public event DestinationMarkerEventHandler DestinationMarkerHover;  //  当现有对撞机继续发生碰撞时发出。
        public event DestinationMarkerEventHandler DestinationMarkerSet;//当目标标记在场景中处于活动状态时发出,以确定最后的目标位置(用于选择和远程迁移)。
    

    方法

    public virtual void SetNavMeshCheckDistance(float distance)

    public virtual void SetNavMeshData(VRTK_NavMeshData givenData) 根据给定NavMeshData对象中的设置将目标标记限制为场景NavMesh
    public virtual void SetHeadsetPositionCompensation(bool state)确定在设置目标标记时是否应考虑耳机距游戏区域中心的偏移位置。如果true那时它将考虑偏移位置。
    public virtual void SetForceHoverOnRepeatedEnter(bool state)设置如果现有碰撞对象与先前的enter调用相同,则Enter事件是否会强制调用Hover事件。

       // Destination Marker|Pointers|10010
      namespace VRTK
      {
    using UnityEngine;
    
    /// <summary>
    /// Event Payload
    /// </summary>
    /// <param name="distance">The distance between the origin and the collided destination.</param>
    /// <param name="target">The Transform of the collided destination object.</param>
    /// <param name="raycastHit">The optional RaycastHit generated from when the ray collided.</param>
    /// <param name="destinationPosition">The world position of the destination marker.</param>
    /// <param name="destinationRotation">The world rotation of the destination marker.</param>
    /// <param name="forceDestinationPosition">If true then the given destination position should not be altered by anything consuming the payload.</param>
    /// <param name="enableTeleport">Whether the destination set event should trigger teleport.</param>
    /// <param name="controllerReference">The optional reference to the controller controlling the destination marker.</param>
    public struct DestinationMarkerEventArgs
    {
        public float distance;
        public Transform target;
        public RaycastHit raycastHit;
        public Vector3 destinationPosition;
        public Quaternion? destinationRotation;
        public bool forceDestinationPosition;
        public bool enableTeleport;
        public VRTK_ControllerReference controllerReference;
    }
    
    /// <summary>
    /// Event Payload
    /// </summary>
    /// <param name="sender">this object</param>
    /// <param name="e"><see cref="DestinationMarkerEventArgs"/></param>
    public delegate void DestinationMarkerEventHandler(object sender, DestinationMarkerEventArgs e);
    
    /// <summary>
    /// Provides a base that all destination markers can inherit from.
    /// </summary>
    /// <remarks>
    /// **Script Usage:**
    ///   > This is an abstract class that is to be inherited to a concrete class that provides object control action functionality, therefore this script should not be directly used.
    /// </remarks>
    public abstract class VRTK_DestinationMarker : MonoBehaviour
    {
        [Header("Destination Marker Settings", order = 1)]
    
        [Tooltip("If this is checked then the teleport flag is set to true in the Destination Set event so teleport scripts will know whether to action the new destination.")]
        public bool enableTeleport = true;
        [Tooltip("A specified VRTK_PolicyList to use to determine whether destination targets will be considered valid or invalid.")]
        public VRTK_PolicyList targetListPolicy;
    
        /// <summary>
        /// Emitted when a collision with another collider has first occurred.
        /// </summary>
        public event DestinationMarkerEventHandler DestinationMarkerEnter;
        /// <summary>
        /// Emitted when the collision with the other collider ends.
        /// </summary>
        public event DestinationMarkerEventHandler DestinationMarkerExit;
        /// Emitted when a collision the existing collider is continuing.
        /// </summary>
        public event DestinationMarkerEventHandler DestinationMarkerHover;
        /// <summary>
        /// Emitted when the destination marker is active in the scene to determine the last destination position (useful for selecting and teleporting).
        /// </summary>
        public event DestinationMarkerEventHandler DestinationMarkerSet;
    
        [System.Obsolete("`VRTK_DestinationMarker.navMeshCheckDistance` is no longer used. This parameter will be removed in a future version of VRTK.")]
        protected float navMeshCheckDistance = 0f;
    
        protected VRTK_NavMeshData navmeshData;
        protected bool headsetPositionCompensation;
        protected bool forceHoverOnRepeatedEnter = true;
        protected Collider existingCollider;
    
        public virtual void OnDestinationMarkerEnter(DestinationMarkerEventArgs e)
        {
            if (DestinationMarkerEnter != null && (!forceHoverOnRepeatedEnter || (e.raycastHit.collider != existingCollider)))
            {
                existingCollider = e.raycastHit.collider;
                DestinationMarkerEnter(this, e);
            }
    
            if (forceHoverOnRepeatedEnter && e.raycastHit.collider == existingCollider)
            {
                OnDestinationMarkerHover(e);
            }
        }
    
        public virtual void OnDestinationMarkerExit(DestinationMarkerEventArgs e)
        {
            if (DestinationMarkerExit != null)
            {
                DestinationMarkerExit(this, e);
                existingCollider = null;
            }
        }
    
        public virtual void OnDestinationMarkerHover(DestinationMarkerEventArgs e)
        {
            if (DestinationMarkerHover != null)
            {
                DestinationMarkerHover(this, e);
            }
        }
    
        public virtual void OnDestinationMarkerSet(DestinationMarkerEventArgs e)
        {
            if (DestinationMarkerSet != null)
            {
                DestinationMarkerSet(this, e);
            }
        }
    
        /// <summary>
        /// The SetNavMeshCheckDistance method sets the max distance the destination marker position can be from the edge of a nav mesh to be considered a valid destination.
        /// </summary>
        /// <param name="distance">The max distance the nav mesh can be from the sample point to be valid.</param>
        [System.Obsolete("`DestinationMarker.SetNavMeshCheckDistance(distance)` has been replaced with the method `DestinationMarker.SetNavMeshCheckDistance(givenData)`. This method will be removed in a future version of VRTK.")]
        public virtual void SetNavMeshCheckDistance(float distance)
        {
            VRTK_NavMeshData givenData = gameObject.AddComponent<VRTK_NavMeshData>();
            givenData.distanceLimit = distance;
            SetNavMeshData(givenData);
        }
    
        /// <summary>
        /// The SetNavMeshData method is used to limit the destination marker to the scene NavMesh based on the settings in the given NavMeshData object.
        /// </summary>
        /// <param name="givenData">The NavMeshData object that contains the NavMesh restriction settings.</param>
        public virtual void SetNavMeshData(VRTK_NavMeshData givenData)
        {
            navmeshData = givenData;
        }
    
        /// <summary>
        /// The SetHeadsetPositionCompensation method determines whether the offset position of the headset from the centre of the play area should be taken into consideration when setting the destination marker. If `true` then it will take the offset position into consideration.
        /// </summary>
        /// <param name="state">The state of whether to take the position of the headset within the play area into account when setting the destination marker.</param>
        public virtual void SetHeadsetPositionCompensation(bool state)
        {
            headsetPositionCompensation = state;
        }
    
        /// <summary>
        /// The SetForceHoverOnRepeatedEnter method is used to set whether the Enter event will forciably call the Hover event if the existing colliding object is the same as it was the previous enter call.
        /// </summary>
        /// <param name="state">The state of whether to force the hover on or off.</param>
        public virtual void SetForceHoverOnRepeatedEnter(bool state)
        {
            forceHoverOnRepeatedEnter = state;
        }
    
        protected virtual void OnEnable()
        {
            VRTK_ObjectCache.registeredDestinationMarkers.Add(this);
        }
    
        protected virtual void OnDisable()
        {
            VRTK_ObjectCache.registeredDestinationMarkers.Remove(this);
        }
    
        protected virtual DestinationMarkerEventArgs SetDestinationMarkerEvent(float distance, Transform target, RaycastHit raycastHit, Vector3 position, VRTK_ControllerReference controllerReference, bool forceDestinationPosition = false, Quaternion? rotation = null)
        {
            DestinationMarkerEventArgs e;
            e.controllerReference = controllerReference;
            e.distance = distance;
            e.target = target;
            e.raycastHit = raycastHit;
            e.destinationPosition = position;
            e.destinationRotation = rotation;
            e.enableTeleport = enableTeleport;
            e.forceDestinationPosition = forceDestinationPosition;
            return e;
        }
    }
      }

    相关文章

      网友评论

        本文标题:VRTK_DestinationMarker脚本简析(VRTK_

        本文链接:https://www.haomeiwen.com/subject/zvpoiqtx.html