美文网首页VRTK3.3.0版本脚本简析
VRTK_HeadsetCollisionFade脚本简析(VR

VRTK_HeadsetCollisionFade脚本简析(VR

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

    当检测到眼镜即Camera的碰撞事件时,启动眼镜视图的淡入淡出。
    PlayArea对象上挂在此脚本;

    参数:

        public float timeTillFade = 0f;//等待淡入的时间
        public float blinkTransitionSpeed = 0.1f;//碰撞时的闪烁速度
        public Color fadeColor = Color.black;//指定颜色
        public VRTK_PolicyList targetListPolicy;//类
        public VRTK_HeadsetCollision headsetCollision;    //类   
        public VRTK_HeadsetFade headsetFade;//类
    

    protected修饰的方法:

    OnEnable()
    获取这两个类:headsetFade headsetCollision
    headsetCollision注册两个事件
    OnDisable()
    headsetCollision移除两个事件

    事件绑定的方法:

    OnHeadsetCollisionDetect(): 里面执行 Invoke("StartFade", timeTillFade);
    OnHeadsetCollisionEnded(): 里面执行CancelInvoke("StartFade");
    headsetFade.Unfade(blinkTransitionSpeed);

    StartFade() 开始颜色变黑,调用的VRTK_HeadsetFade类里的Fade方法
    ValidTarget() 返回bool值,判断碰撞后是否需要颜色变黑

        // Headset Collision Fade|Presence|70030
         namespace VRTK
       {
      using UnityEngine;
    /// <summary>
    /// Initiates a fade of the headset view when a headset collision event is detected.
    /// </summary>
    /// <remarks>
    /// **Required Components:**
    ///  * `VRTK_HeadsetCollision` - A Headset Collision script to determine when the HMD has collided with valid geometry.
    ///  * `VRTK_HeadsetFade` - A Headset Fade script to alter the visible colour on the HMD view.
    ///
    /// **Script Usage:**
    ///  * Place the `VRTK_HeadsetCollisionFade` 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_HeadsetCollisionFade")]
    public class VRTK_HeadsetCollisionFade : MonoBehaviour
    {
        [Header("Collision Fade Settings")]
    
        [Tooltip("The amount of time to wait until a fade occurs.")]
        public float timeTillFade = 0f;//等待淡入的时间
        [Tooltip("The fade blink speed on collision.")]
        public float blinkTransitionSpeed = 0.1f;//碰撞时的闪烁速度
        [Tooltip("The colour to fade the headset to on collision.")]
        public Color fadeColor = Color.black;//碰撞后的颜色
        [Tooltip("A specified VRTK_PolicyList to use to determine whether any objects will be acted upon by the Headset Collision Fade.")]
        public VRTK_PolicyList targetListPolicy;//指定的VRTK_PolicyList类,用于确定耳机冲突是否会对任何对象起作用
    
        [Header("Custom Settings")]
    
        [Tooltip("The VRTK Headset Collision script to use when determining headset collisions. If this is left blank then the script will need to be applied to the same GameObject.")]
        public VRTK_HeadsetCollision headsetCollision;//确定耳机碰撞的类 determining headset collisions
        [Tooltip("The VRTK Headset Fade script to use when fading the headset. If this is left blank then the script will need to be applied to the same GameObject.")]
        public VRTK_HeadsetFade headsetFade;//负责褪色的类
    
        protected virtual void OnEnable()
        {
            headsetFade = (headsetFade != null ? headsetFade : FindObjectOfType<VRTK_HeadsetFade>());
            headsetCollision = (headsetCollision != null ? headsetCollision : FindObjectOfType<VRTK_HeadsetCollision>());
    
            if (headsetFade == null)
            {
                VRTK_Logger.Error(VRTK_Logger.GetCommonMessage(VRTK_Logger.CommonMessageKeys.REQUIRED_COMPONENT_MISSING_FROM_GAMEOBJECT, "VRTK_HeadsetCollisionFade", "VRTK_HeadsetFade", "the same or child"));
                return;
            }
    
            if (headsetCollision == null)
            {
                VRTK_Logger.Error(VRTK_Logger.GetCommonMessage(VRTK_Logger.CommonMessageKeys.REQUIRED_COMPONENT_MISSING_FROM_GAMEOBJECT, "VRTK_HeadsetCollisionFade", "VRTK_HeadsetCollision", "the same or child"));
                return;
            }
    
            headsetCollision.HeadsetCollisionDetect += new HeadsetCollisionEventHandler(OnHeadsetCollisionDetect);
            headsetCollision.HeadsetCollisionEnded += new HeadsetCollisionEventHandler(OnHeadsetCollisionEnded);
        }
    
        protected virtual void OnDisable()
        {
            if (headsetCollision != null)
            {
                headsetCollision.HeadsetCollisionDetect -= new HeadsetCollisionEventHandler(OnHeadsetCollisionDetect);
                headsetCollision.HeadsetCollisionEnded -= new HeadsetCollisionEventHandler(OnHeadsetCollisionEnded);
            }
        }
    
        protected virtual void OnHeadsetCollisionDetect(object sender, HeadsetCollisionEventArgs e)
        {
            if (ValidTarget(e.collider))
            {
                Invoke("StartFade", timeTillFade);
            }
        }
    
        protected virtual void OnHeadsetCollisionEnded(object sender, HeadsetCollisionEventArgs e)
        {
            if (ValidTarget(e.collider))
            {
                CancelInvoke("StartFade");
                headsetFade.Unfade(blinkTransitionSpeed);
            }
        }
    
        protected virtual void StartFade()
        {
            headsetFade.Fade(fadeColor, blinkTransitionSpeed);
        }
    
        protected virtual bool ValidTarget(Collider target)
        {
            return (target != null && !(VRTK_PolicyList.Check(target.gameObject, targetListPolicy)));
        }
    }
    

    }

    相关文章

      网友评论

        本文标题:VRTK_HeadsetCollisionFade脚本简析(VR

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