【unity之IMGUI实践】游戏结束流程封装实现【七】


👨‍💻个人主页@元宇宙-秩沅

👨‍💻 hallo 欢迎 点赞👍 收藏⭐ 留言📝 加关注✅!

👨‍💻 本文由 秩沅 原创

👨‍💻 收录于专栏unityUI专题篇


⭐⭐


文章目录


🎶前言


🅰️


🎶(A)关键逻辑梳理



🎶(B)需求分析




🎶(C)行为实现------血条



😶‍🌫️:步骤实现

  • 1.坐标三个转化
  • 2.GUI的原点和屏幕的原点
  • 3.结构体的特点回顾------涉及Rect_结构体类型
  • 4.血条的显隐
  • 5.倒计时的两个方法
  • 6.近大远小血条的缩放------待


血条核心代码

cpp 复制代码
 //GUI生命函数
    private void OnGUI()
    {
        if(showTime >=0) //血条倒计时展示
        {
            //简单的倒计时
            showTime -= Time.deltaTime;
            //世界坐标转屏幕坐标
            SreecPosition = Camera.main.WorldToScreenPoint(transform.position);

            //屏幕坐标转GUI坐标_____GUI的原点在左上角,屏幕坐标原点在左下角
            SreecPosition.y = Screen.height - SreecPosition.y;

            //底图Rect位置参数赋值
            GUIPosition1.x = SreecPosition.x - 50;
            GUIPosition1.y = SreecPosition.y - 70;
            GUIPosition1.width = 100;
            GUIPosition1.height = 15;

            //血条图Rect位置参数赋值
            GUIPosition2.x = SreecPosition.x - 50;
            GUIPosition2.y = SreecPosition.y - 70;

            //血条长度和血量同步
            GUIPosition2.width = 100*(nowBlood/maxBlood);
            GUIPosition2.height = 15;

            //绘制底图    
            GUI.DrawTexture(GUIPosition1, underTexture);
           
            //绘制血条       
            GUI.DrawTexture(GUIPosition2, topTexture);

        }
       
    }

更新敌方坦克

______-

cpp 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//---------------------------------------------------------------------------------------------------------------
//___________项目:      
//___________功能:敌方坦克逻辑封装
//___________创建者:秩沅_______________
//_____________________________________
//-------------------------------------
public class EnemyTank:TankFather 
{
    //随机移动点的位置数组
    public  Transform[] randomPosition;
    //目标位置
    private Transform target;
    //目标坦克
    public Transform Player;
    //检测范围
    public float distance = 10f;
    //子弹发射参数
    public AudioSource shootMusic;   //发射音效
    public GameObject shootball;     //发射的子弹类型
    public float shootSpeed = 1000f; //子弹的速度 
    public Transform[] shootTransform; //发射的组件信息
    public bool shootSwitch = false;  
    private float endTime = 3f;      //子弹发射间隔的时间
    //血条的参数
    public Texture underTexture;
    public Texture topTexture;
    private Vector3 SreecPosition;
    private Rect GUIPosition1;
    private Rect GUIPosition2;
    public float showTime = 0;

    private void Start()
    {
        //属性初始化赋值
        
        maxBlood = 500;
        nowBlood = 500;
        attack = 30;
        HeadSpeed = 50;
        //射击音效关闭
        shootMusic.enabled = false;
        moveSpeed = 10f;
        //先随机整一个目标点
        RandomPosition();

    }
    private void Update()
    {    
        transform.LookAt(target);

        //始终超自己的正方向移动
        transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);  //当距离差不多相等时,再随机目标点
        if(Vector3.Distance(transform .position ,target .position)< 0.5f)
        {
            RandomPosition();
        }
            
