Unity-角色互动加分显示的实现

角色互动加分显示的实现

思路:在角色的脚本里对应该加分的地方用变量统计分数,同时再创建一个ui对象,然后用委派事件获取角色的得分。

在角色的脚本里设置分数

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3LrnKCuu-1680091056097)(C:/Users/86188/AppData/Roaming/Typora/typora-user-images/image-20230329175238050.png)]

   //基础分数
   public  int stepPoint;
   //总共得到了多少分数
   private int pointResult;
   //角色互动加分的函数
    public void Jump(InputAction.CallbackContext context)
   {
      //如果角色短跳触发成功,加分
      if (dir == Direction.Up && context.performed && !isJump)
      {
         pointResult += stepPoint;
      }
   }
   

布置页面以及编译对应的脚本

页面如下:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HXPEmiQv-1680091056098)(C:/Users/86188/AppData/Roaming/Typora/typora-user-images/image-20230329181422222.png)]

创建个新的脚本挂载到Canvas Text里:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UiManager : MonoBehaviour
{
    public Text screenText;

    void Start()
    {
        screenText.text = "0";
    }
}

把之前创建的文字丢进去。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nB8OSokL-1680091056099)(C:/Users/86188/AppData/Roaming/Typora/typora-user-images/image-20230329190606491.png)]

显示数字的控制也完成了!

使用事件委托将两者连接

创建委托脚本:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NT3VuvZC-1680091056100)(C:/Users/86188/AppData/Roaming/Typora/typora-user-images/image-20230329190925045.png)]

脚本如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class EventHander : MonoBehaviour
{
    //委托的事件
    public static event Action<int> GetPointEvent;
    //触发委托事件的函数
    public static void CallGetPointEvent (int point) 
    {   
        //如果委托有接收方就提醒他委托开始,并且把值给他
        GetPointEvent ? .Invoke(point);
    }
}

青蛙(玩家控制)需要建立委托:

   //基础分数
   public  int stepPoint;
   //总共得到了多少分数
   private int pointResult;
   //角色互动加分的函数
    public void Jump(InputAction.CallbackContext context)
   {
      //如果角色短跳触发成功,加分
      if (dir == Direction.Up && context.performed && !isJump)
      {
         pointResult += stepPoint;
         EventHander.CallGetPointEvent(pointResult);  //发起委托
      }
   }

界面部分接收委托:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UiManager : MonoBehaviour
{
    public Text scoreText;

    void Start()
    {
        screenText.text = "0";
    }
    //脚本刚被调用时使用
    private void OnEnable() 
    {
        //注册
        EventHander.GetPointEvent += OnGetPointEvent;
    }
     
    //脚本不再被使用
    private void OnDisable() 
    {

    }
    
    ///<summary>
    ///接收委托传来的值
    ///</summary>
    private void OnGetPointEvent(int point)
    {
        //把传过来的值显示在屏幕
        scoreText.text = point.ToString();
    }
}

完成!

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值