Search Unity

Mouse IPoint trigger

Discussion in 'Scripting' started by cgprojects, Sep 17, 2017.

  1. cgprojects

    cgprojects

    Joined:
    Jul 19, 2016
    Posts:
    46
    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):
    1. // TerranPrime AR-548
    2. // Space Survival Game
    3. // Coded by Vivienne Anthony
    4. //
    5. // Spacd Survival Game
    6. //
    7. // For Protocol Seven Productions 2017
    8. // Copyrighted(2017) All Rights Reserved
    9. //
    10.  
    11. using System.Collections;
    12. using System.Collections.Generic;
    13. using UnityEngine;
    14. using UnityEngine.EventSystems;
    15.  
    16. public class EquipButtonScript : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerClickHandler,IPointerDownHandler,IPointerUpHandler
    17. {
    18.     private Player m_Player;
    19.     public int equipslot;
    20.  
    21.     private bool m_Dragging;
    22.  
    23.     private Rect m_Rect;
    24.     private Vector3 m_LastTouch;
    25.     private bool m_Entered;
    26.  
    27.     // Use this for initialization
    28.     void Start ()
    29.     {
    30.         // Set m_player to null when starting
    31.         m_Player = null;
    32.  
    33.         m_Dragging = false;
    34.  
    35.         RectTransform rectTransform = GetComponent<RectTransform> ();
    36.  
    37.         // get rectinformation
    38.         m_Rect = RectTransformToScreenSpace (rectTransform);          
    39.  
    40.         m_Dragging = false;
    41.     }
    42.  
    43.  
    44.     // Set equip slot
    45.     public void SetEquipSlot (int i)
    46.     {
    47.         // Set button equip slot to assigned slot
    48.         equipslot = i;
    49.     }
    50.  
    51.     // On pointer enter
    52.     public void OnPointerEnter (PointerEventData eventData)
    53.     {      
    54.         // Reset enter flag
    55.         m_Entered = false;
    56.     }
    57.  
    58.     public void OnPointerDown (PointerEventData eventData)
    59.     {  
    60.         // Save mouse position  
    61.         m_LastTouch = Input.mousePosition;
    62.  
    63.         // Set Enter flag
    64.         m_Entered = true;
    65.     }
    66.  
    67. // On Pointer up
    68.     public void OnPointerUp (PointerEventData eventData)
    69.     {        
    70.         // Make sure the button was entered
    71.         if (m_Entered == false)
    72.             return;
    73.            
    74.         // Get Mouse Position
    75.         Vector3 mousePosition = Input.mousePosition;
    76.  
    77.         // Check if the mouse position changed.
    78.         if (m_LastTouch != mousePosition) {
    79.  
    80.             // calc mouse position
    81.             mousePosition = Camera.main.ScreenToWorldPoint (new Vector3 (mousePosition.x, mousePosition.y, 0));
    82.  
    83.             mousePosition.x += Screen.width / 2;
    84.             mousePosition.y += Screen.height / 2;
    85.  
    86.             Debug.Log ("x " + mousePosition.x + " " + m_Rect.x);
    87.             Debug.Log ("y " + mousePosition.y + " " + m_Rect.y);
    88.  
    89.             // Check coordinate here
    90.             if ((mousePosition.x < m_Rect.x - (m_Rect.size.x / 2) || mousePosition.x > m_Rect.x + (m_Rect.size.x / 2)) &&
    91.                 (mousePosition.y < m_Rect.y - (m_Rect.size.y / 2) || mousePosition.y > m_Rect.y + (m_Rect.size.y / 2))) {
    92.            
    93.                 // UnEquip
    94.                 UnEquipMe ();
    95.             }
    96.  
    97.             // Reset flag
    98.             m_Entered = false;
    99.         } else {
    100.             EquipMe ();
    101.         }
    102.        
    103.     }
    104.  
    105.     public void OnPointerClick (PointerEventData eventData)
    106.     {      
    107.         // If click
    108.         EquipMe ();
    109.  
    110.         // Reset entered flag
    111.         m_Entered = false;
    112.     }
    113.  
    114.     public static Rect RectTransformToScreenSpace (RectTransform transform)
    115.     {
    116.         Vector2 size = Vector2.Scale (transform.rect.size, transform.lossyScale);
    117.         float x = transform.position.x + transform.anchoredPosition.x;
    118.         float y = Screen.height - transform.position.y - transform.anchoredPosition.y;
    119.  
    120.         return new Rect (x, y, size.x, size.y);
    121.     }
    122.  
     
    Last edited: Sep 17, 2017
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Could you explain what you're trying to do ? Just a few "steps" about what you wish was happening..
     
  3. cgprojects

    cgprojects

    Joined:
    Jul 19, 2016
    Posts:
    46
    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):
    1. // On pointer enter
    2.     public void OnPointerEnter (PointerEventData eventData)
    3.     {      
    4.         // Reset enter flag
    5.         m_Entered = false;
    6.     }
    7.  
    8.     public void OnPointerDown (PointerEventData eventData)
    9.     {  
    10.         // Save mouse position  
    11.         m_LastTouch = Input.mousePosition;
    12.  
    13.         // Set Enter flag
    14.         m_Entered = true;
    15.     }
    16.  
    17.     // On Pointer up
    18.     public void OnPointerUp (PointerEventData eventData)
    19.     {      
    20.         // Make sure the button was entered
    21.         if (m_Entered == false)
    22.             return;
    23.          
    24.         // Get Mouse Position
    25.         Vector3 mousePosition = Input.mousePosition;
    26.  
    27.         // Check if the mouse position changed.
    28.         if (m_LastTouch != mousePosition) {
    29.  
    30.             // test drag out
    31.             if (RectTransformUtility.RectangleContainsScreenPoint (
    32.                     GetComponent<RectTransform> (), mousePosition, null
    33.                 ) == false) {
    34.                 Debug.Log ("DragOut");  
    35.                 UnEquipMe ();
    36.             }
    37.         } else {
    38.             EquipMe ();
    39.         }
    40.  
    41.         m_Entered = false;
    42.      
    43.     }
    44.  
    The solution was using the RectTransform component and passing the mouseposition to it. Rough version.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Okay, cool.. if it's resolved, I"m glad :)
     
  5. cgprojects

    cgprojects

    Joined:
    Jul 19, 2016
    Posts:
    46

    The start of this video shows what I mean.
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712