Author Topic: Getting Cursor Position to place a Sprite  (Read 2415 times)

Chaosgod_Espér

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 28
    • View Profile
Getting Cursor Position to place a Sprite
« on: December 02, 2015, 06:53:13 AM »
hi there..

Soooo..
Now that i managed to create a Scrollview, 2 Scrollbars and a Texture inside the View, i want to add a "Cursor" to the System.

Therefor i created a BoxCollider, that is covering the ScrollView Size exactly ( Size.x = 256, Size.y = 480).
So now, the Scrollview visible Field should be splitted into 32*32 pixel areas (i want to create a tilemapper). The CursorSprite has a Size of 32*32 pixels.
Means, Inside the ScrollView is space for 8*15 Tiles beeing visible.
A Tileset can be as large as wanted (power of 2) - so the ScrollView can be scrolled..

My Question now:
When i move my Mouse over the ScrollView, the CursorSprite should be placed over the current hovered tile on the tileset. I know there is UICamera.lastHit.point..  The BoxCollider Size is written above, but the Resulting Position of the lastHit.point is:
X between -1.8 on the left and -1.1 on the right
Y between 1.2 on Top and -0.3 on Bottom
Z stays 0..

How can i get the Pixel Position on the Collider?

here´s a current Screenshot:
http://fs5.directupload.net/images/151202/85ohvul6.png


The result should be:
if Mouse is covering Position "49,12,0", it should be placed at "(int)((49/32)*32)-16, (int)(((TileSetHeight-12)/32)*32)+16, 0" to cover the current Mouseover tile.

Chaosgod_Espér

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 28
    • View Profile
Re: Getting Cursor Position to place a Sprite
« Reply #1 on: December 03, 2015, 12:48:38 PM »
Found a solution:

  1.         MouseOnScreen = Input.mousePosition;
  2.         TheCamera = NGUITools.FindCameraForLayer(gameObject.layer);
  3.         worldPos = TheCamera.ScreenToWorldPoint(MouseOnScreen);
  4.         localPos = transform.worldToLocalMatrix.MultiplyPoint3x4(worldPos);
  5.  
  6.         Debug.Log(localPos.ToString());

returns the exact position