Per aspera ad astra.循此苦|

flyall

园龄:4年2个月粉丝:10关注:8

2023-03-04 23:15阅读: 298评论: 0推荐: 0

unity3D制作统计得分UI10

创建UItext画布

image

选择2D方便观察

image

设置字体位置和字体大小 字体颜色等

image
image

为UI建立脚本

image

获取对象

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//2.获得UI
using UnityEngine.UI;

public class UIManager : MonoBehaviour
{
    //1.获得UItext
    public Text shootNumText;
    public Text scoreText;

}

赋值

image

方法

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//2.获得UI
using UnityEngine.UI;

public class UIManager : MonoBehaviour
{
    //6.调用 
    public static UIManager _instance;
    //1.获得UItext
    public Text shootNumText;
    public Text scoreText;

    //4.初始化
    public int shootNum = 0;
    public int score = 0;

//6.调用
private void Awake()
{
    _instance = this;
}

//5.在update中实时更新数据
private void Update()
    {
        shootNumText.text = shootNum.ToString();
        scoreText.text = score.ToString();
    }
    //3.方法
    public void AddShootNum()
    {
        shootNum += 1;
    }
    public void AddScore()
    {
        score += 1;
    }
}

调用方法

image

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

public class MonsterManager : MonoBehaviour
{
    //3.定义动画
    private Animation anim;
    //5.定义两个动画状态
    public AnimationClip idleClip;
    public AnimationClip dieClip;
    private void Awake()
    {
        //4.获得动画组件
        anim = gameObject.GetComponent<Animation>();
        //6.赋值动画
        anim.clip = idleClip;

    }

    //1.检测是否被子弹打中
    private void OnCollisionEnter(Collision collision)
    {
        if(collision.collider.tag == "Bullet")
        {
            //2.销毁子弹
            Destroy(collision.collider.gameObject);
            //7.切换动画
            anim.clip = dieClip;
            anim.Play();

            //10.关闭组件
            gameObject.GetComponent<BoxCollider>().enabled = false;
            StartCoroutine("Deactivate");

            //11.调用UIManager
            UIManager._instance.AddScore(); 
        }
    }
    //8.当怪物disable的时候,将默认的动画修改为idle动画
    private void OnDisable()
    {
        anim.clip = idleClip;
    }

    //9.迭代器
    IEnumerator Deactivate()
    {
        //两次射击间隔为1s 所以这里时间小于1s
        yield return new WaitForSeconds(0.8f);
        //使当前的怪物变为未激活状态,并使整个循环重新开始。TargetManager.cs
        TargetManager._instance.UpdateMonsters();
    }
}

image

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

public class GunManager : MonoBehaviour
{
    //1.枪旋转角度 xy轴轩轩角度
    private float maxYRotation = 120;
    private float minYRotation = 0;
    private float maxXRotation = 60;
    private float minXRotation = 0;

    //2.枪射击时间 射击间隔时长
    private float shootTime = 1;
    private float shootTimer = 0;

    //7.得到子弹物体和位置信息
    public GameObject bulletGO;
    public Transform firePosition;

    //3.更新
    private void Update()
    {
        shootTimer += Time.deltaTime;
        if (shootTimer > shootTime)
        {
            //8.点击鼠标左键进行射击
            if (Input.GetMouseButtonDown(0))
            {
                //9.实例化子弹
                GameObject bulletCurrent = GameObject.Instantiate(bulletGO, firePosition.position, Quaternion.identity);
                //11.获得子弹后 通过刚体组件给子弹添加一个正前方向上的力,以达到让子弹向前运动的效果
                bulletCurrent.GetComponent<Rigidbody>().AddForce(transform.forward * 2400);

                //12.获得组件后做操作 播放手枪开火动画
                gameObject.GetComponent<Animation>().Play();
                //10.计时器归零 每1秒生成一次
                shootTimer = 0;

                //13.调用UIManager
                UIManager._instance.AddShootNum();
            }
        }

        //4.旋转 获取鼠标在屏幕上的位置坐标 去旋转手枪
        float xPosPrecent = Input.mousePosition.x / Screen.width;
        float yPosPrecent = Input.mousePosition.y / Screen.height;

        //5.规定旋转角度最大最小值范围
        float xAngle = -Mathf.Clamp(yPosPrecent * maxXRotation, minXRotation, maxXRotation) + 15;
        float yAngle = Mathf.Clamp(xPosPrecent * maxYRotation, minYRotation, maxYRotation) - 60;

        //6.赋值给枪的组件值
        transform.eulerAngles = new Vector3(xAngle, yAngle, 0);
    }

}

本文作者:flyall

本文链接:https://www.cnblogs.com/flyall/p/17179479.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   flyall  阅读(298)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
历史上的今天:
2022-03-04 SVG和Canvas的优缺点和共性
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 404 not found REOL
404 not found - REOL
00:00 / 00:00
An audio error has occurred.