        //范围检测
        if (Vector3 .Distance(transform .position ,Player.position )<= distance && Player !=null )
        {
            //炮口瞄准玩家
            Head.transform.LookAt(Player);
            //倒计时发射子弹
            endTime = Mathf.MoveTowards(endTime, 0, 0.1f);
            if (endTime <= 0)
            {
                Fire();
                endTime = 3f;
            }        
        }
     
    }

    //随机指向一个移动点
    public void RandomPosition()
    {
        if (randomPosition.Length != 0)

            target = randomPosition[Random.Range(0, randomPosition.Length)];
    }
    //触发检测
    private void OnTriggerEnter(Collider other)
    {
       
        if (other.gameObject.tag == "bullet" )
        {
            //添加子弹挂载的目标坦克,方便获取该子弹的攻击力,与受伤逻辑相结合
            BulletMove ball = other.gameObject.GetComponent<BulletMove>();
            TankFather ballTank = ball.Tank.GetComponent<TankFather>();

            //当子弹不是自己打的到自己身上的时候
            if (ballTank.tag != gameObject.tag)
            {
                //扣血
                Heart(ballTank);
            }

        }
    }

    public override void Fire()
    {
        //开启射击音效
        shootMusic.enabled = true;
        shootMusic.Play();
        for (int i = 0; i < shootTransform.Length ; i++)
        {
            GameObject ball = Instantiate(shootball, shootTransform[i].position, shootTransform[i].rotation);
            BulletMove movScript = ball.GetComponent<BulletMove>();
            Rigidbody shootBall = ball.GetComponent<Rigidbody>();
            shootBall.AddForce(shootTransform[i].transform.forward * shootSpeed);
            movScript.Tank = gameObject;  //声明子弹是由谁打出去的
        }
     
    }
    
    //死亡行为重写
    public override void Death()
    {
        base.Death();
        GamePlane.SingleInstance.AddScore(10);
    }

    //受伤行为重写
    public override void Heart(TankFather other)
    {
        base.Heart(other);
        showTime = 3; //血条倒计时展示
    }

    //GUI生命函数
    private void OnGUI()
    {
        if(showTime >=0) //血条倒计时展示
        {
            //简单的倒计时
            showTime -= Time.deltaTime;
            //世界坐标转屏幕坐标
            SreecPosition = Camera.main.WorldToScreenPoint(transform.position);

            //屏幕坐标转GUI坐标_____GUI的原点在左上角,屏幕坐标原点在左下角
            SreecPosition.y = Screen.height - SreecPosition.y;

            //底图Rect位置参数赋值
            GUIPosition1.x = SreecPosition.x - 50;
            GUIPosition1.y = SreecPosition.y - 70;
            GUIPosition1.width = 100;
            GUIPosition1.height = 15;

            //血条图Rect位置参数赋值
            GUIPosition2.x = SreecPosition.x - 50;
            GUIPosition2.y = SreecPosition.y - 70;

            //血条长度和血量同步
            GUIPosition2.width = 100*(nowBlood/maxBlood);
            GUIPosition2.height = 15;

            //绘制底图    
            GUI.DrawTexture(GUIPosition1, underTexture);
           
            //绘制血条       
            GUI.DrawTexture(GUIPosition2, topTexture);

        }
       
    }
}

更新炮台

cpp 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//---------------------------------------------------------------------------------------------------------------
//___________项目:      
//___________功能:敌方坦克逻辑封装
//___________创建者:秩沅_______________
//_____________________________________
//-------------------------------------
public class EnemyTank:TankFather 
{
    //随机移动点的位置数组
    public  Transform[] randomPosition;
    //目标位置
    private Transform target;
    //目标坦克
    public Transform Player;
    //检测范围
    public float distance = 10f;
    //子弹发射参数
    public AudioSource shootMusic;   //发射音效
    public GameObject shootball;     //发射的子弹类型
    public float shootSpeed = 1000f; //子弹的速度 
    public Transform[] shootTransform; //发射的组件信息
    public bool shootSwitch = false;  
    private float endTime = 3f;      //子弹发射间隔的时间
    //血条的参数
    public Texture underTexture;
    public Texture topTexture;
    private Vector3 SreecPosition;
    private Rect GUIPosition1;
    private Rect GUIPosition2;
    public float showTime = 0;

