SpriteRender 设置:
// draw mode 设置为sliced
主要实现:
// draw mode 设置为sliced
public SpriteRenderer progress;
GlobalFunction.ChangeHP(progress, percent, totalL);
/** 血条变化
*
*/
public static void ChangeHP(SpriteRenderer progress, float percent,float totalL)
{
progress.transform.localPosition = GlobalFunction.GetHPLocalPos(totalL, percent);
progress.size = new Vector2(totalL * percent, progress.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(progress, percent, totalL);
}
void Update ()
{
if(Input.GetKeyDown(KeyCode.Space))
{
Change();
}
}
}