Search code examples
c#unity-game-enginespawngameobjectunity-ui

Spawning GameObject in AR plane except on the Button UI area


I am developing an AR Measuring tool where I'm placing my measuring point disk on the AR Plane. I have added an Undo and Redo button for this, as there is a chance one removes the last placed point using undo button.

Now my issue is that, when I click the undo button, the point disk gets placed behind the button and the Undo buttons for that place only. Same goes for the Redo button. If there is no value for Redo to respawn, the object gets spawned behind the redo button as if I was instantiating the point disk behind the button, which I am not doing.

It will be helpful if someone can give me an idea on how to make sure that I spawn an object but not when I clicked on button. Yet the button does the work it has been assigned.

I have taken two videos as reference and merged the code in that.

For the Undo-Redo Buttons Link using Command Design Pattern

For the line render between the object placement without AR Placement Interactable

Instead of AR Interactable, I used the code of Undo-Redo.

And I have used this method too for removing the spawning code over the button but then the button doesn't work at all.

public void UndoCommands()
{
 if (Input.touchCount > 0)
   {
     Touch touch = Input.GetTouch(0);
     if (undoHistoryStack.Count > 0 && !EventSystem.current.IsPointerOverGameObject(touch.fingerId))
       {
          //Code that is going to work when this function is called
       }
    }
}

This code will be added to Undo Button, and similarly goes for the Redo.

I have been trying to crack this situation for so long. If any other details are required, I will try my best to share. But, please help me out here.


Solution

  • I don't know how many people have seen the above mentioned video and still looking for the answer, but I have got the solution that worked for me very well. If you have seen the Undo-Redo video, you will see there is a file name ClickManager.cs where the data is passed to instantiate object and for the Undo-Redo function. In that file, add this two line:

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            if (EventSystem.current.IsPointerOverGameObject())
                return;
    
            Ray ray = arCamera.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit, Mathf.Infinity))
            {
                commandingManager.ExecuteCommands(new Instantiat(spawnObject, hit.point, txt, lineRenderer, measurementValueParent, measurementPointParent));
            }
        }
    }
    

    The above mentioned line is added which made sure that if I click on button, no object is spawned and the onClick() function of the object worked.