【原】Unity3d 类似Dota血条

实现原理:动态改变GUItexture的宽度。

 

 

  

    EnemyInfo enemyInfo;
    public GUITexture enemyBoxGUI;
    private Texture enemyBox;
    private Texture enemyHp;
    private int currentHp;
    private float hpLength;
    // Use this for initialization
    void Start ()
    {
    enemyInfo=gameObject.transform.parent.GetComponent<EnemyInfo>();
    enemyBox=enemyBoxGUI.texture;
    enemyHp=guiTexture.texture;
    currentHp=enemyInfo.Health();
    hpLength=enemyHp.width/3;  
    }
    // Update is called once per frame
    void Update ()
    {
        print(currentHp);
        Vector3 pos=Camera.mainCamera.WorldToViewportPoint(transform.parent.position);
        enemyBoxGUI.transform.position=new Vector3(pos.x-0.05f,pos.y,pos.z-0.2f);
        guiTexture.transform.position=new Vector3(pos.x-0.05f,pos.y,pos.z-0.2f);
        enemyBoxGUI.pixelInset=new Rect(10,20,enemyBox.width/6,enemyBox.height/8);
        guiTexture.pixelInset=new Rect(10,25,hpLength*currentHp/enemyInfo.AllHealth,enemyHp.height/3);
        currentHp=enemyInfo.Health();
    }

实现效果:

 

posted @ 2012-07-11 22:15  U_探索  阅读(1226)  评论(0编辑  收藏  举报