Search Unity

[SOLVED] Help with GameObject Cursor

Discussion in 'UGUI & TextMesh Pro' started by Ricardo_Reis, Dec 23, 2017.

  1. Ricardo_Reis

    Ricardo_Reis

    Joined:
    Oct 15, 2017
    Posts:
    13
    Hey again,
    As I mentioned in a previous post, I'm making a interactable computer screen system, using world space canvas. As the image shows, using RayCasts I've managed to make the computer cursor move along with the camera (if at a certain distance from the screen). However I've been trying to make it actually interact with UI elements, such as the button bellow, however it does not respond because the computer cursor (Game Object) is not the cursor (real cursor [or pointer as it's called in EventsSystem]).
    I know that it is possible to script the cursor (Game Object) to trigger the cursor(pointer) events, however I was wondering if there was an alternative, namely if there is a way to make a Game Object act as the cursor (pointer), or if to make the player's mouse active (even though invisible) while looking at the monitor.


    Capture.PNG

    Capture1.PNG

    Any suggestion and/or feedback is highly welcome.

    Thank you all in advance!
     
    Last edited: Dec 26, 2017
  2. Ricardo_Reis

    Ricardo_Reis

    Joined:
    Oct 15, 2017
    Posts:
    13
    I've made this work through a scrip for the mouse itself (I'm still working on this code, since sliders aren't working properly but it should help anyone with the same problem as me):

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.EventSystems;
    5.  
    6. public class Mouse : MonoBehaviour
    7. {
    8.     private Collider _col;
    9.     private bool _isDown;
    10.     //private PointerEventData pointerData = new PointerEventData(EventSystem.current);
    11.  
    12.     void OnTriggerEnter(Collider col)
    13.     {
    14.         Debug.Log("Colisão)");
    15.         PointerEventData pointerData = new PointerEventData(EventSystem.current);
    16.  
    17.         pointerData.position = this.transform.position;
    18.  
    19.         _col = col;
    20.         ExecuteEvents.Execute(col.gameObject, pointerData, ExecuteEvents.pointerEnterHandler);
    21.  
    22.     }
    23.     void OnTriggerExit(Collider col)
    24.     {
    25.         Debug.Log("Fora da Colisão)");
    26.         PointerEventData pointerData = new PointerEventData(EventSystem.current);
    27.  
    28.         pointerData.position = this.transform.position;
    29.  
    30.         _col = null;
    31.         ExecuteEvents.Execute(col.gameObject, pointerData, ExecuteEvents.pointerExitHandler);
    32.  
    33.     }
    34.     public void Use1()
    35.     {
    36.         Debug.Log("Usar objectoUI)");
    37.         PointerEventData pointerData = new PointerEventData(EventSystem.current);
    38.  
    39.         pointerData.position = this.transform.position;
    40.         //TODO POE UM TRY EM TODOS OS EXECUTES DE USE PARA SE NAO HOUVER INTERAGIVEL ELE N FAZER ND
    41.         if (_col != null)
    42.         {
    43.             ExecuteEvents.Execute(_col.gameObject, pointerData, ExecuteEvents.pointerDownHandler);
    44.             ExecuteEvents.Execute(_col.gameObject, pointerData, ExecuteEvents.pointerClickHandler);
    45.             _isDown = true;
    46.         }
    47.     }
    48.  
    49.     public void Use1Up()
    50.     {
    51.         if (_col != null)
    52.         {
    53.             PointerEventData pointerData = new PointerEventData(EventSystem.current);
    54.             pointerData.position = this.transform.position;
    55.             ExecuteEvents.Execute(_col.gameObject, pointerData, ExecuteEvents.pointerUpHandler);
    56.             _isDown = false;
    57.         }
    58.     }
    59. }
    The Use methods are activated by a monitor script, which in itself is activated by the player script, I won't put the Player script here as it's too long, but here's the monitor script in case anyone needs it:


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.EventSystems;
    6.  
    7. public class Monitor : MonoBehaviour
    8. {
    9.     [SerializeField] private GameObject _mouse;
    10.     private GameObject _mouseInstance;
    11.  
    12.     public void ChangeMousePosition(Vector3 newPos)
    13.     {
    14.         _mouseInstance.transform.position = newPos;
    15.  
    16.     }
    17.  
    18.     public void Use1()
    19.     {
    20.         _mouseInstance.GetComponent<Mouse>().Use1();
    21.     }
    22.     public void Use1Up()
    23.     {
    24.         _mouseInstance.GetComponent<Mouse>().Use1Up();
    25.     }
    26.     public void Use2()
    27.     {
    28.         Debug.Log("toca mase na tua tia");
    29.     }
    30.  
    31.     void OnEnable()
    32.     {
    33.         Vector3 pos = this.transform.position;
    34.         Quaternion rot = new Quaternion(0, 0, 0, 0);
    35.         _mouseInstance = Instantiate(_mouse, pos, rot, this.gameObject.transform) as GameObject;
    36.     }
    37.     void OnDisable()
    38.     {
    39.         Destroy(_mouseInstance);
    40.     }
    41. }
    If anyone needs help with this subject I'd be glad to help in any way I can, or if someone has some feedback or opinion on the code be sure to let me know :)
     
  3. Bekatam

    Bekatam

    Joined:
    Jan 3, 2021
    Posts:
    3
    This may not help at all, but maybe it is because your ui button is not placed above the canvas it resides in. Try changing the PosZ value of the button(in its Rect Transform component) to a negative number and add your custom cursor as an image on your canvas with a PosZ(also Rect Transform) value of 0. Sometimes your custom cursor can't register the button because the canvas is above it. Hope I helped.