Search code examples
unity-game-enginemrtk

I want instantiate a cube only when other event ocurrs


I have this events, I want create an event to instantiate a new cube like on function OnPointerEnter, is could create event in unity that point to OnPointerEnter. How? I am trying with event trigger function, but i don't want "on Pointer Click event" I want "double click event" if it possible or similar event trigger in mixed reality toolkit... as a air tap event to create a new cube.

enter image description here

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Microsoft.MixedReality.Toolkit.Input;

public class Touchablecube : MonoBehaviour, IMixedRealityPointerHandler
{

    Color colorBlue = Color.blue;
    Renderer rend;
    GameObject cube;

    public void OnPointerClicked(MixedRealityPointerEventData eventData)
    {
        rend.material.color = colorBlue;
        //Vector3 v = new Vector3(
        //                eventData.Pointer.Position.x,
        //                eventData.Pointer.Position.y,
        //                eventData.Pointer.Position.z);

    }


    public void OnPointerDown(MixedRealityPointerEventData eventData)
    {
        rend.material.color = Color.red;

    }

    public void OnPointerDragged(MixedRealityPointerEventData eventData)
    {
        rend.material.color = Color.yellow;
        Vector3 v = new Vector3(
                           eventData.Pointer.Position.x,
                           eventData.Pointer.Position.y,
                           eventData.Pointer.Position.z);
        rend.transform.position = v;

    }

    public void OnPointerUp(MixedRealityPointerEventData eventData)
    {
        rend.material.color = Color.green;


    }

    public void OnPointerEnter()
    {
        cube = (GameObject)Resources.Load("Cube", typeof(GameObject));
        var obsIns = Instantiate(cube, transform.position, transform.rotation);
        obsIns.SetActive(true);
    }

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        rend = GetComponent<Renderer>();
    }

}

Solution

  • In MRTK, the OnPointerClicked event is triggered when a controller is clicked. For example, a click for HoloLens 1 and 2 is the air tap gesture and a click in VR is pressing the trigger button on the controller. A click can also be simulated in editor during play mode using the input simulation. To use the input simulation, hold the space bar to show the input simulation hand and then click mouse to simulate a click for the hand during play mode. Information about the Input Simulation

    By default, the pointer events are triggered if a game obejct is in focus. If you want to use the pointer events globally, the IMixedRealityPointerHandler needs to be registered globally like this:

        private void OnEnable()
        {
            CoreServices.InputSystem?.RegisterHandler<IMixedRealityPointerHandler>(this);
        }
        private void OnDisable()
        {
            CoreServices.InputSystem?.UnregisterHandler<IMixedRealityPointerHandler>(this);
        }
    

    You can create a cube on click by moving your OnPointerEnter() function into OnPointerClicked. A cube is created at the location of the click in the code snippet below:

        private void OnEnable()
        {
            CoreServices.InputSystem?.RegisterHandler<IMixedRealityPointerHandler>(this);
        }
        private void OnDisable()
        {
            CoreServices.InputSystem?.UnregisterHandler<IMixedRealityPointerHandler>(this);
        }
    
        public void OnPointerClicked(MixedRealityPointerEventData eventData)
        {
            Debug.Log("OnPointerClicked");
            OnPointerEnter(eventData.Pointer.Position);
        }
    
        public void OnPointerEnter(Vector3 cubePosition)
        {
            GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
            cube.transform.localScale = Vector3.one * 0.1f;
            cube.transform.position = cubePosition;   
        }
    

    Gif of code snippet Hope this helps!