    private void Start()
    {
        //属性初始化赋值
        
        maxBlood = 500;
        nowBlood = 500;
        attack = 30;
        HeadSpeed = 50;
        //射击音效关闭
        shootMusic.enabled = false;
        moveSpeed = 10f;
        //先随机整一个目标点
        RandomPosition();

    }
    private void Update()
    {    
        transform.LookAt(target);

        //始终超自己的正方向移动
        transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);  //当距离差不多相等时,再随机目标点
        if(Vector3.Distance(transform .position ,target .position)< 0.5f)
        {
            RandomPosition();
        }
            
        //范围检测
        if (Vector3 .Distance(transform .position ,Player.position )<= distance && Player !=null )
        {
            //炮口瞄准玩家
            Head.transform.LookAt(Player);
            //倒计时发射子弹
            endTime = Mathf.MoveTowards(endTime, 0, 0.1f);
            if (endTime <= 0)
            {
                Fire();
                endTime = 3f;
            }        
        }
     
    }

    //随机指向一个移动点
    public void RandomPosition()
    {
        if (randomPosition.Length != 0)

            target = randomPosition[Random.Range(0, randomPosition.Length)];
    }
    //触发检测
    private void OnTriggerEnter(Collider other)
    {
       
        if (other.gameObject.tag == "bullet" )
        {
            //添加子弹挂载的目标坦克,方便获取该子弹的攻击力,与受伤逻辑相结合
            BulletMove ball = other.gameObject.GetComponent<BulletMove>();
            TankFather ballTank = ball.Tank.GetComponent<TankFather>();

            //当子弹不是自己打的到自己身上的时候
            if (ballTank.tag != gameObject.tag)
            {
                //扣血
                Heart(ballTank);
            }

        }
    }

    public override void Fire()
    {
        //开启射击音效
        shootMusic.enabled = true;
        shootMusic.Play();
        for (int i = 0; i < shootTransform.Length ; i++)
        {
            GameObject ball = Instantiate(shootball, shootTransform[i].position, shootTransform[i].rotation);
            BulletMove movScript = ball.GetComponent<BulletMove>();
            Rigidbody shootBall = ball.GetComponent<Rigidbody>();
            shootBall.AddForce(shootTransform[i].transform.forward * shootSpeed);
            movScript.Tank = gameObject;  //声明子弹是由谁打出去的
        }
     
    }
    
    //死亡行为重写
    public override void Death()
    {
        base.Death();
        GamePlane.SingleInstance.AddScore(10);
    }

    //受伤行为重写
    public override void Heart(TankFather other)
    {
        base.Heart(other);
        showTime = 3; //血条倒计时展示
    }

    //GUI生命函数
    private void OnGUI()
    {
        if(showTime >=0) //血条倒计时展示
        {
            //简单的倒计时
            showTime -= Time.deltaTime;
            //世界坐标转屏幕坐标
            SreecPosition = Camera.main.WorldToScreenPoint(transform.position);

            //屏幕坐标转GUI坐标_____GUI的原点在左上角,屏幕坐标原点在左下角
            SreecPosition.y = Screen.height - SreecPosition.y;

            //底图Rect位置参数赋值
            GUIPosition1.x = SreecPosition.x - 50;
            GUIPosition1.y = SreecPosition.y - 70;
            GUIPosition1.width = 100;
            GUIPosition1.height = 15;

            //血条图Rect位置参数赋值
            GUIPosition2.x = SreecPosition.x - 50;
            GUIPosition2.y = SreecPosition.y - 70;

            //血条长度和血量同步
            GUIPosition2.width = 100*(nowBlood/maxBlood);
            GUIPosition2.height = 15;

            //绘制底图    
            GUI.DrawTexture(GUIPosition1, underTexture);
           
            //绘制血条       
            GUI.DrawTexture(GUIPosition2, topTexture);

        }
       
    }
}

🎶(D)场景搭建------敌方阵营



🎶(E)场景搭建------通关面板


_____________

😶‍🌫️:步骤实现:

  • 1.面板制作
  • 2.数据显示和存储
  • 3.循环开始

