VRTK_HeadsetFade脚本简析(VRTK_v3.3.0版)

此脚本作用是持续时间内将眼镜的视图颜色更改为指定的颜色;
在场景区域周围有碰撞的墙壁时,如果用户将头部插入墙壁,则视图会褪色为指定的颜色,一般选黑色;
PlayArea对象上挂在此脚本;
命名空间:VRTK

HeadsetFadeEventArgs 结构体

眼镜淡出事件参数 此处是结构体包含两个参数:
public float timeTillComplete;// 时间
public Transform currentTransform;//当前对象Camera

委托:

public delegate void HeadsetFadeEventHandler(object sender, HeadsetFadeEventArgs e); 眼镜淡出事件处理的委托 参数有发出者,上述结构体参数

事件:

对应上述委托四个事件
public event HeadsetFadeEventHandler HeadsetFadeStart; //当用户的头戴式设备开始褪色到特定颜色时发出。
public event HeadsetFadeEventHandler HeadsetFadeComplete;//当用户的头戴式设备完成淡入淡出并且现在完全处于给定颜色时发出。
public event HeadsetFadeEventHandler HeadsetUnfadeStart;//当用户的耳机开始恢复到透明颜色时发出。
public event HeadsetFadeEventHandler HeadsetUnfadeComplete;//当用户的头戴式设备完成非透明设置并再次完全透明时发出。

三个用到的字段:

protected Transform headset;//头部
protected bool isTransitioning = false;
protected bool isFaded = false;

 public virtual void OnHeadsetFadeStart(HeadsetFadeEventArgs e)
    {
        if (HeadsetFadeStart != null)
        {
            HeadsetFadeStart(this, e);
        }
    }

    public virtual void OnHeadsetFadeComplete(HeadsetFadeEventArgs e)
    {
        if (HeadsetFadeComplete != null)
        {
            HeadsetFadeComplete(this, e);
        }
    }

    public virtual void OnHeadsetUnfadeStart(HeadsetFadeEventArgs e)
    {
        if (HeadsetUnfadeStart != null)
        {
            HeadsetUnfadeStart(this, e);
        }
    }

    public virtual void OnHeadsetUnfadeComplete(HeadsetFadeEventArgs e)
    {
        if (HeadsetUnfadeComplete != null)
        {
            HeadsetUnfadeComplete(this, e);
        }
    }

上述是处理四个事件的方法,可以进行重写;

IsFaded()方法返回isFaded 的bool值,如果当前正在褪色或已经完全褪色,返回true,如果完全未褪色返回false;
IsTransitioning()方法返回isTransitioning 的bool值,如果眼镜当前正在褪色或不褪色返回真,如果眼镜视野完全褪色或未褪色返回假;
Fade(Color color, float duration)方法:在给定的持续时间duration内将耳机视图的颜色更改为给定的颜色color;
Unfade(float duration)方法:在给定的时间内将颜色更改为透明颜色。

余下方法是protected修饰的,脚本内部调用的;

Awake() 调用VRTK_SDKManager类里的一个Add方法
OnEnable() 给三个参数赋初始值和 调用VRTK_SharedMethods类的一个AddCameraFade方法;
OnDestroy() 调用VRTK_SDKManager类里的一个Remove方法,和Awake相呼应;
SetHeadsetFadeEvent方法:返回上面定义的一个枚举;
FadeComplete()方法:Fade和UnFade里调用;
UnfadeComplete()方法:Fade和UnFade里调用;

 // Headset Fade|Presence|70020
namespace VRTK
{
 using UnityEngine;
/// <summary>
/// Event Payload
/// </summary>
/// <param name="timeTillComplete">A float that is the duration for the fade/unfade process has remaining.</param>
/// <param name="currentTransform">The current Transform of the object that the Headset Fade script is attached to (Camera).</param>
public struct HeadsetFadeEventArgs
{
    public float timeTillComplete;
    public Transform currentTransform;
}

/// <summary>
/// Event Payload
/// </summary>
/// <param name="sender">this object</param>
/// <param name="e"><see cref="HeadsetFadeEventArgs"/></param>
public delegate void HeadsetFadeEventHandler(object sender, HeadsetFadeEventArgs e);

/// <summary>
/// Provides the ability to change the colour of the headset view to a specified colour over a given duration.
/// </summary>
/// <remarks>
/// **Script Usage:**
///  * Place the `VRTK_HeadsetFade` script on any active scene GameObject.
/// </remarks>
/// <example>
/// `VRTK/Examples/011_Camera_HeadSetCollisionFading` has collidable walls around the play area and if the user puts their head into any of the walls then the headset will fade to black.
/// </example>
[AddComponentMenu("VRTK/Scripts/Presence/VRTK_HeadsetFade")]
public class VRTK_HeadsetFade : MonoBehaviour
{
    /// <summary>
    /// Emitted when the user's headset begins to fade to a given colour.
    /// </summary>
    public event HeadsetFadeEventHandler HeadsetFadeStart;
    /// <summary>
    /// Emitted when the user's headset has completed the fade and is now fully at the given colour.
    /// </summary>
    public event HeadsetFadeEventHandler HeadsetFadeComplete;
    /// <summary>
    /// Emitted when the user's headset begins to unfade back to a transparent colour.
    /// </summary>
    public event HeadsetFadeEventHandler HeadsetUnfadeStart;
    /// <summary>
    /// Emitted when the user's headset has completed unfading and is now fully transparent again.
    /// </summary>
    public event HeadsetFadeEventHandler HeadsetUnfadeComplete;

