【问题标题】:is there a gaze only (as opposed to click/button input) mode in unity3d google vr sdk?unity3d google vr sdk 中是否只有凝视(与单击/按钮输入相反)模式?
【发布时间】:2019-12-30 10:21:37
【问题描述】:

我在 unity3d 中使用 google VR SDK(用于纸板),但我拥有的纸板没有按钮。有没有办法启用仅凝视模式,将扩展凝视(几秒钟)视为按钮点击? (我在一个教程视频中说“你可以启用仅注视模式”,但之后没有解释任何内容)这是一个内置功能还是我必须从头开始编写注视交互来实现这一点?

我到处搜索代码以启用此功能,不仅是在演示场景中可用的不同预制件上的编辑器上显示的公共道具。

【问题讨论】:

    标签: unity3d google-cardboard google-vr google-vr-sdk gaze-buttons


    【解决方案1】:

    我不知道如何或是否有内置方法。但是您可以轻松构建扩展按钮:

    using UnityEngine.UI;
    #if UNITY_EDITOR
    using UnityEditor;
    using UnityEditor.UI;
    #endif
    
    public class DwellTimeButton : Button
    {
        public float dwellTime = 0.5f;
    
        public override void OnPointerEnter(PointerEventData pointerEventData)
        {
            base.OnPointerEnter(pointerEventData);
    
            StartCoroutine (DwellTimeRoutine());
        }
    
        public override void OnPointerExit (PointerEventData pointerEventData)
        {
            base.OnPointerExit(pointerEventData);
    
            StopAllCoroutines ();
        }
    
        private IEnumerator DwellTimeRoutine ()
        {
            yield return new WaitForSeconds (dwellTime);
    
            onClick.Invoke();
        }
    
    #if UNITY_EDITOR
        [CustomEditor (typeof (DwellTimeButton)]
        private class DwellTimeButtonEditor : ButtonEditor
        {
            SerializedProperty dwellTime;
    
            protected override void OnEnable()
            {
                base.OnEnable();
    
                dwellTime = serializedObject.FindProperty("dwellTime");
            }
    
            public override void OnInspectorGUI()
            {
                base.OnInspectorGUI();
    
                serializedObject.Update();
    
                EditorGUILayout.PropertyField(dwellTime);
    
                serializedObject.ApplyModifiedProperties();
            }
        }
    #endif
    }
    

    用这个组件替换一个普通的 Unity Button 并设置所需的驻留时间。然后它会在停留时间保持专注后自动调用 onClick 事件。


    作为替代方案,无需在任何地方替换 Button,您只需将此组件附加到每个按钮上即可:

    using System.Collections;
    using UnityEngine;
    using UnityEngine.EventSystems;
    using UnityEngine.UI;
    
    [RequireComponent(typeof(Button))]
    public class DwellTimeButton : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
    {
        [SerializeField] private Button button;
    
        public float dwellTime = 0.5f;
    
        private void Awake()
        {
            if (!button) button = GetComponent<Button>();
        }
    
        public  void OnPointerEnter(PointerEventData pointerEventData)
        {
            button.OnPointerEnter(pointerEventData);
    
            StartCoroutine(DwellTimeRoutine());
        }
    
        public  void OnPointerExit(PointerEventData pointerEventData)
        {
            button.OnPointerExit(pointerEventData);
    
            StopAllCoroutines();
        }
    
        private IEnumerator DwellTimeRoutine()
        {
            yield return new WaitForSeconds(dwellTime);
    
            button.onClick.Invoke();
        }
    }
    

    这基本上是相同的,但会调用onClickButton 组件的其他事件。


    注意:在智能手机上输入,但我希望思路清晰

    【讨论】:

    • 感谢您的帮助,我最终做了这样的事情。但是对于您的解决方案,我的意思是这可能适用于按钮,但是我是否必须扩展需要此行为的每个对象?我可能会使用协程(我目前正在使用更新方法)
    • @Vahakn 当然,您也可以只实现放置在按钮旁边的组件,然后只调用GetComponent&lt;Button&gt;.onClick.Invoke();。将其添加为更新
    猜你喜欢
    • 2017-07-02
    • 1970-01-01
    • 1970-01-01
    • 2020-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-09
    相关资源
    最近更新 更多