Unity makes health progress bar

Use the slider component in the UI to make a progress bar:
insert image description here
the effect of the health value progress bar is as above, you can directly mount the component slider on the game object to get the component directly in the script, or create an empty object in the object to create a new slider object and hang it on the script contained in the variable,insert image description here

In the sub-objects in the left frame, background represents the underlying content. We set it to red according to the example, that is, if the life value is empty, it will be all red; fill area sub-object fill is the content of the upper layer, which is used to display healthy life value The progress bar is set to green in the example; the handle in the handle slide area is to set the drag button in the progress bar according to the actual situation; the above is the progress bar production;

For the life value , it is easier to put the fill area sub-object fill into the background sub-object, and delete the fill area and handle slide area for the others, and press the alt key to click on the middle position of the fillinsert image description here

insert image description here

Then you can dynamically change the life bar through a line of code in the script:
slider.value = (float)HP/MAXHP;//Update the health value
slider.transform.LookAt(Camera.main.transform);//This is the life value has been Facing the main camera

Making a scene loading progress bar (asynchronous scene loading)
is also similar to making a loading progress bar. After adjusting the parameters, you can use some methods in AsyncOperation to realize code manipulation and update the scene, but the ui interface is the same as above;

Guess you like

Origin blog.csdn.net/weixin_50746193/article/details/121446211