首先呢,血条文本比较简单,基本上和血条差不多的操作。
首先建一个text,然后就是输入血条数值。
接下来就全是代码的事情了
做得一头雾水
stat代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class stat : MonoBehaviour {
private Image content;
[SerializeField]
private Text statValue;
[SerializeField]
private float lerpSpeed;
private float currentFill;
public float MyMaxValue { get; set; }
private float currentValue;
public float MyCurrentValue
{
get
{
return currentValue;
}
set
{
if(value>MyMaxValue)
{
currentValue = MyMaxValue;
}
else if(value<0)
{
currentValue = 0;
}
else
{
currentValue = value;
}
currentFill = currentValue / MyMaxValue;
statValue.text = currentValue + "/" + MyMaxValue;
}
}
// Use this for initialization
void Start () {
content = GetComponent<Image>();
}
// Update is called once per frame
void Update ()
{
if (currentFill != content.fillAmount)
{
content.fillAmount = Mathf.Lerp(content.fillAmount, currentFill, Time.deltaTime * lerpSpeed);
}
}
public void Initialize(float currentValue, float maxValue)
{
MyMaxValue = maxValue;
MyCurrentValue = currentValue;
}
}
character代码:using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public abstract class character : MonoBehaviour {
[SerializeField]
private float speed;
private Animator myAnimator;
// Use this for initialization
protected Vector2 direction;
private Rigidbody2D myRigidbody;
public bool IsMoving
{
get
{
return direction.x != 0 || direction.y != 0;
}
}
protected virtual void Start() {
myRigidbody = GetComponent<Rigidbody2D>();
myAnimator = GetComponent<Animator>();
}
// Update is called once per frame
protected virtual void Update () {
HandleLayers();
}
private void FixedUpdate()
{
Move();
}
public void Move()
{
myRigidbody.velocity = direction.normalized * speed;
//x=20,y=2//Normalize =1,1
}
public void HandleLayers()
{
if (IsMoving)
{
ActivateLayer("WalkLayer");
myAnimator.SetLayerWeight(1, 1);
myAnimator.SetFloat("x", direction.x);
myAnimator.SetFloat("y", direction.y);
}
else
{
ActivateLayer("IdleLayer");
}
}
public void ActivateLayer(string layerName)
{
for(int i = 0;i <myAnimator.layerCount;i++)
{
myAnimator.SetLayerWeight(i, 0);
}
myAnimator.SetLayerWeight(myAnimator.GetLayerIndex(layerName), 1);
}
}
player代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class player : character {
[SerializeField]
private stat health;
[SerializeField]
private stat mana;
[SerializeField]
private float initHealth = 100;
private float initMana = 50;
protected override void Start()
{
health.Initialize(initHealth,initHealth);
mana.Initialize(initMana, initMana);
base.Start();
}
// Use this for initialization
// Update is called once per frame
protected override void Update()
{
GetInput();
base.Update();
}
private void GetInput()
{
direction = Vector2.zero;
//for debug
if(Input.GetKeyDown(KeyCode.I))
{
health.MyCurrentValue -= 10;
mana.MyCurrentValue -= 10;
}
if(Input.GetKeyDown(KeyCode.O))
{
health.MyCurrentValue += 10;
mana.MyCurrentValue += 10;
}
//here
if(Input.GetKey(KeyCode.W))
{
direction += Vector2.up;
}
if (Input.GetKey(KeyCode.A))
{
direction += Vector2.left;
}
if (Input.GetKey(KeyCode.S))
{
direction += Vector2.down;
}
if (Input.GetKey(KeyCode.D))
{
direction += Vector2.right;
}
}
}