Unity's name tag, blood bar and other overhead pendants

Source: Internet
Author: User

Unity in the realization of the name of the head of the figure, the implementation of the blood bar, many online blogs have mentioned. Here combined with their own development in the project, a brief summary of a few points.

Xuanhusung's popular blog mentions the direct use of Unity's own Ongui () to achieve character blood strips: http://www.xuanyusong.com/archives/1032.

Aside from the update of the Blood Bar progress bar, focus on updating the position of the head object. To simplify, to achieve the name of the head of the character card.

The core code is as follows:

Using unityengine;using System.collections;public class Following:monobehaviour {public    Camera M_camera = Null;
   public Gameobject m_gofollowing = null;    Public Vector3 M_voffset;    void Ongui ()    {        Vector3 vposscreen = m_camera.worldtoscreenpoint (m_goFollowing.transform.position + m_voffset );        Gui. Label (New Rect (vposscreen.x, SCREEN.HEIGHT-VPOSSCREEN.Y, 200, 80), "Shepherd Boy Fantasy Trip");    


However, the actual project temporarily does not use Unity's native UI, the current project is mostly used unity4.x version, the native UI is too difficult to use, multi-use Ngui and other plug-ins. The following is a main introduction to unity in the use of Ngui to achieve the head of the person's hanging pieces.
Ngui implementation of overhead objects can be divided into two ways according to the rendering of the camera: (1) The rendering of the scene of the Maincamera to render overhead objects, such as the overhead objects can be regarded as ordinary 3D objects, can be obscured by 3D objects in the scene. (2) Dedicated UI camera rendering, overhead objects as the UI, will not be obscured by 3D objects in the scene. Generally we will adopt the way 2,ui try not to render with the main camera and render with a dedicated UI camera. However, let's take a look at the core code that uses the way (1) to handle the UI:
public class Following:monobehaviour {public    gameobject m_gofollowing = null;    public float m_foffset = 0;    void Update ()    {        gameObject.transform.position = m_goFollowing.transform.position + new Vector3 (0, M_foffset, 0 );        GameObject.transform.rotation = Camera.main.transform.rotation;    }}
If you find that UILabel is not visible in practice, please note: (1) UILabel uisprite cannot be used alone, it must be accompanied by a panel, as a sub-node of the panel or Nguipanel and UILabel Uisprite hangs under the same gameobject. (2) At the same time Uilabel layer must be consistent with the accompanying nguipanel. Make sure that your camera does not block the layer where the Uilabel is located. (3) UILabel Uisprite under any panel, try to put it under Uiroot. If some objects are invisible, adjust the scale.
We typically use the 2,ui to render with a dedicated UI camera, and the core is to map the transform under the main camera to the transform under the UI camera.
Using unityengine;using System.collections;public class Following:monobehaviour {public Gameobject m_goFollowing = n    Ull    public float m_foffset = 0;    Private Camera M_go3dcamera = null;    Private Camera M_go2dcamera = null; void Start () {M_go3dcamera = Gameobject.findwithtag ("Maincamera").        Getcomponent<camera> (); M_go2dcamera = Gameobject.find ("UI Root"). Transform. Findchild ("Camera").    Getcomponent<camera> ();        }void Update () {Vector3 vPOS = Transpos (m_goFollowing.transform.position + new Vector3 (0, M_foffset, 0));    GameObject.transform.position = new Vector3 (vpos.x, vpos.y, 0);   Get proper Z, z = 0.0f simply gameObject.transform.rotation = m_go2DCamera.transform.rotation;  Keep the same rotation with camera, so Parallel with camera}//transform Worldpos in 3DCamera to new Worldpos In 2DCamera. They share the same screenpos Vector3 Transpos (Vector3 worldpos) {if (M_go3dcamera = null | | m_Go2dcamera = = null) {return vector3.zero;        } Vector3 Screenpos = M_go3dcamera.worldtoscreenpoint (Worldpos);        Vector3 Newworldpos = M_go2dcamera.screentoworldpoint (Screenpos);    return newworldpos; }}
The main points of this are written in comments. The game scene generally uses 3DCamera rendering, the name card blood bar and so on belong to the UI, should be rendered by the UI camera. The name tag on the top of the head. With the object in the 3D scene, the key is how to map the position in the 3D camera space to the UI camera camera space. The Middle bridge is the screen position, and the top objects seen by two cameras should map to the same screen coordinates. Also, two details need to be noted: 1. To ensure that the UI is visible, the coordinate depth that is obtained through the UI camera transformation is set to the appropriate value, too far or too close to the field of view. Can be set simply to 0. 2. The overhead object should be kept in parallel with the UI camera, that is, the rotation of the overhead object is set to the rotation of the UI camera.
In practical applications, there are some performance issues to be aware of. (1) Reduce Drawcall:ngui built-in merge drawcall mechanism, the same panel, not interrupted by the depth interval segment, using the same material, these widgets can be merged into a drawcall. Specifically, using the same Atlas Uisprite, the depth interval is hung under the same uipanel, and this depth interval is not used by other atlas in this uipanel. Uilabel also assigns a depth interval for each font. Of course, the premise is not to affect functional requirements. Take the name of the head of the strip Namebar, blood strips Hpbar For example, Namebar.prefab, Hpbar.prefab itself does not carry uipanel, we create a common uipanel--headwearingpanel for the head of the object under Ngui-uiroot, all the instantiated Namebar, Hpbar are hung under the headwearingpanel. In addition, NGUI3.8.0 previous versions, Show/Hide uiwdget will cause all uidrawcall to be destroyed and rebuilt, with a larger CPU load. If you use a previous version of NGUI3.8.0, it is recommended that you do not use Gameobejct.setactive (false) to hide the UI control, either Transform.scale = Vector3.zero, or move the UI you want to hide out of view. The NGUI3.8.0 version does not have this problem. (2) Reduce GC make a resource pool for the UI that you want to create destruction frequently. Avoid frequent creation of a large number of GC for destroying objects. Create a pool for each type of prefab. Delete Namebar, Hpbar, do not really destroy, hide it. Common Namebar, Hpbar, first from the pool to find out if there are available Namebar, Hpbar, if not found to really instantiate the creation of new objects.







Unity's name tag, blood bar and other overhead pendants

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.