NGUI study-3D model coordinates to 2D screen coordinates

Source: Internet
Author: User
A friend asked me today. A typical example is the blood bar of Characters in the game. The principle is to convert a 3D point into a 2D point, but since NGUI itself is 3D, we need to convert the point under NGUI into a 2D point first, and then convert it into a 3D point. It sounds a bit difficult. It doesn't matter if I directly access the code. Looking at NGUI research that is not clear about screen adaptation

A friend asked me today. A typical example is the blood bar of Characters in the game. The principle is to convert a 3D point into a 2D point, but since NGUI itself is 3D, we need to convert the point under NGUI into a 2D point first, and then convert it into a 3D point. It sounds a bit difficult. It doesn't matter if I directly access the code. Looking at NGUI research that is not clear about screen adaptation

Zookeeper

A friend asked me today. A typical example is the blood bar of Characters in the game. The principle is to convert a 3D point into a 2D point, but since NGUI itself is 3D, we need to convert the point under NGUI into a 2D point first, and then convert it into a 3D point. It sounds a bit difficult. It doesn't matter if I directly access the code. How NGUI study adapts to the screen

At present, I have always used NGUI as a blood bar for people, but there is a limit for 2D blood strips, that is, it cannot be related to the model. However, the blood bar can be adjusted according to the position of the character. For example, the blood bar of a character farther away will be smaller, and the blood bar of a person closer to the person will be larger.

It is best to let the art directly build a GameObject point when doing FBX. Because the model is high or low, the location and height of the blood strip are different, if the art is built in, you can let the art adjust the points on the top of the model, which is more convenient.


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

UsingUnityEngine;

Using System. Collections;

PublicclassNewBehaviourScript: MonoBehaviour {

// Role

PublicTransform Cube;

// Role blood bar

PublicTransform UI;

// The default distance between the blood strip and the camera

PrivatefloatFomat;

// The position on the top of the role's head. It is best for art to directly include this point in the fbx model.

PrivateTransform Head;

VoidStart ()

{

// Locate the top point of the role

Head = Cube. Find ("head ");

// Calculate the distance between the following default blood records. You can also write a constant to mark it.

Fomat = Vector3.Distance (Head. position, Camera. main. transform. position );

}

VoidUpdate ()

{

// Here we can judge that if the position is not changed, do not assign a value.

FloatnewFomat = Fomat/Vector3.Distance (Head. position, Camera. main. transform. position );

UI. position = WorldToUI (Head. position );

// Calculate the scaling ratio of the bleeding strip

UI. localScale = Vector3.one * newFomat;

// Test the code. Press w s to move the role.

If (Input. GetKey (KeyCode. W ))

Cube. Translate (Vector3.forward );

If (Input. GetKey (KeyCode. S ))

Cube. Translate (Vector3.back );

}

// The core code here converts 3D points into 2D points on the NGUI screen.

PublicstaticVector3WorldToUI (Vector3point)

{

Vector3 pt = Camera. main. WorldToScreenPoint (point );

// I found that sometimes UICamera. currentCamera sometimes gets an error in currentCamera. Please pay attention to it.

Vector3 ff = UICamera. currentCamera. ScreenToWorldPoint (pt );

// If the UI is used, the Z axis equals 0.

Ff. z = 0;

Returnff;

}

}

Generally, blood may be composed of multiple UISprite, so it is best to put these Sprite under the same GameObject, so that only the parent object needs to be corrected, so the zooming of the sub-object will be correct. Controls the movement of roles closer to each other.

If the role is controlled to move a little farther, you will find that the proportion of blood bars on the screen will not change.

I wish you a pleasant learning experience. Welcome to the discussion.

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.