VR 콘텐츠 기초

InMindVR - OnGvrPointerHover, OnPointerEnter, OnPointerExit Hover[보는 상태일 때 Infomation 창 띄우기], Enter[들어갔을 때 btnYes or No를 봤으면 Popup창을 띄우거나 안띄우기], Exit[Popup창 삭제하기]

다모아 2023. 10. 18. 14:56

Player의 Body Mesh Renderer 끄기 - 360도 돌 때 내 몸이 보임

 

카페에 있는 GoogleVRForUnity Import하기

 

메인카메라에 부착


대리자 사용 X

 

일단 구현을 해봤고, 잘되었다.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class Car : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IGvrPointerHoverHandler
{
    [SerializeField] private PlayerAI playerAi;
    [SerializeField] private GameObject infomation;
    [SerializeField] private GameObject popUp;
    [SerializeField] private Infomation info;

    private float elapsedTime = 0f;

    public void OnLookAt(bool isLookAt)
    {
        //Debug.LogFormat("isLookAt: {0}", isLookAt);
    }

    public void OnGvrPointerHover(PointerEventData eventData)
    {
        Debug.Log("Hover");
        this.playerAi.MovePause();
        if (eventData.pointerEnter.gameObject.CompareTag("btnYes"))
        {
            this.CheckTime();
            //PopUp창 띄우기
            if (this.elapsedTime >= 3f)
            {
                this.popUp.SetActive(true);
                this.infomation.SetActive(false);
                this.elapsedTime = 0f;
            }
        }
        else if (eventData.pointerEnter.gameObject.CompareTag("btnNo"))
        {
            this.CheckTime();
            //PopUp창 끄기
            if (this.elapsedTime >= 3f)
            {
                this.popUp.SetActive(false);
                this.infomation.SetActive(false);
                this.elapsedTime = 0f;
            }
        }
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        Debug.Log("Enter");
        this.infomation.SetActive(true);
        
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        Debug.Log("Exit");
        this.playerAi.MovePlay();
        this.popUp.SetActive(false);
    }

    private void CheckTime()
    {
        this.elapsedTime += Time.deltaTime;
        Debug.Log(elapsedTime);
    }
}

대리자 사용

 

너무 지저분해서 Car.cs에 enum을 추가해 대리자로 Main에다가 쓰려고 만들어주었다.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class Car : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IGvrPointerHoverHandler
{
    public enum eInteractionType
    {
        Hover, Enter, Exit
    }

    public Action<eInteractionType, GameObject> onInteractionType;

    public void OnGvrPointerHover(PointerEventData eventData)
    {
        this.onInteractionType(eInteractionType.Hover, eventData.pointerEnter);
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        this.onInteractionType(eInteractionType.Enter, eventData.pointerEnter);
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        this.onInteractionType(eInteractionType.Exit, eventData.pointerEnter);
    }
}

 

Main에서 내가 메뉴, 정보, 플레이어, 차를 알아서 메인에서 처리하게 만들었다.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestMain : MonoBehaviour
{
    [SerializeField] private Car car;
    [SerializeField] private PlayerAI playerAi;
    [SerializeField] private UIMenu menu;
    [SerializeField] private UIInfomation info;

    private float elapsedTime = 0f;
    // Start is called before the first frame update
    void Start()
    {
        this.car.onInteractionType = (interactionType, go) => {
            if(interactionType == Car.eInteractionType.Hover)
            {
                if(go.gameObject.tag == "btnYes")
                {
                    this.CheckTime();
                    if(elapsedTime >= 3f)
                    {
                        this.menu.Hide();
                        this.info.Show();
                        this.elapsedTime = 0f;
                    }
                }
                else if(go.gameObject.tag == "btnNo")
                {
                    this.CheckTime();
                    if (elapsedTime >= 3f)
                    {
                        this.menu.Hide();
                        this.elapsedTime = 0f;
                    }
                }

                //info창 띄우기, Player 멈추기
                this.playerAi.MovePause();
            }
            else if(interactionType == Car.eInteractionType.Enter)
            {
                this.menu.Show();
            }
            else
            {
                //Exit
                this.playerAi.MovePlay();
                this.info.Hide();
            }
        };
    }

    private void CheckTime()
    {
        Debug.Log(elapsedTime);
        this.elapsedTime += Time.deltaTime;
    }
}

 

메뉴와 정보 스크립트에서 Show, Hide로 게임오브젝트를 키거나 끄는 메서드를 만들었다.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UIMenu : MonoBehaviour
{
    public void Show()
    {
        this.gameObject.SetActive(true);
    }
    
    public void Hide()
    {
        this.gameObject.SetActive(false);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UIInfomation : MonoBehaviour
{
    public void Show()
    {
        this.gameObject.SetActive(true);
    }

    public void Hide()
    {
        this.gameObject.SetActive(false);
    }
}

결과