반응형

0

그림과 같이  Player의 HP Bar 를 만들고 적의 공격을 당했을때 HP Bar 가 감소 되는 작업을 하겠습니다

 

 

새로운 Canvas 를 만들과 새로운 Canvas 아래에 아래 그림과 같이 만듬니다  새로운 image  생성하여 이름을 Panel

새로운 Text 생성하여  PlayerName   새로운 Text 생성하여  PlayerMoney  새로운 image 두개를 생성하여 하나는 색깔을 회색으로 하고 이름을 HPBack 그리고 색깔을 붉은색으로 하여 이름을 HPFront 라 합니다 

 

각각 생성한 다음 아래 그림과 같이 Anchors 를 왼쪽 위로 설정한다

 

 

그리고 UIManager 스크립트를 새로만든다

 

UIManager 스크립트 작성

 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIManager : MonoBehaviour
{
    // 언제 어디서나 쉽게 접금할수 있도록 하기위해 만든 정적변수
    public static UIManager instance;

    public Text playerName;
    public Text playerMoney;
    public Image playerHPBar;


    private void Awake()
    {
        if (instance == null)
        {
            instance = this;
        }
    }

    public void UpdatePlayerUI(PlayerParams playerParams)
    {
        playerName.text = playerParams.name;
        playerMoney.text = "Coin : " + playerParams.money.ToString();
        playerHPBar.rectTransform.localScale = 
            new Vector3((float)playerParams.curHp / (float)playerParams.maxHp, 1f, 1f);

    }
    void Update()
    {
        
    }
}

 

 

 

새로 만든  Canvas 를 선택하고 새로 만든 UIManager를 드래그 드롭한다

 

 

Canvas 의 UIManager 에 각각 의 빈곳에 PlayerName, PalyerMoney, PlayerHPBar 에 드래그 드롭합니다

 

 

PlayerParams스크립트를 선택하고 수정합니다

 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerParams : CharacterParams
{
    public string name { get; set; }
    public int curExp { get; set; }
    public int expToNextLevel { get; set; }
    public int money { get; set; }

    public override void InitParams()
    {
        name = "hong";
        level = 1;
        maxHp = 100;
        curHp = maxHp;
        attackMin = 5;
        attackMax = 8;
        defense = 1;

        curExp = 0;
        expToNextLevel = 100 * level;
        money = 0;

        isDead = false;

        //초기화 할때 헤드업 디스플레이에 플레이어의 이름과 기타 정보들이 제대로 표시되도록함
        UIManager.instance.UpdatePlayerUI(this);
    }

    protected override void UpdateAfterReceiveAttack()
    {
        base.UpdateAfterReceiveAttack();

        UIManager.instance.UpdatePlayerUI(this);
    }
}

 

게임을 실행하고 플레이어 HP 가 적의 공격을 받았을때 줄어 드는지 확인 합니다

 

 

 

0

반응형

+ Recent posts