Author Topic: Relative mouse click position inside an element  (Read 4671 times)

chaosmaker

  • Guest
Relative mouse click position inside an element
« on: October 07, 2013, 01:40:29 PM »
Hi,

I am new to NGUI and now I am trying to build a UI element which has a straightforward behaviour (which we have seen at many games) but before starting my own implementation, I wanted to get some advice for a possible already-made implementation inside NGUI.

I have a simple sprite with collider, and when you click on it, a small texture drawn on clicked area (like an indicator). Also you can get the relative position of clicked area like if you have a sprite of 100x100 then you could be able to get the clicked area inside your 100x100 sprite (Clicked = 26, 12)

I could have a sprite and get OnPress event from it. Then inside that I can get the mouse position, that is very easy so far. But what about next?

1. For drawing texture, I could draw directly on mouse screen coordinates with Unity's own texture drawing system or is there any way to set another NGUI texture's position to mouse position? Maybe UICamera.lastTouchPosition but would setting the UISprite's transform position to it would be enough?

2. Same goes for getting the relative position of mouse inside a UI sprite. You could think this as a mini-map on GUI and you could map this mini-map to real world map and clicking on it makes navigation or some other action on the map. Also of course it should work on different resolutions and full-screen mode.

Thanks!

rorakin3

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 10
    • View Profile
Re: Relative mouse click position inside an element
« Reply #1 on: October 07, 2013, 05:05:51 PM »
You want to create a object where the mouse clicks, like in the 1st tutorial video linked here?
http://forum.unity3d.com/threads/203574
You will want to store what you want to create as a prefab, and then instantiate the prefab at the mouse position. To convert from mouse position to world position:
curObj.transform.position = UICamera.currentCamera.ViewportToWorldPoint(new Vector3(Mathf.Clamp01(UICamera.currentTouch.pos.x / Screen.width), Mathf.Clamp01(UICamera.currentTouch.pos.y / Screen.height),0));

In this example, curObj is the gameObject you instantiate from your prefab.