Hello, I have some code basic using the IPointHandler. I use the IPointEnterHandler to reset a entered flag then IPointDownHandler to set the button entered. The problem I have is sometimes with I toggle between two buttons with the script. It seem to unequip. For example, click A, B, A, B, if I click A again the item drops. I am assuming on OnPointerDown it saves the mouse location on OnPointerUp it just check if the mouse was up outside of the area. I don't think it testing the RectTransform to the mouse position correctly. Additionally if I could check that properly, I wouldn't need OnPointerClick. Any thoughts. Vivienne Code (CSharp): // TerranPrime AR-548 // Space Survival Game // Coded by Vivienne Anthony // // Spacd Survival Game // // For Protocol Seven Productions 2017 // Copyrighted(2017) All Rights Reserved // using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; public class EquipButtonScript : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerClickHandler,IPointerDownHandler,IPointerUpHandler { private Player m_Player; public int equipslot; private bool m_Dragging; private Rect m_Rect; private Vector3 m_LastTouch; private bool m_Entered; // Use this for initialization void Start () { // Set m_player to null when starting m_Player = null; m_Dragging = false; RectTransform rectTransform = GetComponent<RectTransform> (); // get rectinformation m_Rect = RectTransformToScreenSpace (rectTransform); m_Dragging = false; } // Set equip slot public void SetEquipSlot (int i) { // Set button equip slot to assigned slot equipslot = i; } // On pointer enter public void OnPointerEnter (PointerEventData eventData) { // Reset enter flag m_Entered = false; } public void OnPointerDown (PointerEventData eventData) { // Save mouse position m_LastTouch = Input.mousePosition; // Set Enter flag m_Entered = true; } // On Pointer up public void OnPointerUp (PointerEventData eventData) { // Make sure the button was entered if (m_Entered == false) return; // Get Mouse Position Vector3 mousePosition = Input.mousePosition; // Check if the mouse position changed. if (m_LastTouch != mousePosition) { // calc mouse position mousePosition = Camera.main.ScreenToWorldPoint (new Vector3 (mousePosition.x, mousePosition.y, 0)); mousePosition.x += Screen.width / 2; mousePosition.y += Screen.height / 2; Debug.Log ("x " + mousePosition.x + " " + m_Rect.x); Debug.Log ("y " + mousePosition.y + " " + m_Rect.y); // Check coordinate here if ((mousePosition.x < m_Rect.x - (m_Rect.size.x / 2) || mousePosition.x > m_Rect.x + (m_Rect.size.x / 2)) && (mousePosition.y < m_Rect.y - (m_Rect.size.y / 2) || mousePosition.y > m_Rect.y + (m_Rect.size.y / 2))) { // UnEquip UnEquipMe (); } // Reset flag m_Entered = false; } else { EquipMe (); } } public void OnPointerClick (PointerEventData eventData) { // If click EquipMe (); // Reset entered flag m_Entered = false; } public static Rect RectTransformToScreenSpace (RectTransform transform) { Vector2 size = Vector2.Scale (transform.rect.size, transform.lossyScale); float x = transform.position.x + transform.anchoredPosition.x; float y = Screen.height - transform.position.y - transform.anchoredPosition.y; return new Rect (x, y, size.x, size.y); }
I resolved it I think. Basically a UI interface area that contains a image with a button component. I can display four slots and equip a item that updates a area. Touching a area and dragging out, would unequip and drop the game object. Touching would simply equip a game object. Originally I was using the Button component to detect a click and calling a function. Also, I then tried just use the IPointClickHandler. Which failed. Code (CSharp): // On pointer enter public void OnPointerEnter (PointerEventData eventData) { // Reset enter flag m_Entered = false; } public void OnPointerDown (PointerEventData eventData) { // Save mouse position m_LastTouch = Input.mousePosition; // Set Enter flag m_Entered = true; } // On Pointer up public void OnPointerUp (PointerEventData eventData) { // Make sure the button was entered if (m_Entered == false) return; // Get Mouse Position Vector3 mousePosition = Input.mousePosition; // Check if the mouse position changed. if (m_LastTouch != mousePosition) { // test drag out if (RectTransformUtility.RectangleContainsScreenPoint ( GetComponent<RectTransform> (), mousePosition, null ) == false) { Debug.Log ("DragOut"); UnEquipMe (); } } else { EquipMe (); } m_Entered = false; } The solution was using the RectTransform component and passing the mouseposition to it. Rough version.