VRTK_DestinationMarker脚本简析(VRTK_v3.3.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;
    }
}
  }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 228,398评论 6 532
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 98,510评论 3 416
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 176,346评论 0 374
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 62,972评论 1 311
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 71,739评论 6 410
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 55,196评论 1 324
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 43,260评论 3 441
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 42,413评论 0 288
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 48,951评论 1 336
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 40,779评论 3 354
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 42,980评论 1 369
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 38,522评论 5 359
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 44,217评论 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 34,647评论 0 26
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 35,887评论 1 286
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 51,659评论 3 391
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 47,967评论 2 374

推荐阅读更多精彩内容