加载场景时,保存经验值,并让经验值在UI实时更新显示
Experience
- using RPG.Saving;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- namespace RPG.Resources
- {
- public class Experience : MonoBehaviour,ISaveable
- {
- [SerializeField] float experiencePoints = 0;
-
-
- public void GainExperience(float experience)// 获得经验
- {
- experiencePoints += experience;
- }
-
- public float GetPoint()//返回经验的点数 -用于UI显示
- {
- return experiencePoints;
- }
-
-
- public object CaptureState()
- {
- return experiencePoints;
- }
-
- public void RestoreState(object state)
- {
- experiencePoints = (float)state;
- }
- }
- }
复制代码
ExperienceDisplay
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
-
- namespace RPG.Resources
- {
- public class ExperienceDisplay : MonoBehaviour
- {
- Experience experience;
- Text xpDisplay;
-
- private void Awake()
- {
- experience = GameObject.FindWithTag("Player").GetComponent<Experience>();
- xpDisplay = GetComponent<Text>();
- }
-
- private void Update()
- {
- xpDisplay.text = String.Format("{0:0}", experience.GetPoint());
- }
- }
- }
-
复制代码
|