FPS Microgame 代码解析——UI提示(2)

2. 消息的文字产生

2.1 开头“Kill all the enemy”的产生

  • 需要objectiveKillEnemy.prefab。
  • 挂载脚本ObjectiveKillEnemies。
    在这里插入图片描述
  • Title为所要展示的文字信息。其他不需要。
using Unity.FPS.Game;
using UnityEngine;

namespace Unity.FPS.Gameplay
{
   
   
    public class ObjectiveKillEnemies : Objective
    {
   
   
        
        protected override void Start()
        {
   
   
            base.Start();//Objective.cs
            

            EventManager.AddListener<EnemyKillEvent>(OnEnemyKilled);

            // set a title and description specific for this type of objective, if it hasn't one
            if (string.IsNullOrEmpty(Title))
                Title = "Eliminate " + " the" 
                        +" enemy";
        }
        void OnDestroy()
        {
   
   
            EventManager.RemoveListener<EnemyKillEvent>(OnEnemyKilled);
        }
    }
}

2.2 Title的产生:

  • 利用Objective.cs来存储函数,不挂载。
  • 利用using Unity.FPS.Game来使用。
  • 代码:
using System;
using UnityEngine;

namespace Unity.FPS.Game
{
   
   
    public abstract class Objective : MonoBehaviour
    {
   
   
        [Tooltip("Name of the objective that will be shown on screen")]
        public string Title;

        [Tooltip("Delay before the objective becomes visible")]
        public float DelayVisible;

        public bool IsCompleted {
   
    get; private set; }
        public bool IsBlocking() => !(IsCompleted);

        public static event Action<Objective> OnObjectiveCreated;
        public static event Action<Objective> OnObjectiveCompleted;

        protected virtual void Start()
        {
   
   
            OnObjectiveCreated?.Invoke(this);

            DisplayMessageEvent displayMessage = Events.DisplayMessageEvent;//Event.cs里定义的
            displayMessage.Message = Title;
            displayMessage.DelayBeforeDisplay = 0.0f;
            EventManager.Broadcast(displayMessage);
        }


      /*  public void CompleteObjective(string descriptionText, string counterText, string notificationText)
        {
            IsCompleted = true;//可能要用于结束消息提示。 单独挑出来
            ObjectiveUpdateEvent evt = Events.ObjectiveUpdateEvent;
            evt.Objective = this;
            evt.NotificationText = notificationText;
            evt.IsComplete = IsCompleted;
            EventManager.Broadcast(evt);

            OnObjectiveCompleted?.Invoke(this);
        }*/
        //任务完成时,借鉴使用 
    }
}

2.3 Event.cs–定义事件名字

  • 不挂载,用于存储函数。
  • 命名空间为Unity.FPS.Game。(这里只截取需要的函数)
  • 后续
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值