    protected Transform headset;
    protected bool isTransitioning = false;
    protected bool isFaded = false;

    public virtual void OnHeadsetFadeStart(HeadsetFadeEventArgs e)
    {
        if (HeadsetFadeStart != null)
        {
            HeadsetFadeStart(this, e);
        }
    }

    public virtual void OnHeadsetFadeComplete(HeadsetFadeEventArgs e)
    {
        if (HeadsetFadeComplete != null)
        {
            HeadsetFadeComplete(this, e);
        }
    }

    public virtual void OnHeadsetUnfadeStart(HeadsetFadeEventArgs e)
    {
        if (HeadsetUnfadeStart != null)
        {
            HeadsetUnfadeStart(this, e);
        }
    }

    public virtual void OnHeadsetUnfadeComplete(HeadsetFadeEventArgs e)
    {
        if (HeadsetUnfadeComplete != null)
        {
            HeadsetUnfadeComplete(this, e);
        }
    }

    /// <summary>
    /// The IsFaded method returns true if the headset is currently fading or has completely faded and returns false if it is completely unfaded.
    /// </summary>
    /// <returns>Returns `true` if the headset is currently fading or faded.</returns>
    public virtual bool IsFaded()
    {
        return isFaded;
    }

    /// <summary>
    /// The IsTransitioning method returns true if the headset is currently fading or unfading and returns false if it is completely faded or unfaded.
    /// </summary>
    /// <returns>Returns `true` if the headset is currently in the process of fading or unfading.</returns>
    public virtual bool IsTransitioning()
    {
        return isTransitioning;
    }

    /// <summary>
    /// The Fade method initiates a change in the colour of the headset view to the given colour over a given duration.
    /// </summary>
    /// <param name="color">The colour to fade the headset view to.</param>
    /// <param name="duration">The time in seconds to take to complete the fade transition.</param>
    public virtual void Fade(Color color, float duration)//
    {
        isFaded = false;//此时为假
        isTransitioning = true;//此时为真
        VRTK_SDK_Bridge.HeadsetFade(color, duration);
        OnHeadsetFadeStart(SetHeadsetFadeEvent(headset, duration));//执行事件SetHeadsetFadeEvent(headset, duration)方法返回定义的的枚举
        CancelInvoke("UnfadeComplete");//取消UnfadeComplete方法在这个脚本中所有的调用 
        Invoke("FadeComplete", duration);// duration时间后执行FadeComplete方法
    }

    /// <summary>
    /// The Unfade method initiates the headset to change colour back to a transparent colour over a given duration.
    /// </summary>
    /// <param name="duration">The time in seconds to take to complete the unfade transition.</param>
    public virtual void Unfade(float duration)
    {
        isFaded = true;
        isTransitioning = true;
        VRTK_SDK_Bridge.HeadsetFade(Color.clear, duration);
        OnHeadsetUnfadeStart(SetHeadsetFadeEvent(headset, duration));
        CancelInvoke("FadeComplete");
        Invoke("UnfadeComplete", duration);
    }

    protected virtual void Awake()
    {
        VRTK_SDKManager.AttemptAddBehaviourToToggleOnLoadedSetupChange(this);
    }

    protected virtual void OnEnable()
    {
        headset = VRTK_DeviceFinder.HeadsetCamera();
        isTransitioning = false;
        isFaded = false;

        VRTK_SharedMethods.AddCameraFade();
    }

    protected virtual void OnDestroy()
    {
        VRTK_SDKManager.AttemptRemoveBehaviourToToggleOnLoadedSetupChange(this);
    }

    protected virtual HeadsetFadeEventArgs SetHeadsetFadeEvent(Transform currentTransform, float duration)
    {
        HeadsetFadeEventArgs e;
        e.timeTillComplete = duration;
        e.currentTransform = currentTransform;
        return e;
    }

    protected virtual void FadeComplete()
    {
        isFaded = true;//谈出完成
        isTransitioning = false;
        OnHeadsetFadeComplete(SetHeadsetFadeEvent(headset, 0f));//事件执行
    }

    protected virtual void UnfadeComplete()
    {
        isFaded = false;//恢复透明色完成
        isTransitioning = false;
        OnHeadsetUnfadeComplete(SetHeadsetFadeEvent(headset, 0f));
    }
}
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,524评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,869评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,813评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,210评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,085评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,117评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,533评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,219评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,487评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,582评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,362评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,218评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,589评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,899评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,176评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,503评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,707评论 2 335