The Avatar and blood strip are generated in the 3d world, so there is a real relationship between vertical depth and occlusion. If you don't talk much about it, see how I implement it.
Step 1: Create an avatar and a blood strip in the UI Root.
The procedure is basically the same as that described in the previous article about how to create a blood strip. I will cut a picture below.
It is HeroPanel above. It is very simple. Create a main panel HeroPanel and create a Slider in it to show the blood bar, and then create a sprite to show the Avatar, here I also have a sprite that is the background frame of the Avatar, and then adjust the position and the color of the blood strip. I don't know how to read the article about the blood strip in front of my avatar. It looks like this:
Step 2: Make the Avatar and blood strip into prefab.
Drag the created HeroPanel to the Resources folder of the Project (Project-> Assets-> Resources), and create one without the Resources folder for future loading. In this way, the prefab of HeroPanel is generated, and then the prefab under the UI Root can be deleted.
Step 3: Enter the program.
After the above preparation work is OK, it is the programming stage. Each of my roles has an Npc script, and the prefab of HeroPanel is loaded in the Start of the Npc script.
[Csharp]View plaincopy
- GameObject heroPanel = Resources. Load ("HeroPanel") as GameObject;
- _ HeroPanel = Instantiate (heroPanel, transform. position, transform. rotation) as GameObject;
- _ HeroPanel. transform. localScale = new Vector3 (0.006f, 0.006f, 0.006f );
- _ BloodSlider = _ heroPanel. GetComponentInChildren <UISlider> ();
- _ HeroHeight = gameObject. collider. bounds. size. y;
The above code is very simple. First, load the prefab of HeroPanel, then generate the instance _ heroPanel, and then adjust the size. This must be adjusted to the desired size. Otherwise, you can't bear to look directly at it, then obtain the UISlider script and assign it to the variable _ bloodSlider, which will be obtained later. If you want to change the Avatar image, you can click here. I will not change it. Finally, obtain the height of the role, use the collision box to obtain it, and use the coordinates calculated in the subsequent Update.
Then the Update part is:
[Csharp]View plaincopy
- Vector3 pos = new Vector3 (transform. position. x, transform. position. y + _ heroHeight + HEAD_OFFSET, transform. position. z );
- _ HeroPanel. transform. position = pos;
- _ HeroPanel. transform. rotation = Camera. main. transform. rotation;
In the Update, You need to obtain the coordinates of the role at any time. The most important thing is the y coordinate. Because you want to place the coordinates on the top of the role, the previous _ heroHeight, A HEAD_OFFSET is added later, which is the offset. I am 1.0, and then assign the coordinates to _ heroPanel. In this way, the blood bar of the Avatar will follow the role, but don't forget, it is the orientation of the camera. You need to keep this panel at all times. This is the role of the third code.
Now you have implemented the basic functions. It's easy. The rest is the slider of the blood strip. This is the same as the usage of the article I introduced earlier, here, the value of _ bloodSlider is changed, which is the percentage. I will not introduce it here. Haha, close the job.