UGUI SpriteRender 实现血量变化效果

SpriteRender 设置:

// draw mode 设置为sliced

 

主要实现:

    // draw mode 设置为sliced
    public SpriteRenderer progress;

 

  GlobalFunction.ChangeHP(progresspercenttotalL);

 

    /** 血条变化
     * 
     */
    public static void ChangeHP(SpriteRenderer progressfloat percent,float totalL)
    {
        progress.transform.localPosition = GlobalFunction.GetHPLocalPos(totalLpercent);
        progress.size = new Vector2(totalL * percentprogress.size.y);
    }

 

 

test:

 

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

public class TestProgress : MonoBehaviour
{
    // draw mode 设置为sliced
    public SpriteRenderer progress;

    float totalL;
    float percent = 1f;

    private void Start()
    {
        totalL = progress.size.x;

        Debug.Log("totalL : " + totalL);
    }

    void Change ()
    {
        percent -= 0.1f;

        //progress.transform.localPosition = GlobalFunction.GetHPLocalPos(totalL, percent);
        //progress.size = new Vector2(totalL * percent, progress.size.y);

        GlobalFunction.ChangeHP(progresspercenttotalL);
    }

    void Update () 
    {
        if(Input.GetKeyDown(KeyCode.Space))
        {
            Change();
        }
    }
}
 

你可能感兴趣的:(游戏开发)