此腳本作用是持續(xù)時(shí)間內(nèi)將眼鏡的視圖顏色更改為指定的顏色;
在場(chǎng)景區(qū)域周圍有碰撞的墻壁時(shí),如果用戶將頭部插入墻壁,則視圖會(huì)褪色為指定的顏色,一般選黑色;
PlayArea對(duì)象上掛在此腳本;
命名空間:VRTK
HeadsetFadeEventArgs 結(jié)構(gòu)體
眼鏡淡出事件參數(shù) 此處是結(jié)構(gòu)體包含兩個(gè)參數(shù):
public float timeTillComplete;// 時(shí)間
public Transform currentTransform;//當(dāng)前對(duì)象Camera
委托:
public delegate void HeadsetFadeEventHandler(object sender, HeadsetFadeEventArgs e); 眼鏡淡出事件處理的委托 參數(shù)有發(fā)出者,上述結(jié)構(gòu)體參數(shù)
事件:
對(duì)應(yīng)上述委托四個(gè)事件
public event HeadsetFadeEventHandler HeadsetFadeStart; //當(dāng)用戶的頭戴式設(shè)備開(kāi)始褪色到特定顏色時(shí)發(fā)出。
public event HeadsetFadeEventHandler HeadsetFadeComplete;//當(dāng)用戶的頭戴式設(shè)備完成淡入淡出并且現(xiàn)在完全處于給定顏色時(shí)發(fā)出。
public event HeadsetFadeEventHandler HeadsetUnfadeStart;//當(dāng)用戶的耳機(jī)開(kāi)始恢復(fù)到透明顏色時(shí)發(fā)出。
public event HeadsetFadeEventHandler HeadsetUnfadeComplete;//當(dāng)用戶的頭戴式設(shè)備完成非透明設(shè)置并再次完全透明時(shí)發(fā)出。
三個(gè)用到的字段:
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);
}
}
上述是處理四個(gè)事件的方法,可以進(jìn)行重寫(xiě);
IsFaded()方法返回isFaded 的bool值,如果當(dāng)前正在褪色或已經(jīng)完全褪色,返回true,如果完全未褪色返回false;
IsTransitioning()方法返回isTransitioning 的bool值,如果眼鏡當(dāng)前正在褪色或不褪色返回真,如果眼鏡視野完全褪色或未褪色返回假;
Fade(Color color, float duration)方法:在給定的持續(xù)時(shí)間duration內(nèi)將耳機(jī)視圖的顏色更改為給定的顏色color;
Unfade(float duration)方法:在給定的時(shí)間內(nèi)將顏色更改為透明顏色。
余下方法是protected修飾的,腳本內(nèi)部調(diào)用的;
Awake() 調(diào)用VRTK_SDKManager類里的一個(gè)Add方法
OnEnable() 給三個(gè)參數(shù)賦初始值和 調(diào)用VRTK_SharedMethods類的一個(gè)AddCameraFade方法;
OnDestroy() 調(diào)用VRTK_SDKManager類里的一個(gè)Remove方法,和Awake相呼應(yīng);
SetHeadsetFadeEvent方法:返回上面定義的一個(gè)枚舉;
FadeComplete()方法:Fade和UnFade里調(diào)用;
UnfadeComplete()方法:Fade和UnFade里調(diào)用;
// 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;//此時(shí)為假
isTransitioning = true;//此時(shí)為真
VRTK_SDK_Bridge.HeadsetFade(color, duration);
OnHeadsetFadeStart(SetHeadsetFadeEvent(headset, duration));//執(zhí)行事件SetHeadsetFadeEvent(headset, duration)方法返回定義的的枚舉
CancelInvoke("UnfadeComplete");//取消UnfadeComplete方法在這個(gè)腳本中所有的調(diào)用
Invoke("FadeComplete", duration);// duration時(shí)間后執(zhí)行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));//事件執(zhí)行
}
protected virtual void UnfadeComplete()
{
isFaded = false;//恢復(fù)透明色完成
isTransitioning = false;
OnHeadsetUnfadeComplete(SetHeadsetFadeEvent(headset, 0f));
}
}
}