【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大全(万字详解),不信你不收藏



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

相关推荐
军训猫猫头33 分钟前
20.抽卡只有金,带保底(WPF) C#
ui·c#·wpf
异次元的归来8 小时前
Unity DOTS中的share component
unity·游戏引擎
向宇it11 小时前
【从零开始入门unity游戏开发之——C#篇25】C#面向对象动态多态——virtual、override 和 base 关键字、抽象类和抽象方法
java·开发语言·unity·c#·游戏引擎
白乐天_n11 小时前
腾讯游戏安全移动赛题Tencent2016A
安全·游戏
这是我5811 小时前
C++打小怪游戏
c++·其他·游戏·visual studio·小怪·大型·怪物
_oP_i12 小时前
unity webgl部署到iis报错
unity
Go_Accepted12 小时前
Unity全局雾效
unity
向宇it12 小时前
【从零开始入门unity游戏开发之——C#篇24】C#面向对象继承——万物之父(object)、装箱和拆箱、sealed 密封类
java·开发语言·unity·c#·游戏引擎
tealcwu12 小时前
【游戏设计原理】21 - 解谜游戏的设计
游戏·游戏策划
清梦202013 小时前
经典问题---跳跃游戏II(贪心算法)
算法·游戏·贪心算法