Unity3d character name and blood bar Drawing Method, unity3d drawing
The method for drawing the name and blood bar of a person is simple, but the problem we need to solve is how to find the appropriate coordinates in the 3D world. Because the characters in the 3D world are moving, they are moving in the 3D world, not in the 2D plane, but we need to convert the 3D Character coordinates into the coordinates in the 2D plane, then, find the 2D coordinates of the person's head on the screen and use the GUI to draw the name and blood strip.
First, I will learn how to convert any 3D coordinates in the game world into 2D coordinates on the screen. The point in the lower-left corner of the Screen is 0.0, and the coordinate in the upper-right corner is 1.1. Therefore, the actual 2D coordinates must be calculated using Screen. height and Screen. width.
1
Vector2 position = camera. WorldToScreenPoint (worldPosition );
Import the role Controller component in the unity project. If you do not know the role controller, please read my previous article. Create a Plane as the ground of the game, and then use the role Controller component to create two models, one as the main character, and the other as the NPC. The main character can be controlled to move around to observe the NPC object. Because the area on the ground is relatively small, we make a physical layer of the border to avoid the leader falling out of the border. The physical layer is actually very simple, that is, four planes are placed around the plane and the plane is surrounded by four planes, and the Box Collider component is bound to the four planes around the plane, so that the main character will not fall out of the border. Because no texture is provided, the four objects cannot be seen in the effect. As shown in, in the scenario, the main character is surrounded by four planes on the way. in real time, it desperately wants to go out, but it still cannot jump out.
Create the script NPC. cs and then attach the script to the NPC object. In the script, draw the blood bar and name of the main character.
- NPC. cs
- 001
- Using UnityEngine;
- 002
- Using System. Collections;
- 003
- 004
- Public class NPC: MonoBehaviour {
- 005
- 006
- // Main camera object
- 007
- Private Camera camera;
- 008
- // NPC name
- 009
- Private string name = "I am Yu Song MOMO ";
- 010
- 011
- // Main character object
- 012
- GameObject hero;
- 013
- // NPC model height
- 014
- Float npcHeight;
- 015
- // Red blood strip texture
- 016
- Public Texture2D blood_red;
- 017
- // Black blood strip texture
- 018
- Public Texture2D blood_black;
- 019
- // The default NPC blood value
- 020
- Private int HP = 100;
- 021
- 022
- Void Start ()
- 023
- {
- 024
- // Obtain the protagonist object based on the Tag
- 025
- Hero = GameObject. FindGameObjectWithTag ("Player ");
- 026
- // Obtain the camera object
- 027
- Camera = Camera. main;
- 028
- 029
- // Annotation 1
- 030
- // Obtain the original height of the model.
- 031
- Float size_y = collider. bounds. size. y;
- 032
- // Obtain the scaling ratio of the model.
- 033
- Float scal_y = transform. localScale. y;
- 034
- // Their product is the height
- 035
- NpcHeight = (size_y * scal_y );
- 036
- 037
- }
- 038
- 039
- Void Update ()
- 040
- {
- 041
- // Keep the NPC as the main character
- 042
- Transform. LookAt (hero. transform );
- 043
- }
- 044
- 045
- Void OnGUI ()
- 046
- {
- 047
- // Obtain the coordinates of the NPC head in the 3D world
- 048
- // By default, the NPC coordinate point is under the soles of the feet, so you can add the height of the npcHeight model here.
- 049
- Vector3 worldPosition = new Vector3 (transform. position. x, transform. position. y + npcHeight, transform. position. z );
- 050
- // Convert the 3D coordinates on the head of the NPC to the coordinates on the 2D screen.
- 051
- Vector2 position = camera. WorldToScreenPoint (worldPosition );
- 052
- // Obtain the 2D coordinates of the real NPC head.
- 053
- Position = new Vector2 (position. x, Screen. height-position. y );
- 054
- // Annotation 2
- 055
- // Calculate the width and height of the bleeding strip
- 056
- Vector2 bloodSize = GUI. skin. label. CalcSize (new GUIContent (blood_red ));
- 057
- 058
- // Calculate the red blood bar display area based on the blood value
- 059
- Int blood_width = blood_red.width * HP/100;
- 060
- // Draw black blood records first
- 061
- GUI. DrawTexture (new Rect (position. x-(bloodSize. x/2), position. y-bloodSize. y, bloodSize. x, bloodSize. y), blood_black );
- 062
- // Draw a red blood bar
- 063
- GUI. DrawTexture (new Rect (position. x-(bloodSize. x/2), position. y-bloodSize. y, blood_width, bloodSize. y), blood_red );
- 064
- 065
- // Note 3
- 066
- // Calculate the width and height of the NPC name
- 067
- Vector2 nameSize = GUI. skin. label. CalcSize (new GUIContent (name ));
- 068
- // Set the display color to yellow.
- 069
- GUI. color = Color. yellow;
- 070
- // Draw the NPC name
- 071
- GUI. label (new Rect (position. x-(nameSize. x/2), position. y-nameSize. y-bloodSize. y, nameSize. x, nameSize. y), name );
- 072
- 073
- }
- 074
- 075
- // The following is the event of the classic Mouse clicking object. You should know what it means.
- 076
- Void OnMouseDrag ()
- 077
- {
- 078
- Debug. Log ("when you drag the model area ");
- 079
- }
- 080
- 081
- Void OnMouseDown ()
- 082
- {
- 083
- Debug. Log ("when you press the mouse ");
- 084
- 085
- If (HP> 0)
- 086
- {
- 087
- HP-= 5;
- 088
- }
- 089
- 090
- }
- 091
- Void OnMouseUp ()
- 092
- {
- 093
- Debug. Log ("when the mouse is lifted ");
- 094
- }
- 095
- 096
- Void OnMouseEnter ()
- 097
- {
- 098
- Debug. Log ("when the mouse enters the object area ");
- 099
- }
- 100
- Void OnMouseExit ()
- 101
- {
- 102
- Debug. Log ("when the mouse leaves the model area ");
- 103
- }
- 104
- Void OnMouseOver ()
- 105
- {
- 106
- Debug. Log ("when the mouse stays in the object area ");
- 107
- }
- 108
- 109
- }
Copy code
Note 1: You can use collider. bounds. size to obtain the axial height of the model, but the model can be scaled. Therefore, the actual height of the model should be the original height multiplied by the zoom factor. Transform. localScale can obtain three axial scaling coefficients for the model. because we need the height of the model, the X axis and the Z axis are ignored.
NOTE 2: here we calculate the width of the blood strip, GUI. skin. label. Calcsize () This method is to remove the parameter object width and height from the default skin Object Label object. The parameter "new GUIContent (blood_Red)" indicates the width and height of the red strip texture, Which is saved in the returned size. Finally, draw the blood strip in the screen at the width and height, and take two layers of blood strip. The background is black and the front is red. When the person is charged for blood, the red blood strip is reduced.
NOTE 3: here we use a string to obtain the overall width and height. Because the name of the NPC is variable, We need to dynamically obtain the overall display area. CalcSize is also called using the GUI. skin. label object. Dog planing Learning Network]
As shown in, when you click the NPC object with the mouse, the blood on the top of the NPC head begins to lose blood. In this example, you can use the OnGUI to create a GUI Texture or GUI Text object in the Hierarchy view.