实现原理:动态改变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(); 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(); }
实现效果: