1.血条缓慢变淡

        

unitySpriteRenderer遮挡 unity 图层遮挡关系_unity

        我们都知道,当敌人过多的时候,血条重叠在一起会给人在视觉上造成很大的干扰,也不利于打击感。所以,有必要建立血条缓慢变透明的脚本。


主要思路还是判断延迟血条与真实血条重合时,通过协程的间隔来控制血条的阿尔法透明值。让血条变淡。

下面是player子物体HP脚本。

unitySpriteRenderer遮挡 unity 图层遮挡关系_unity_02

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

public class HPControl : MonoBehaviour
{
    public float delHpSpeed = 1;//延迟血条减少速度
    public float delHpTime = 1;//延时血条减少时间间隔
    public float AlpHpTime = 1;//延时血条变淡速度
    private CharacterPanel character ;//角色面板
    private Transform hpBox;//血条图片
    private Transform delhpBox;//延迟血条图片
    private float delHPtime = 1;
    private float AlpHptime = 1;
    // Start is called before the first frame update
    void Start()
    {
        character = transform.GetComponentInParent<CharacterPanel>();
        hpBox = transform.GetChild(1);//0-HPmaxBox 1-hpBox 2-delhpBox
        delhpBox = transform.GetChild(2);
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        check();
       
    }
    //检查血量变化控制血条
    private void check() {

        float x = character.Hp / character.Hpmax;
        hpBox.localScale = new Vector3(x, 1, 1);
        if (Mathf.Abs(delhpBox.localScale.x )>  x)
        {//血条显示
            Debug.Log(1);
            for (int i = 0; i < transform.childCount; i++)
            {

                transform.GetChild(i).GetComponent<SpriteRenderer>().color = new Color(transform.GetChild(i).GetComponent<SpriteRenderer>().color.r, transform.GetChild(i).GetComponent<SpriteRenderer>().color.g, transform.GetChild(i).GetComponent<SpriteRenderer>().color.b, 1);
                Debug.Log(transform.GetChild(i).GetComponent<SpriteRenderer>().color.a);
            }
        }
        if ((Mathf.Abs(delhpBox.localScale.x )> x)&& delHPtime == 1)//延迟血条与血条有数差
        {
           
            //延迟血条开启协程
            StartCoroutine(delHP(x));
            delHPtime = 0;

        }
        //没有数差,且透明度不为0 协程血条变淡
        if (hpBox.GetComponent<SpriteRenderer>().color.a > 0&& AlpHptime==1)
        {

            StartCoroutine(AlpHp());
            AlpHptime = 0;
        }
    }
    IEnumerator delHP(float x)
    {
        yield return new WaitForSeconds(0.7f);//起初停顿时间

        while (Mathf.Abs(delhpBox.localScale.x )>= x)//直到延迟血条完成
        {

            yield return new WaitForSeconds(0.005f* delHpTime);//间隔0.005*delHpTime秒
            delhpBox.localScale =new Vector3(delhpBox.localScale.x-0.001f* delHpSpeed, 1, 1);
        }
        delHPtime = 1;

    }
    IEnumerator AlpHp()
    {

        while (hpBox.GetComponent<SpriteRenderer>().color.a > 0)
            {
                yield return new WaitForSeconds(0.2f * delHpTime);//等待0.2*delHpTime秒
            for (int i = 0; i < transform.childCount; i++)
            { transform.GetChild(i).GetComponent<SpriteRenderer>().color = new Color(transform.GetChild(i).GetComponent<SpriteRenderer>().color.r, transform.GetChild(i).GetComponent<SpriteRenderer>().color.g, transform.GetChild(i).GetComponent<SpriteRenderer>().color.b, transform.GetChild(i).GetComponent<SpriteRenderer>().color.a - 0.15f*AlpHpTime);
             
            }
            }
        AlpHptime = 1;

    }
}

2.伤害数值显示

为角色下面新建一个画布,然后在画布下面在新建txt文本。注意打开。画布的这模式为世界空间这样他就可以跟随角色进行移动。控制画布的scale调整好文本框和画布的位置。

注意调整好角色物体下各个子物体的图层顺序,不然可能会遮挡(这就是我准备下一课做背景的原因,早做背景不好调试)

unitySpriteRenderer遮挡 unity 图层遮挡关系_游戏程序_03

 为了使得游戏的扩展性强,我们为单独画布做一个脚本。用来控制角色画布上所有的UI。


