Home > Back-end >  Unity - How to Interact With Objects Between UI and World Space
Unity - How to Interact With Objects Between UI and World Space

Time:01-19

enter image description hereI am new to unity and want to make a card - board game. I want to be able to place a card on a particular tile on the board. However, my draggable script that is attached to my card seems to not interact with the world space tile (it will interact with other UI). The world space tile has a droppable script, that currently doesn't have any real functionality, but should at least output a message to the console when a user hover overs it with their cursor. It doesn't even do that. (Again, droppable works with other UI elements) I thought that maybe because when the user is selecting their cards from their hand, and the canvas is in render mode - screen space camera - that had an effect, so I wrote a script that changes the render mode of the canvas to world space and it still seems to not interact. I will attach as much as I can for a clearer explanation, but at this point all I wish is for the board or tile to be able to recognize the user is hovering over it with the cursor.

Any help or information is appreciated, I am trying to absorb as much knowledge as I can, so even if you don't have a solution, advice is useful!

Dropzone.cs

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

public class Dropzone : MonoBehaviour, IDropHandler, IPointerEnterHandler, IPointerExitHandler
{
    public void OnPointerEnter(PointerEventData eventData)
    {
        Debug.Log("OPEnt");
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        Debug.Log("OPExt");
    }

    public void OnDrop(PointerEventData eventData)
    {
        Debug.Log("on drop "   gameObject.name);
        
    }
}

Draggable.cs

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

public class Draggable : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
    Transform originalParent = null;
    private RectTransform RectTransform;

    private void Awake()
    {
        RectTransform = GetComponent<RectTransform>();
    }

    public void OnBeginDrag(PointerEventData eventData)
    {
        originalParent = this.transform.parent;
        this.transform.SetParent(this.transform.root);
        GetComponent<CanvasGroup>().blocksRaycasts = false;
        this.transform.parent.gameObject.GetComponent<CameraRender>().ChangeState();
    }

    public void OnDrag(PointerEventData eventData)
    {
        RectTransform.anchoredPosition  = eventData.delta;
    }

    public void OnEndDrag(PointerEventData eventData)
    {   
        this.transform.parent.gameObject.GetComponent<CameraRender>().ChangeState();
        this.transform.SetParent(originalParent);
        GetComponent<CanvasGroup>().blocksRaycasts = true;
    }
}

CameraRender.cs

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

public class CameraRender : MonoBehaviour
{
    enum RenderModeStates { camera, overlay, world };
    RenderModeStates m_RenderModeStates;

    Canvas m_Canvas;

    // Use this for initialization
    public void Start()
    {
        m_Canvas = GetComponent<Canvas>();
    }

    // Update is called once per frame
    public void Update()
    {
        //Press the space key to switch between render mode states
        if (Input.GetKeyDown(KeyCode.Space))
        {
            ChangeState();
        }
    }

    public void ChangeState()
    {
        switch (m_RenderModeStates)
        {
            case RenderModeStates.camera:
                m_Canvas.renderMode = RenderMode.WorldSpace;
                m_RenderModeStates = RenderModeStates.world;
                break;

            case RenderModeStates.world:
                m_Canvas.renderMode = RenderMode.ScreenSpaceCamera;
                m_RenderModeStates = RenderModeStates.camera;
                break;
        }
    }
}

I have tried to add a script that transitions render modes from Screen Space to World Space, I have attached box colliders to the world space objects.

CodePudding user response:

For non-UI 3D objects in order to receive the IPointerXYHandler etc messages you need

  • Collider on the according object(s)
  • a PhysicsRaycaster component attached to your Camera (and make sure the according Layers are selected as "Event Mask" in its Inspector)
  • Related