cpp 复制代码
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
//-------------------------------------
//---------------------------------------------------------------------------------------------------------------
//___________项目:      
//___________功能:通关面板逻辑相关
//___________创建者:秩沅_______________
//_____________________________________
//-------------------------------------
public class VictorPlane : BeginFather<VictorPlane>
{
    public Button Require;  //确认按钮
    public InputFram name;  //输入的昵称
    public Label Score;     //显示的分数
    public Label LTime;      //显示的时间
    private int time;

 
    private void Start()
    {
        //添加按钮事件
        Require.triggerEvent += () => {

            //点击按钮后跳到主场景
            SceneManager.LoadScene("BeginScreen");
            //将数据存储
            SaveGameData.DataContorl.SaveRankData(name.ContorlContent.text, Convert.ToInt32(Score.ContorlContent.text), time);
        };
        //默认隐藏
        Hidden();
    }
    private void Update()
    {
        //将光标显示出来
        Cursor.visible = true;
        //分数显示
        Score.ContorlContent.text = GamePlane.SingleInstance.laScore.ContorlContent.text;
        //时间显示
        LTime.ContorlContent.text = GamePlane.SingleInstance.laTime.ContorlContent.text;
        //时间
        time = GamePlane.SingleInstance.time;
    }

}

🎶(F)行为实现------通关点触发


cpp 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//---------------------------------------------------------------------------------------------------------------
//___________项目:      
//___________功能:  通关触发
//___________创建者:秩沅_______________
//_____________________________________
//-------------------------------------
public class over : MonoBehaviour
{
    private void OnTriggerEnter(Collider other)
    {
        if(other.CompareTag ("Player") )
        {
            Time.timeScale = 0;
           VictorPlane.SingleInstance.Show();
            
        }
    }
}

🎶(G)场景搭建------结束面板


cpp 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
//-------------------------------------
//---------------------------------------------------------------------------------------------------------------
//___________项目:       ______________
//___________功能: 死亡显示面板
//___________创建者:秩沅_______________
//_____________________________________
//-------------------------------------
public class DiedPlane : BeginFather<DiedPlane >
{
    public Button back;
    private void Start()
    {
        back.triggerEvent += () =>
        {
            //点击按钮后跳到主场景
            SceneManager.LoadScene("BeginScreen");
        };
        Hidden();       
    }
}

相关文章


【2023unity游戏制作-mango的冒险】-6.关卡设计

【2023unity游戏制作-mango的冒险】-5.攻击系统的简单实现

【2023unity游戏制作-mango的冒险】-4.场景二的镜头和法球特效跟随

【2023unity游戏制作-mango的冒险】-3.基础动作和动画API实现

【2023unity游戏制作-mango的冒险】-2.始画面API制作

【2023unity游戏制作-mango的冒险】-1.场景搭建

"狂飙"游戏制作---游戏分类图鉴(网易游学)

本站最全-unity常用API大全(万字详解),不信你不收藏



你们的点赞👍 收藏⭐ 留言📝 关注✅是我持续创作,输出优质内容的最大动力!

相关推荐
半盏茶香1 小时前
【C语言】分支和循环详解(下)猜数字游戏
c语言·开发语言·c++·算法·游戏
△曉風殘月〆5 小时前
WPF MVVM入门系列教程(二、依赖属性)
c#·wpf·mvvm
逐·風6 小时前
unity关于自定义渲染、内存管理、性能调优、复杂物理模拟、并行计算以及插件开发
前端·unity·c#
_oP_i8 小时前
Unity Addressables 系统处理 WebGL 打包本地资源的一种高效方式
unity·游戏引擎·webgl
PandaQue9 小时前
《怪物猎人:荒野》游戏可以键鼠直连吗
游戏
m0_6569747410 小时前
C#中的集合类及其使用
开发语言·c#
九鼎科技-Leo10 小时前
了解 .NET 运行时与 .NET 框架:基础概念与相互关系
windows·c#·.net
代码盗圣12 小时前
GODOT 4 不用scons编译cpp扩展的方法
游戏引擎·godot
九鼎科技-Leo12 小时前
什么是 ASP.NET Core?与 ASP.NET MVC 有什么区别?
windows·后端·c#·asp.net·mvc·.net
.net开发12 小时前
WPF怎么通过RestSharp向后端发请求
前端·c#·.net·wpf