Unity3d role Overhead Information 3d&2d occlusion solution

Source: Internet
Author: User

First, only by the word description, brain repair should be some difficulty--

There are three characters (we call them a, B, C) from left to right, a 2D UI (the Middle Action selection box), a cube (right square)

Cube blocks the head of character C.

The role and cube are blocked by the UI

The common denominator is to show only the parts that are not obscured, and the masked ones do not show

I believe most people read this article before I have seen the Rain pine Momo "the name of the person's head and the Blood bar update and draw"

Not seen can go to see: http://www.xuanyusong.com/archives/1032

If you use the code in the Momo article directly, you will find some problems,

Here I do not use the blood bar, only draw the text, all the same!

First: Because it is drawn with Ongui, the role nickname and Blood Bar used in the process will block the UI created by Ngui. You can make up your mind. When you open your backpack, a line of text and blood strips block your backpack ...

Second: When the character is obscured by other objects, here I'm talking about a 3D object, not a UI. How to say better understand it!! Just like the character walked behind the wall, the character has been obscured by the wall, but the character nickname and Blood bar still show (perhaps some people want this effect!). Well, at least in my project this effect gives me a very bad feeling.

Third: When there are two characters in the same scene, two characters have their own nickname and Blood Bar, if you and another character is too far away and rotate the angle, the name and the blood bar unexpectedly appeared in the sky! Yes, it's the sky.

...... The above mentioned just point out some bugs, and no superfluous meaning, we do not spray, I am also a rookie, just share the experience with you!!

Momo The purpose of writing this article is to give everyone a thought, specifically in the project how to achieve the desired effect on their own

This idea is very good, but because of some of my own project reasons for the time not to use this idea, this idea we can go to see, will certainly be able to use

Now with another way of thinking, here we have to learn a thing: layer (layers)

For an introduction to layer, please see the Holy Scriptures: http://game.ceeger.com/Components/Layers.html

In fact, I have not seen, in order to write this article only to search, I'm all right when the blind point will know what it is to do

Because we need to use Ngui, we first import the Ngui plug-in package

Go to the Chase:

1, create an empty Gameobject, renamed to Namepanel, set as the role sub-object, then reset and adjust the position to the role of the head position

2, the Namepanel layer set and the role of the same tier, and add a UIPanel

3. Add Uilabel under Namepanel by selecting Namepanel->ngui->create->label and setting the Label overflow property to resizefreely ( Automatically adapts to text size and length)

4. Set the properties of the Uilabel component. Font, display text, font color. Effect Select outline, color selection is black, this parameter is stroke, make the font clearer

Why, what's the size of the font? Change the font size a little bit, 10, 5, 1, how is it still so big, and can not see clearly

Don't worry, it's not your operation error, it's not finished!

When I first encountered this problem I also wondered, and later thought, there may be no reason to add uiroot

Used Ngui know, Uiroot is an essential part of Ngui, on the phone adaptive resolution depends on it

Well, now that we know why, let's add a uiroot and try it.

5, select Namepanel, add a uiroot component, Scaling style select Fixedsizeonmobiles

Haha, the fonts are back to normal! If you feel the font is small, you can adjust the font size a bit

When you run the game, you'll notice that when the character moves, the top of the head information follows the character, but when the character rotates or the camera rotates, the head information rotates toward the camera. If that's what you want, it's a big drop, it's not easy to figure out the code.

Now let's write the code, the code is simple.

1      Publicgameobject targets;2     Privatecamera camera;3     4     voidStart ()5     {6         //get the main camera7Camera =Camera.main;8     }9 Ten     voidUpdate () One     { A         //Set Rotation angle -Targets.transform.rotation =camera.transform.rotation; -}

Save the code, hang the script on the character, and drag the Namepanel to the parameter targets

Finally run the game, look at the effect, OK, perfect

Finally, summarize:

The code is very simple, look at my code to know, can say that there is only one line of code ...

The main is to set the layer, the layer must be divided clearly, very important!

Well, originally wanted to put the HUD text plug-in also said, the belly is hungry, first to eat something, a while in another write an article

This address: http://www.cnblogs.com/shenggege/p/4179012.html

Unity3d role Overhead Information 3d&2d occlusion solution

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.