UGUI의 Mask를 이용하여 HPbar를 만들어보았습니다.
ㅇ소스코드
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | using UnityEngine; using UnityEngine.UI; using System.Collections; public class HPbar : MonoBehaviour { public Image mask; private RectTransform maskRect; public float maxHP; private float currentHP; private float maxHpBarWidth; // Use this for initialization void Start () { maskRect = mask.GetComponent<RectTransform> (); maxHpBarWidth = maskRect.sizeDelta.x; currentHP = maxHP; } public void HpUP() { currentHP += 10; if (currentHP > maxHP) { currentHP = maxHP; } float deltaSize = currentHP / maxHP; maskRect.sizeDelta = new Vector2(maxHpBarWidth * deltaSize, maskRect.sizeDelta.y); } public void HpDown() { currentHP -= 10; if (currentHP < 0) { currentHP = 0; } float deltaSize = currentHP / maxHP; maskRect.sizeDelta = new Vector2(maxHpBarWidth * deltaSize, maskRect.sizeDelta.y); } } | cs |
'Unity > UGUI' 카테고리의 다른 글
Unity RichText (0) | 2016.06.25 |
---|---|
UGUI의 Toggle을 이용하여 RadioButtonGroup 만들기(Toggle Group) (5) | 2016.03.08 |
스킬 쿨타임 UI 만들기 (3) | 2016.03.07 |
UGUI (2) | 2016.03.04 |