前言:这章我们将修改一下脚本,加入委托的使用。然后在下一篇文章中,我们将真正的去学习观察者设计模式。
1.创建一个空物体,修改名字为:“SceneController”,并创建脚本“SceneController.cs”,将我们的角色初始化放在这个脚本中。
2.脚本如下:
using UnityEngine;
public class SceneController : MonoBehaviour
{
private void Awake()
{
}
private void Start()
{
Player.Instance.Init();
}
}
1.在Gamecontroller.cs脚本中,定义一个Action委托方法。
2.当角色血量变化的时候,调用该委托方法。
3.脚本如下:
using System;
using UnityEngine;
public class GameController : MonoBehaviour
{
public static GameController Instance;
public Action OnChangeHp;
private void Awake()
{
Instance = this;
}
private void Start()
{
}
private void Update()
{
//按下数字键 1 减血
if (Input.GetKeyDown(KeyCode.Alpha1))
{
if (!Player.Instance.PlayerDead())
{
ChangeHp(-20);
}
}
//按下数字键 2 加血
if (Input.GetKeyDown(KeyCode.Alpha2))
{
if (!Player.Instance.PlayerFullBlood())
{
ChangeHp(20);
}
}
}
///
/// 修改血量
///
private void ChangeHp(int hp)
{
if (OnChangeHp != null)
{
OnChangeHp.Invoke(hp);
}
}
}
1.监听GameController.cs脚本上的委托。
2.定义一个Action委托方法。
3.当角色血量变化的时候,调用该委托方法。
4.代码如下:
using System;
using UnityEngine;
public class Player : MonoBehaviour
{
public static Player Instance;
public Action OnChangeHp;
///
/// 满血血量
///
public float Max_Hp;
///
/// 当前血量
///
public float Current_Hp;
private void Awake()
{
Instance = this;
}
private void Start()
{
GameController.Instance.OnChangeHp += ChangeHp;
}
private void Destroy()
{
if (GameController.Instance.OnChangeHp != null)
{
GameController.Instance.OnChangeHp -= ChangeHp;
}
}
///
/// 角色初始化信息
///
public void Init()
{
Max_Hp = 100;
Current_Hp = Max_Hp;
}
///
/// 判断当前角色是否死亡
///
///
public bool PlayerDead()
{
if (Current_Hp <= 0)
{
Current_Hp = 0;
return true;
}
return false;
}
///
/// 判断当前角色是否满血
///
///
public bool PlayerFullBlood()
{
if (Current_Hp >= Max_Hp)
{
Current_Hp = Max_Hp;
return true;
}
return false;
}
public void ChangeHp(float hp)
{
Debug.Log("Player收到消息 当前血量:" + Current_Hp);
Current_Hp += hp;
float b = Current_Hp / Max_Hp;
if (OnChangeHp != null)
{
OnChangeHp.Invoke(Current_Hp, b);
}
}
}
1.监听Player.cs脚本上的委托。
2.修改对应的参数。
3.脚本如下:
using UnityEngine;
using UnityEngine.UI;
public class UIPlayer : MonoBehaviour
{
private Image m_HpImage;
private void Awake()
{
m_HpImage = transform.Find("Canvas/HP_Image/Image").GetComponent();
}
private void Start()
{
Player.Instance.OnChangeHp += ChangeHp;
}
private void Destroy()
{
if (Player.Instance.OnChangeHp != null)
{
Player.Instance.OnChangeHp -= ChangeHp;
}
}
public void ChangeHp(float currHp, float b)
{
m_HpImage.transform.localScale = new Vector3(b, 1.0f, 1.0f);
}
}
using UnityEngine;
using UnityEngine.UI;
public class UIRoot01 : MonoBehaviour
{
private Image m_HpImage;
private void Awake()
{
m_HpImage = transform.Find("Canvas/HP_Image/Image").GetComponent();
}
private void Start()
{
Player.Instance.OnChangeHp += ChangeHp;
}
private void Destroy()
{
if (Player.Instance.OnChangeHp != null)
{
Player.Instance.OnChangeHp -= ChangeHp;
}
}
public void ChangeHp(float currHp, float b)
{
m_HpImage.transform.localScale = new Vector3(b, 1.0f, 1.0f);
}
}
using System.Text;
using UnityEngine;
using UnityEngine.UI;
public class UIRoot02 : MonoBehaviour
{
private Text m_Text;
private void Awake()
{
m_Text = transform.Find("Canvas/PlayerInfo/Text").GetComponent();
}
private void Start()
{
Player.Instance.OnChangeHp += ChangeHp;
}
private void Destroy()
{
if (Player.Instance.OnChangeHp != null)
{
Player.Instance.OnChangeHp -= ChangeHp;
}
}
public void ChangeHp(float currHp, float b)
{
StringBuilder sb = new StringBuilder();
sb.Append("当前血量:");
sb.Append(currHp);
m_Text.text = sb.ToString();
}
}
这章我们完成了加入委托的方式去实现我们想要的功能,那么下一章,我们将真正的学习观察者设计模式。