Author Topic: UITexture click (or tap) location  (Read 7290 times)

EToreo

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 88
    • View Profile
UITexture click (or tap) location
« on: December 31, 2012, 07:56:45 PM »
I want to display a mini-map of my terrain (with cities on it) in a NGUI dialog. It's my understanding that I can use a UITexture to do this with ease.  But I need the player to select one of the cities on the mini-map.  Thus, I need to get the x,y location of the touch on the mini-map so I can transform it into a real-world coordinate and check if a city was selected.

Any ideas on how I might accomplish this?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UITexture click (or tap) location
« Reply #1 on: December 31, 2012, 10:51:51 PM »
You know the position of the click -- UICamera.lastTouchPosition. You know the position and size of your map (transform.localPosition and transform.localScale). The rest is math.

EToreo

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 88
    • View Profile
Re: UITexture click (or tap) location
« Reply #2 on: January 01, 2013, 02:21:19 AM »
UILamera.lastTouchedPosition is exactly was I was looking for, thank you.

EToreo

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 88
    • View Profile
Re: UITexture click (or tap) location
« Reply #3 on: January 05, 2013, 07:00:06 PM »
I have my UITexture set to 256x256 and am successfully using it to click on and jump to that location with the game camera.  With one exception.  When the UITexture is scaled down by NGUI (because a resolution was selected that causes the GUI to scale down) the math just won't work.  I have spend hours trying to find something in NGUI that would allow me to detect when the GUI is scaled, but I can't seem to find the right combination of things.

Any suggestions?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UITexture click (or tap) location
« Reply #4 on: January 05, 2013, 07:06:47 PM »
UIRoot.pixelSizeAdjustment

EToreo

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 88
    • View Profile
Re: UITexture click (or tap) location
« Reply #5 on: January 05, 2013, 07:37:19 PM »
Thanks!  I was able to re-work my math with this.  It's all working now for arbitrary resolutions.

One more question... I am "cheating" and using UIButtonMessage script on the texture to generate a message when the user clicks on it.  Is there a better way to do this so that I can call the same message as long as the mouse click is down (and press is held) so the user can drag around on the mini map and have my camera follow it?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UITexture click (or tap) location
« Reply #6 on: January 05, 2013, 08:02:08 PM »
You get OnDrag events while something is being dragged. UIButtonMessage can forward events somewhere, and UIEventListener can be used to listen to events from somewhere (basically the opposite of UIButtonMessage). UIEventListener is more efficient.

EToreo

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 88
    • View Profile
Re: UITexture click (or tap) location
« Reply #7 on: January 06, 2013, 04:26:30 PM »
I am trying to use UIEventListener to get the drag event and it's working functionally, but I am getting a bunch of errors in the console.

Here is one of the many errors:
Quote
Failed to call function OnPress of class MiniMapTexture
Calling function OnPress with 1 parameter but the function requires 2.

Here is how I set things up:
  1.  
  2. public class MiniMapTexture : MonoBehaviour
  3. {
  4.    // Use this for initialization
  5.    void Start ()
  6.    {
  7.       UIEventListener.Get(gameObject).onPress += OnPress;
  8.       UIEventListener.Get(gameObject).onDrag += OnDrag;
  9.    }
  10.    
  11.    // Update is called once per frame
  12.    void Update ()
  13.    {
  14.      
  15.    }
  16.    
  17.    void MiniMapPositionSelected()
  18.    {
  19.       ...
  20.    }
  21.    
  22.    void OnPress(GameObject pGameObject, bool press)
  23.    {
  24.       MiniMapPositionSelected();
  25.    }
  26.    
  27.    void OnDrag(GameObject pGameObject, Vector2 pOffset)
  28.    {
  29.       MiniMapPositionSelected();
  30.    }
  31. }
  32.  
  33.  

I am sure it's just how I set up the delegates.  What did I do wrong?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UITexture click (or tap) location
« Reply #8 on: January 06, 2013, 08:16:47 PM »
It tells you what the problem is. Look at UIEventListener, check its function delegates up top. UIEventListener has a game object, but OnPress function (which is a default NGUI callback name) does not.
« Last Edit: January 06, 2013, 08:56:37 PM by ArenMook »

EToreo

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 88
    • View Profile
Re: UITexture click (or tap) location
« Reply #9 on: January 06, 2013, 08:34:19 PM »
What am I missing?  Because I looked and found that onDrag takes 2 arguments, the first is a GameObject.

Quote
delegate void UIEventListener.VectorDelegate   (   GameObject    go,
Vector2    delta
)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UITexture click (or tap) location
« Reply #10 on: January 06, 2013, 08:57:51 PM »
Get rid of your entire Start function. You don't need it. And get rid of the GameObject parameter in OnPress and OnDrag.

UIEventListener is for subscribing to events on a remote object. You're doing local event handling.

EToreo

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 88
    • View Profile
Re: UITexture click (or tap) location
« Reply #11 on: January 07, 2013, 12:01:10 AM »
Works like a charm.  Thanks!

What would be the syntax if had I a remote object and needed to use the UIEventListener?  I might need to do that in the future and am interested.

Cripple

  • TNP Alpha
  • Full Member
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 117
    • View Profile
Re: UITexture click (or tap) location
« Reply #12 on: January 07, 2013, 05:28:16 AM »
Assuming that you have a delegate 'TheDelegate'  which takes a GameObject as parameter and a Script 'ListenToMe' which as a public event called EventToListen  of that delegate.

  1. public class MyClass: MonoBehaviour
  2. {
  3.    // The scripts which triggers the event, add it threw the inspector of using GetComponent<ListenToMe >() in the Awake function.
  4.    public ListenToMe EventCaller.
  5.  
  6.    // The delegate which will contains the link to the function 'OnEvent' we want to call when the event is triggered.
  7.    private TheDelegate _handler;
  8.  
  9.    void Start()
  10.   {
  11.       // Create the delegate with the function to call.
  12.       _handler = new TheDelegate (OnEvent);
  13.   }
  14.  
  15.    // Use this for initialization
  16.    void OnEnable()
  17.    {
  18.         // Subscribe to the remote event
  19.         EventCaller.EventToListen += _hander;
  20.    }
  21.  
  22.    void OnDisable()
  23.   {
  24.         // When you subscribe to an event with the +
  25.         // always, alwAYS, ALWAYS unsubscribe to it with the -
  26.         // otherwise your game will be full of leaks and then you die. (Oh wait you may not die but your application will be a spaghetti machine.)
  27.         EventCaller.EventToListen -= _hander;
  28.   }
  29.    
  30.    // The function called when the event is triggered.
  31.    void OnEvent(GameObject pGameObject)
  32.    {
  33.         Debug.Log("Event triggered");
  34.    }
  35. }
« Last Edit: January 07, 2013, 05:43:52 AM by Cripple »
Graphicstream Dev.