"Komatsu teaches you to develop" "Unity practical Skills" the tips interface generated from the previous gameobject coordinates

Source: Internet
Author: User

One of the requirements for developing games, especially for MMO hands, is to tap an outfit, create a tips interface near it, and describe the capabilities and information of the device.

Like this in the red box above.

The main problem is to generate this detail interface according to the gameobject of the click, the detail interface position needs to be placed reasonably (cannot display, cannot obscure etc.)

The basic idea is that

First find Gameobject's position,

The phone screen is roughly divided into four quadrants, know which quadrant of the gameobject is probably on this screen (upper left, lower left, upper right, bottom right)

Depending on the quadrant, the detail interface should be generated on which side of the Gameobject

(for example, if Gameobject is in the lower right, the detail interface should be generated at the top left of Gameobject)

According to the above position plus the width of the gameobject half, plus the width of the half of their interface, plus a few pixels to offset,

This allows the location of the detail interface to be placed.

However, there is a more important problem here, usually, gameobject and their own detail interface is not generally under the same camera.

So the first thing to do here is to get the coordinates on both sides.

Here's a quick look.

There are basically 4 coordinates in unity.

World coordinates, relative coordinates, screen coordinates, relative screen 0, 0 point coordinates

Since we're going to switch coordinates before the camera, for the sake of simplification, we're not going to calculate the world coordinates and relative coordinates and convert through screen coordinates.

That means that

Find Gameobject's world coordinates, calculate its own camera's screen coordinates through the interface, and pass the screen coordinates to the detail interface.

The detail interface transforms the gameobject screen coordinates through the conversion interface of your camera into world coordinates, placing position.

This allows you to calculate the conversion of the same point coordinate on the screen in the case of different camera.

Put the code below.

[CSharp]View PlainCopy 
  1. Public void Resetposition (gameobject relativeobject,camera Cam)
  2. {
  3. //Get the position that triggers this dialog gameobject relative to the screen
  4. Vector3 relativeposition = Cam. Worldtoscreenpoint (relativeObject.transform.position); //through the world coordinates to obtain
  5. Vector3 relativepositionpercent = Cam. Worldtoviewportpoint (relativeObject.transform.position); //Gets the position of the gameobject relative to the screen 0, 0 points (x, y range between 0, 1)
  6. Vector3 newposition = CDialogManager.Instance.GetCamera (euiindex.index_orthographicthree). Screentoworldpoint (relativeposition); //back to world coordinates
  7. M_mainGameObject.transform.position = newposition; //Position first, then gross position offset value plus
  8. Bounds realativeobjectbounds = Nguimath.calculaterelativewidgetbounds (relativeobject.transform); //Calculate the Gameobject border size
  9. float realativeobjectwidth = realativeobjectbounds.extents.x;
  10. float realativeobjectheight = realativeobjectbounds.extents.y;
  11. Bounds objectbounds = Nguimath.calculaterelativewidgetbounds (m_maingameobject.transform); //Calculate your own border size
  12. float objectwidth = objectbounds.extents.x;
  13. float objectheight = objectbounds.extents.y;
  14. float spacingx = 5; //5 pixel offset
  15. float spacingy = 5;
  16. float OffsetX = 0; //Total offset
  17. float OffsetY = 0;
  18. if (relativepositionpercent.x >= 0.5 && relativepositionpercent.y >= 0.7)//For better results, the y-axis is divided into the upper and lower three quadrants /c4>
  19. {
  20. OffsetX =-REALATIVEOBJECTWIDTH-OBJECTWIDTH-SPACINGX;
  21. OffsetY =-realativeobjectheight-objectheight-spacingy;
  22. }
  23. Else if (relativepositionpercent.x >= 0.5 && relativepositionpercent.y <= 0.3)
  24. {
  25. OffsetX =-REALATIVEOBJECTWIDTH-OBJECTWIDTH-SPACINGX;
  26. OffsetY = realativeobjectheight +objectheight + spacingy;
  27. }
  28. Else if (relativepositionpercent.x >= 0.5 && relativepositionpercent.y > 0.3 && Relativepositionpercent.y < 0.7)
  29. {
  30. OffsetX =-REALATIVEOBJECTWIDTH-OBJECTWIDTH-SPACINGX;
  31. OffsetY = 0;
  32. }
  33. Else if (relativepositionpercent.x <= 0.5 && relativepositionpercent.y >= 0.7)
  34. {
  35. OffsetX = realativeobjectwidth + objectwidth + spacingx;
  36. OffsetY =-realativeobjectheight-objectheight-spacingy;
  37. }
  38. Else if (relativepositionpercent.x <= 0.5 && relativepositionpercent.y <= 0.3)
  39. {
  40. OffsetX = realativeobjectwidth + objectwidth + spacingx;
  41. OffsetY = realativeobjectheight + objectheight + spacingy;
  42. }
  43. Else if (relativepositionpercent.x <= 0.5 && relativepositionpercent.y > 0.3 && Relativepositionpercent.y < 0.7)
  44. {
  45. OffsetX = realativeobjectwidth + objectwidth + spacingx;
  46. OffsetY = 0;
  47. }
  48. Span style="White-space:pre" > </span>//Last offset by relative coordinates
  49. M_mainGameObject.transform.localPosition = New Vector3 (m_maingameobject.transform.localposition.x + OffsetX, m_  MAINGAMEOBJECT.TRANSFORM.LOCALPOSITION.Y + OffsetY, 0);
  50. }

"Komatsu teaches you to develop" "Unity practical Skills" the tips interface generated from the previous gameobject coordinates

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.