个人主页:@元宇宙-秩沅
hallo 欢迎 点赞 收藏⭐ 留言 加关注✅!
本文由 秩沅 原创
收录于专栏:unityUI专题篇
️
步骤一创建游戏面板,进行组件管控
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//—————————————————————————————————————
//___________脚本名: 游戏面板类
//___________功能: 管理游戏中UI控件和面板数据更新
//___________创建者:秩沅_______________
//_____________________________________
//-------------------------------------
public class GamePlane : BeginFather<GamePlane>
{
public Label laScore; //分数
public Label laTime; //时间
public Button buQuit; //退出
public Button buSet; //设置
public TextureIMGUI bloodRoll;//血条
//----记录最终的分数和时间,为公有方便外部调用-----
[HideInInspector]
public int nowScore;
[HideInInspector]
public float nowTime;
private int time; //做时间格式转化的中介
private void Start()
{
//退出事件
buQuit.triggerEvent += () => { InformPlane.SingleInstance.Show(); };
//设置事件
buSet .triggerEvent += () => { SetPlane.SingleInstance.Show(); };
}
private void Update()
{
UpdateTime();
}
//更新时间
private void UpdateTime()
{
nowTime = nowTime + Time.deltaTime; //帧间隔更新时间更准确
time = (int)nowTime;
//时间格式转化
if (time / 3600 > 0)
{
laTime.ContorlContent.text = $"{time / 3600}小时{time / 60 % 60}分钟{time % 60}秒";
}
else if (time / 3600 == 0 && time / 60 % 60 > 0)
{
laTime.ContorlContent.text = $"{time / 60 % 60}分钟{time % 60}秒";
}
else if (time / 3600 == 0 && time / 60 % 60 == 0)
{
laTime.ContorlContent.text = $"{time % 60}秒";
}
}
//外部更新分数
public void AddScore( int defitScore)
{
nowScore += defitScore;
laScore.ContorlContent.text = nowScore .ToString();
}
//外部更新血条
public void UpdataBlood(int max , int now)
{
bloodRoll.ContorlPosition.width = 330*(float)(now / max); //根据百分比来缩放血条大小
}
}
2.创建提示面板进行退出确定
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
//-------------------------------------
//—————————————————————————————————————
//___________项目: ______________
//___________功能: 提示面板,确认退出
//___________创建者:秩沅_______________
//_____________________________________
//-------------------------------------
public class InformPlane : BeginFather<InformPlane>
{
public Button yes; //确定按钮
public Button No; //取消按钮
private void Start()
{
yes.triggerEvent += () => SceneManager.LoadScene("BeginScreen"); //退出游戏
No.triggerEvent += () => Hidden();
Hidden();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//—————————————————————————————————————
//___________项目:
//___________功能: 坦克基类——集中子类相同点
//___________创建者:______秩沅______
//___________________________________
//-------------------------------------
public abstract class TankFather : MonoBehaviour
{
//攻击和防御相关
public int attack;
public int defence;
public int nowBlood;
public int maxBlood;
//移动和转速相关
public int moveSpeed;
public int RotateSpeed;
public int HeadSpeed;
//击败特效
public GameObject DefitInstance;
//受伤行为
public virtual void Heart(TankFather other)
{
//当攻击力大于防御力时才生效
if (other.attack - defence > 0)
{
nowBlood -= (other.attack - defence);
}
if(nowBlood <= 0 )
{
nowBlood = 0;
Death();
}
//更新血条
GamePlane.SingleInstance.UpdataBlood(maxBlood ,nowBlood );
}
//死亡行为
public virtual void Death()
{
Destroy(this);
//将特效实例化相关逻辑
GameObject effect = Instantiate(DefitInstance,this.transform.position ,this.transform.rotation);
AudioSource soudClip = GetComponent<AudioSource>();
soudClip.enabled = MusicContol.SingleMusicContol.nowMusicData.soundSwitch;
soudClip.volume = MusicContol.SingleMusicContol.nowMusicData.soundRoll ;
soudClip.mute = false;
soudClip.playOnAwake = true;
}
//开火行为
public abstract void Fire(); //子类中每一个的开火方式都不同,作为抽象方法
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//—————————————————————————————————————
//___________项目:
//___________功能: 坦克的移动和旋转
//___________创建者:_______秩沅_______
//_____________________________________
//-------------------------------------
public class TankSport : TankFather
{
private void Start()
{
maxBlood = 100;
nowBlood = 100;
attack = 30;
defence = 10;
moveSpeed = 5;
RotateSpeed = 50;
HeadSpeed = 500;
}
private void Update()
{
//坦克的移动 = 大小*方向
transform.Translate(Input.GetAxis("Vertical") *Vector3.forward*moveSpeed *Time.deltaTime );
//坦克的旋转 = 大小*轴向
transform.Rotate(Input.GetAxis("Horizontal") *Vector3.up *RotateSpeed *Time .deltaTime );
//头部炮管的旋转 = 大小*轴向
Head.transform.Rotate(Input.GetAxis ("Mouse X") *Vector3.up*HeadSpeed*Time .deltaTime );
//左键发射炮弹
if(Input.GetMouseButtonDown(0))
{
Fire();
}
}
public override void Fire()
{
throw new System.NotImplementedException();
}
}
⭐相关文章⭐
⭐【2023unity游戏制作-mango的冒险】-6.关卡设计
⭐【2023unity游戏制作-mango的冒险】-5.攻击系统的简单实现
⭐【2023unity游戏制作-mango的冒险】-4.场景二的镜头和法球特效跟随
⭐【2023unity游戏制作-mango的冒险】-3.基础动作和动画API实现
⭐【2023unity游戏制作-mango的冒险】-2.始画面API制作
⭐【2023unity游戏制作-mango的冒险】-1.场景搭建
⭐“狂飙”游戏制作—游戏分类图鉴(网易游学)
⭐本站最全-unity常用API大全(万字详解),不信你不收藏
你们的点赞 收藏⭐ 留言 关注✅是我持续创作,输出优质内容的最大动力!