[Unity3D] Unity3D re-describes the blood bar and skill cool-down effect of NGUI, unity3dngui
Certificate -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
If you like my blog, please remember my name: Qin Yuanpei. My blog address is blog.csdn.net/qinyuanpei.
Reprinted please indicate the source, Author: Qin Yuanpei, the source of this article: http://blog.csdn.net/qinyuanpei/article/details/40744781
Certificate ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Dear friends, I'm Qin Yuanpei. Welcome to follow my blog. My blog address is blog.csdn.net/qinyuanpei. After finishing all the courses in February, it means that Bo is mainly looking for a job. Because the blogger is a non-computer professional, he must complete his professional internship first, as a result, bloggers are busy searching for internships during this period. In the process of searching for practice units, enterprises are more or less involved in the transformation from traditional industries to the Internet industry, bloggers deeply touched on the Internet industry and deeply influenced all walks of life while thinking about the nature of the industry. The Internet industry is a service industry with technical, media, and social attributes. It connects people, people, information, and information by providing a variety of services for people. The Internet industry is more and more like an industry. On the contrary, it is more and more like a society. A successful internet business model will eventually connect to the real world at a certain stage, therefore, the Internet industry is not winning by technology, but by finding a better way to link the virtual world with the real world. Maybe because of the Fixed Thinking Model Formed by long-term research on technology, bloggers have always thought that technology is the core of the Internet industry. So when I was reading Li Yanhong's book "Silicon Valley business war, li Yanhong's "technology itself is not the only decisive strategy, but the commercial strategy is the key to winning a thousand miles, at this moment, the bloggers are deeply aware of this. Faith, feelings, and technology can certainly help you do a good job, but if you want to make one thing, you need to consider a variety of factors. Because the Internet industry is faced with end users, as long as they are people, they will encounter problems in traditional industries, even if you prefer to stay at home, even if you do not like to deal with people, you should always face the problems you should face. Well, let's talk about this first. Today we use NGUI to achieve bloodline and skill cooling. First, let's take a look at the results to be achieved today:
In this demonstration, we can find that the specific content we want to achieve today is the implementation of the GUI blood bar on the game interface, the blood bar on the head of the monster, and the skill cooling effect, here we will divide it into two parts: Blood strip and skill cool-down. I hope it will be helpful for you to learn NGUI. If there are any shortcomings, I hope you can understand.
I. Blood strip
In the previous article, the blogger once shared with you the content about using NGUI in Unity3D, so today we will continue to learn how to use NGUI to implement blood strip in Unity3D! It is very easy to implement a blood bar in Unity3D. In the previous article Unity3D Game Development's 2D Texture implementation blood bar component, the blogger introduced and implemented a blood bar component based on 2D textures, the principle of this method is to display two different foreground pasters and background pasters in front and back, and change the width of the foreground pasters to achieve the effect of blood pasters. In NGUI, Slider is such a control, so we can use it to achieve the effect of blood strips. First, create a Slider control in the scenario,
In this way, we can use NGUI to make a beautiful blood strip effect. Next, we will repeat this job three times, and finally we can design this effect:
The UI is composed of a background image (Sprite) and three Slider, so that we can build the interface in the game. The Slider control has a most important property value called SliderValue, which is a value between 0 and 1, indicating the length and percentage of the slide in the Slider, here we use this attribute value to display the blood volume in the blood bar. For example, if we set the player's total blood volume to 100, the percentage of the player's blood volume is the ratio of the player's current blood volume to the total blood volume, you only need to assign this value to the SliderValue of the Slide control. For example, we can use the following code to indicate that a player is injured:
this.HPValue=this.HPValue-10;HPTrans.GetComponent<UISlider>().sliderValue=(float)this.HPValue/100;
Here, as the name implies, HPValue is the current blood volume of the monster, and HPTrans is the Transform of the Slider. In actual design, the total volume of monsters varies depending on the level of monsters, so we 'd better save the total volume to an archive file or database, in this way, we can change the blood bar according to the player's blood volume. You can note that in the previous demonstration, the amount of blood will decrease when players are attacked by monsters. Now that we have reduced the amount of blood, let's take a look at the design of automatic blood return and automatic blue recovery in some games. For example, in the game "Gu jianqitan 2", because the combat mode is changed to the real-time mode, the player's "gas" in the game no longer relies on attacking the enemy in the battle, the player recovers slowly when the player is in the Idle status. Since the amount of blood that a player loses reduces, automatic recovery naturally increases the amount of blood, which we will not talk about anymore.
Let's talk about how the blood on the head of the monsters in the game is achieved? Unlike the blood bar in the GUI, the blood bar above the head of the monster will move as the player moves. Here, we first need to introduce two concepts: the BillBoard and the HUD ).
The billboard is simply a quadrilateral or plane in the scenario. It is always aligned with the camera's line of sight. What can the bulletin board technology do? From the GUI in the game to the prompt message in the game, tree rendering in the scenario, and so on, the billboard technology will be used. For example, when playing a classic csgame, when the player presses the E key, the bomb can be removed. At this time, the game interface will prompt players to perform related operations. This is an application scenario of the billboard technology. In addition, in early 3D games, trees in scenes are usually implemented in a parallel form (paper), which can effectively reduce the number of rendering in the game, however, this will reduce the game performance, which is more common in games such as "Legend of the legend of the fairy sword" played by bloggers.
The head-up display device is a game term derived from the military field. It can intuitively display game-related information on the game screen, at the same time, it does not affect the player's normal field of view content, so that the player can understand the most important and most direct related content at any time. Common application scenarios of devices with a head-up display: life, magic, status, and mobility in RPG games, battle statistics, scores, highlights, and combos in ACT games. These two concepts are mentioned here mainly because their technical implementation is similar and basically involves the conversion of 2D and 3D coordinates. Here, the blood bar above the monster needs to convert the 3D coordinates in Unity3D into 2D coordinates, and then convert the 2D coordinates into the 3D coordinates used in NGUI. For details, refer to Xuan Yusong's article NGUI Research Institute's 3D model coordinates to 2D screen coordinates-blood strips. So how can we implement it here? Here, we can use NGUI to create a blood bar premade, and then mount it to a certain position on the head of the monster, and then ensure that the blood bar on the top of the monster is always oriented to the main camera. This is quite simple in Unity3D:
HPTrans.LookAt(Camera.main.transform.position);
In this way, the monster blood strip will display different proportions based on the distance from the camera. The blogger thinks the effect is quite good. Okay, the cute girl is besieged by a group of moles again. I am so cruel .......
Ii. Skill cooling
Skill cool is a concept that emerged after the rise of real-time games, especially World of Warcraft games. The so-called skill cool-down refers to the time from the last use of the skill to the start of this skill use, that is, the skill cd. An intuitive example is that if a skill icon is fully lit on the game interface, it indicates that the skill cool-down has ended; otherwise, it indicates that the skill is cool-down, however, we can use this skill again only after the skill cool-down is over. Let's take "Gu jianqitan 2" as an example to explain the skill cool-down, which is the actual combat image of the game.
In terms of the game production philosophy, "Gu jianqitan 2" broke through the Chinese single-host game combination system for the first time. It cannot be said that it was a major breakthrough in the history of Chinese single-host game production. You can notice that in the skill bar below the screen, the skills in this skill bar are cool at this time, so players cannot use these skills at this time, it can only be used after skill cooling ends. When talking about the battle of "Gu jianqitan 2", the bloggers still can't help but talk about it. First, the skill is too slow, and the players have to stop and wait for the game's protagonist to finish a set of actions, what is even more speechless is that players cannot stop their skills by interrupting the skills. Therefore, when playing this game, bloggers are in a state of disorder (smirking) most of the time ), however, when the voices of weapons and enemies stirred up together, the bloggers felt that playing the dance troupe was also a good choice, haha. Okay, no kidding. Next, we will take the Unity3D skill cooling effect as an example to explore the skill cooling effect. First, we will first understand the principle. The implementation of the skill cooling effect requires two NGUI controls, that is, the Sprite and FilledSprite controls. The Sprite control displays skill pictures, and the FilledSprite control displays skill masks. For skill masks, you only need to select a gray or black image and set the transparency. What are the advantages of the FilledSprite control that allows images to be filled in certain ways? For example, in many games, the cool-down effect of a skill image can be filled up from the bottom of the skill image or clockwise/counterclockwise at a certain speed. We can use FilledSprite to implement this requirement. The specific practices are as follows:
Next we wrap the skill icon and mask with an empty game body. Here we made three skill icons and set the transparency of the mask layer image to 150. Now let's take a look at the FilledSprite control. We noticed that it has an important parameter Fill Dir, that is, the Fill direction. Here we can change this value to achieve different filling effects, select clockwise here
360-degree filling. In addition, here there is a FillAmount parameter indicating the filling ratio, which is a value between 0 and 1. When this value is set to 1, it indicates full filling, that is, the entire mask layer is displayed, indicating that the skill is being cooled. If this value is set to 0, it indicates that the entire mask layer is not filled. That is, the skill cool-down ends. Based on this principle, we can easily implement the following script:
// Enter cool-down if (! IsSkillCooling) {timeout = 1; isSkillCooling = true;} // start timing if (isSkillCooling) {ui_skillMask.fillAmount-= (1.0F/SkillCoolTime) * Time. deltaTime; if (ui_skillMask.fillAmount <= 0.01F) {ui_skillMask.fillAmount = 0; isSkillCooling = false ;}}
Let's take a look at the actual results:
We can notice that the skill cool-down effect has basically come out, but we need to refresh the skill column over and over again. what we hope is that when the skill cool-down ends, when a player presses a certain key position or clicks a skill icon, the skill bar begins to cool after the player releases the skill technology. Based on this idea, based on the first script, we have improved the Code:
Using UnityEngine; using System. collections; public class cdSkill: MonoBehaviour {// the cool-down time of the skill is 2.0 spublic float SkillCoolTime = 2.0F; // The time when the player releases the skill is private float mSkillUseTime = 0; // skill length: 1.5 spublic float SkillLength = 1.5F; // skill Cool mask private GameObject go_skillMask; private UIFilledSprite ui_skillMask; // whether the cool state is private bool isSkillCooling = false; void Awake () {// get skill cool-down mask layer go_skillMask = transform. findChild ("SkillMask "). gam EObject; // obtain UIFilledSpriteui_skillMask = go_skillMask.GetComponent <UIFilledSprite> (); // bind a Click event UIEventListener. get (transform. gameObject ). onClick = SkillEvent;} void Update () {SkillCooling ();} void SkillCooling () {// enter if (! IsSkillCooling) {timeout = 1; isSkillCooling = true;} // start timing if (isSkillCooling) {ui_skillMask.fillAmount-= (1.0F/SkillCoolTime) * Time. deltaTime; if (Signature <= 0.01F) {ui_skillMask.fillAmount = 0; isSkillCooling = false ;}} if (msk1_setime <SkillCoolTime) {msk1_setime + = Time. deltaTime;} else {mskinisetime = SkillCoolTime;} void SkillEvent (GameObject go) {if (mskinisetime = SkillCoolTime) {Debug. log ("add specific skill effects here"); mSkillUseTime = 0; isSkillCooling = false ;}}}
Now, we have achieved two things in today's article: Blood strip and skill cool. I hope everyone will like it. It's really not much time for the bloggers to write code during this time, because they are about to graduate ...... finally, let's take a look at this little game played by the blogger! Special effects, please ignore them directly !........
Daily Rumor: the greatest competitor of a person is his laziness rather than others. You must do your best to say that you are lucky.
Certificate -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
If you like my blog, please remember my name: Qin Yuanpei. My blog address is blog.csdn.net/qinyuanpei.
Reprinted please indicate the source, Author: Qin Yuanpei, the source of this article: http://blog.csdn.net/qinyuanpei/article/details/40744781
Certificate ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Updated on April 9, November 4:
Due to the negligence of the blogger, there is an error in the Code. In the second section of the skill cool-down code, please comment out the 50th lines of code. Sorry.
How to use code to control NGUI controls such as Button, Lable, and SlicedSprite in Unity3d?
Very easy.
Public UILabel text;
Declaration, and then the script to an object, directly drag the label, and then directly use text. text = "Chinese" When referencing ";
This is the label.
As for the button event, do you want to ask?
Add the script buttonmessage and drag the script with the event to the target. Enter your method name for the function name below.
Who has Unity3d login verification implemented using the Ngui control? How should I obtain the Ngui input control value in the source code script? Details
Associate the label of the Input and obtain the text of the label. This is your Input.