Thứ Năm, 6 tháng 3, 2014

Project RPG BÀI 13. SỬ DỤNG GUI TEXTURE CHO THANH MÁU

 


Ở bài viết đầu tiên, chúng ta đã tạo thanh máu cho nhân vật bằng GUI Box, bây giờ, chúng ta sẽ sử dụng GUI Texture để hiển thị thanh máu với sự hỗ trợ của C# Messenger Extended đã tạo ở bài 12.



B1. Double click vào scene Level1 để làm việc với màn này. Tại thẻ Project, nhấp phải vào thư mục Prefab và chọn Create | Prefabs và đặt tên là Player Health bar Prefab.



B2. Tải file guiTexture.rar này về (Tệp / Lưu Ctrl + S) giải nén và kéo thả ảnh redBar.png vào thư mục Skin, kéo thả thư mục monsterMob vào thư mục Assets.

B3. Vào GameObject | Create Other | GUI Texture. Tại thẻ Project, kéo thả file ảnh redBar vào Texture của UnityWatermark-small ở thẻ Inspector vừa tạo ở bước trên và điều chỉnh như sau:


B4. Kéo thả file C# VitalBar ở thẻ Project vào UnityWatermark-small ở thẻ Hierarchy.


B5. Kéo thả UnityWatermark-small vào Player Health bar Prefab ở thẻ Project.


B6. Nhấp chọn UnityWatermark-small ở thẻ Hierarchy và ấn Delete.


B7. Kéo thả Player Health bar Prefab ở thẻ Project vào thẻ Hierarchy.


B8. Tại thẻ Project, nhấp chọn Player Health bar Prefab, ấn Ctrl + D để nhân đôi đối tượng này lên và đổi tên đối tượng mới thành Mob Health bar Prefab.


B9. Kéo thả  Mob Health bar Prefab ở thẻ Project vào thẻ Hierarchy.


B10. Nhấp chọn Mob Health bar Prefab ở thẻ Hierarchy, qua thẻ Inspector, điều chỉnh các thông số như hình sau:


B11. Double click vào file C# VitalBar, xóa tất cả đi và chèn lại đoạn code sau vào:

 /// <summary>
/// VitalBar.cs
///
/// This class is responsble for displaying a vita fpr the player character or a mob...
/// </summary>
using UnityEngine;
using System.Collections;

public class VitalBar : MonoBehaviour {
    public bool _isPlayerHealthBar;

    private int _maxBarLength;
    private int _curBarLength;
    private GUITexture _display;

    // Use this for initialization
    void Start () {
        //_isPlayerHealthBar = true;

        _display = gameObject.GetComponent<GUITexture>();

        _maxBarLength = (int)_display.pixelInset.width;
        OnEnable();
    }
  
    // Update is called once per frame
    void Update () {
  
    }

    //This method is called when the gameobject is enabled
    public void OnEnable(){
        if(_isPlayerHealthBar)
            Messenger<int, int>.AddListener("player health update", OnChangeHealthBarSize);
        else
            Messenger<int, int>.AddListener("mob health update", OnChangeHealthBarSize);
    }

    //this method is called when the gameobject is disabled
    public void OnDisable(){
        if(_isPlayerHealthBar)
            Messenger<int, int>.RemoveListener("player health update", OnChangeHealthBarSize);
        else
            Messenger<int, int>.RemoveListener("mob health update", OnChangeHealthBarSize);

    }

    //This method will calculate the total size of the healthbar in relation to the % of health the target has left
    public void OnChangeHealthBarSize(int curHealth, int maxHealth){
        //Debug.Log("We heard an event: curHealth = " + curHealth + " - maxHealth = " + maxHealth);
        _curBarLength = (int)((curHealth / (float)maxHealth) * _maxBarLength);    //this calculate the current bar length based on the player health %
        _display.pixelInset = new Rect(_display.pixelInset.x, _display.pixelInset.y, _curBarLength, _display.pixelInset.height);
  
    }

    //setting the health bar to the player or mob
    public void SetPlayerHealth(bool b){
        _isPlayerHealthBar = b;
    }
}


B12. Double click vào file C# PlayerCharacter, xóa tất cả đi và chèn lại đoạn code sau vào:

 public class PlayerCharacter : BaseCharacter {
    void Update(){
        Messenger<int, int>.Broadcast("player health update", 80, 100);
    }
}


B13. Tại thẻ Hierarchy, nhấp chọn Mob Health bar Prefab, qua thẻ Inspector, bỏ dấu stick ở mục Is Player Health Bar đi.


B14. Tại thẻ Hierarchy, nhấp chọn Player Health bar Prefab, qua thẻ Inspector, đánh dấu stick vào mục Is Player Health Bar.


B15. Nhấn Ctrl + S để save scene Level1 này lại. Nhấp nút Play để kiểm tra thành quả.


Không có nhận xét nào:

Đăng nhận xét