其中角色面板函数

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CharacterPanel : MonoBehaviour
{

    public float Hpmax = 100;//最大生命
    public float Hp = 100;//生命
    public float Atk = 10;//攻击力
    public float AtkRan = 1;//攻击浮动
    public float Def = 10;//防御力

    private Transform Canvas;//获取角色个人UI面板
    // Start is called before the first frame update
    void Start()
    {
        Canvas = transform.Find("Canvas");
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        check();
    }
    //标准化检查
    private void check()
    {
        if (Hp > Hpmax) Hp = Hpmax;//血量不超过上限
        if (Hp < 0) Hp = 0;//血量不超过下限
    }
    public void hurt(float atk)
    {
       float hurtnum= (20 * atk / (20 + Def)) + Random.Range(-AtkRan, AtkRan);
 
        Canvas.GetComponent<CharaCanvas>().ShowHurtText(hurtnum);
        Hp -= hurtnum;
    
    }
}

其中CharaCanvas角色画布脚本为Canvas的

通过协程使得间隔几秒来控制字体大小,位置,颜色变化

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

public class HPControl : MonoBehaviour
{
    public float delHpSpeed = 1;//延迟血条减少速度
    public float delHpTime = 1;//延时血条减少时间间隔
    public float AlpHpTime = 1;//延时血条变淡速度
    private CharacterPanel character ;//角色面板
    private Transform hpBox;//血条图片
    private Transform delhpBox;//延迟血条图片
    private float delHPtime = 1;
    private float AlpHptime = 1;
    // Start is called before the first frame update
    void Start()
    {
        character = transform.GetComponentInParent<CharacterPanel>();
        hpBox = transform.GetChild(1);//0-HPmaxBox 1-hpBox 2-delhpBox
        delhpBox = transform.GetChild(2);
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        check();
       
    }
    //检查血量变化控制血条
    private void check() {

        float x = character.Hp / character.Hpmax;
        hpBox.localScale = new Vector3(x, 1, 1);
        if (delhpBox.localScale.x > Mathf.Abs(x))
        {//血条显示
       
            for (int i = 0; i < transform.childCount; i++)
            {

                transform.GetChild(i).GetComponent<SpriteRenderer>().color = new Color(transform.GetChild(i).GetComponent<SpriteRenderer>().color.r, transform.GetChild(i).GetComponent<SpriteRenderer>().color.g, transform.GetChild(i).GetComponent<SpriteRenderer>().color.b, 1);
                Debug.Log(transform.GetChild(i).GetComponent<SpriteRenderer>().color.a);
            }
        }
        if ((delhpBox.localScale.x > Mathf.Abs(x))&& delHPtime == 1)//延迟血条与血条有数差
        {
           
            //延迟血条开启协程
            StartCoroutine(delHP(x));
            delHPtime = 0;

        }
        //没有数差,且透明度不为0 协程血条变淡
        if (hpBox.GetComponent<SpriteRenderer>().color.a > 0&& AlpHptime==1)
        {

            StartCoroutine(AlpHp());
            AlpHptime = 0;
        }
    }
    IEnumerator delHP(float x)
    {
        yield return new WaitForSeconds(0.7f);//起初停顿时间

        while (delhpBox.localScale.x >= Mathf.Abs(x))//直到延迟血条完成
        {

            yield return new WaitForSeconds(0.005f* delHpTime);//间隔0.005*delHpTime秒
            delhpBox.localScale =new Vector3(delhpBox.localScale.x-0.001f* delHpSpeed, 1, 1);
        }
        delHPtime = 1;

    }
    IEnumerator AlpHp()
    {

        while (hpBox.GetComponent<SpriteRenderer>().color.a > 0)
            {
                yield return new WaitForSeconds(0.2f * delHpTime);//等待0.2*delHpTime秒
            for (int i = 0; i < transform.childCount; i++)
            { transform.GetChild(i).GetComponent<SpriteRenderer>().color = new Color(transform.GetChild(i).GetComponent<SpriteRenderer>().color.r, transform.GetChild(i).GetComponent<SpriteRenderer>().color.g, transform.GetChild(i).GetComponent<SpriteRenderer>().color.b, transform.GetChild(i).GetComponent<SpriteRenderer>().color.a - 0.15f*AlpHpTime);
             
            }
            }
        AlpHptime = 1;

    }
}

注意事项:将text文本调的足够大,不然当字体大小超过文本框大小的时候,字体不显示。你也可以设置好看字体,居中显示,允许横向溢出。

然后将角色面板物体复制给敌人物体,并更新预制体。