目录
AudioManager
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AudioManager : MonoBehaviour
{
public static AudioManager instance { get; private set; }
private AudioSource audioS;
// Start is called before the first frame update
void Start()
{
instance = this;
audioS = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
}
public void AudioPlay(AudioClip clip)
{
audioS.PlayOneShot(clip);
}
}
BullectControl
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Pathfinding;
public class BulletControl : MonoBehaviour
{
Rigidbody2D rbody;
//子弹命中声
public AudioClip hitClip;
// Start is called before the first frame update
void Awake()
{
rbody = GetComponent<Rigidbody2D>();
//2秒后不管打没打中,子弹销毁子弹
Destroy(this.gameObject, 2f);
}
// Update is called once per frame
void Update()
{
}
//子弹发射,射击
public void Shoot(Vector2 moveDirection,float moveForce)
{
rbody.AddForce(moveDirection * moveForce);
}
//子弹命中
private void OnCollisionEnter2D(Collision2D collision)
{
//调用机器人控制脚本的修复函数,修改机器人的状态
RobotControl ec = collision.gameObject.GetComponent<RobotControl>();
if (ec!=null)
{
ec.Fixed();//杀敌
if (RobotControl.isFixed%2==0)
{
collision.gameObject.GetComponent<AIPath>().enabled = false;
PlayerControl.enemyleft--;//更改玩家控制中的击杀数,以此判断任务是否完成,格式:脚本名.静态变量--;
Debug.Log("当前敌人数:" + PlayerControl.enemyleft);
}
if (RobotControl.isFixed%2 != 0)
{
//打开命中对象的AI组件
collision.gameObject.GetComponent<AIPath>().enabled = true;
//collision.gameObject.GetComponent<RobotControl>().enabled = false;
//RobotControl.AIon();
//ScriptSelect.changestatus();
}
Debug.Log("命中敌人了");
}
//播放命中声
AudioManager.instance.AudioPlay(hitClip);
//立即销毁子弹
Destroy(this.gameObject);
}
}
BulletGet
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletGet : MonoBehaviour
{
public int bulletCount=10;
public ParticleSystem collectEffect;
private void OnTriggerEnter2D(Collider2D collision)
{
PlayerControl pc = collision.GetComponent<PlayerControl>();
if(pc!=null)
{
if(pc.MyCurBulletCount<pc.MyMaxBulletCount)
{
pc.ChangeBulletCount(bulletCount);
Instantiate(collectEffect, transform.position, Quaternion.identity);
Destroy(this.gameObject);
}
}
}
}
Collect
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Collect : MonoBehaviour
{
public ParticleSystem collectEffect;
public AudioClip collectClip;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter2D(Collider2D other)
{
PlayerControl pc = other.GetComponent<PlayerControl>();
if(pc!=null)
{
if(pc.MyCurrentHealth<pc.MyMaxHealth)
{
pc.chnageHealth(1);
Instantiate(collectEffect, transform.position, Quaternion.identity);
AudioManager.instance.AudioPlay(collectClip);
Destroy(this.gameObject);
}
Debug.Log("玩家碰到了草莓!");
}
}
}
Damage
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Damage : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerStay2D(Collider2D other)
{
PlayerControl pc = other.GetComponent<PlayerControl>();
if(pc!=null)
{
pc.chnageHealth(-1);
}
}
}
Follow
using UnityEngine;
using System.Collections;
public class Follow : MonoBehaviour
{
//目的地,射线与平面的交点
private Vector2 hitPoint;
//自动导航速度
public float speed = 3;
//是否点击
private bool clicked = false;
//定义动画
Animator anim;
//初始屏幕看
Vector2 lookDirection = new Vector2(0, -1);
// Use this for initialization
void Start()
{
//获取动画组件
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
//方向向量
Vector2 curposition = new Vector2(gameObject.transform.position.x, gameObject.transform.position.y);
Vector2 moveDirection = hitPoint - curposition;
if (moveDirection.x != 0 || moveDirection.y != 0)
{
lookDirection = moveDirection;
}
//按下鼠标左键
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit2D raycast = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
if (raycast.collider != null)
{
hitPoint = new Vector2(raycast.point.x, raycast.point.y);
Debug.Log(raycast.point.x+"/"+ raycast.point.y);
Debug.Log("clicked object name is ---->" + raycast.collider.gameObject);
}
clicked = true;
}
if (clicked) gameObject.transform.position = Vector2.MoveTowards(gameObject.transform.position, hitPoint, Time.deltaTime * speed);
//如果到达、则停止这次自动导航
if (gameObject.transform.position.x== hitPoint.x && gameObject.transform.position.y== hitPoint.y)
{
clicked = false;
//默认停止显示正面或反面
anim.SetFloat("move X", 0);
anim.SetFloat("move Y", lookDirection.y);
anim.SetFloat("runspeed", 0);
Debug.Log(lookDirection);
}
//如果在自动前往目的地时,按下了方向键,撤销这次自动导航
if(Input.GetAxisRaw("Horizontal")!=0||Input.GetAxisRaw("Vertical")!=0)
{
clicked = false;
}
//如果这次自动导航未被打断,并且未结束,为运动中的动画状态集赋值,结束后回到运动前的朝向
if (clicked == true && moveDirection.x != 0 && moveDirection.y != 0)
{
//为动画集赋值
//状态集的切换由运动矢量决定
anim.SetFloat("move X", lookDirection.x);
anim.SetFloat("move Y", lookDirection.y);
anim.SetFloat("runspeed", moveDirection.magnitude);
Debug.Log(lookDirection);
}
}
}
NextMission
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class NextMission : MonoBehaviour
{
Rigidbody2D rbody;
// Start is called before the first frame update
void Start()
{
rbody = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
}
public void OnCollisionEnter2D(Collision2D collision)
{
if (PlayerControl.showflag == 1)
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
}
}
NPCManager
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class NPCManager : MonoBehaviour
{
public GameObject dialogImage;
public GameObject tipImage;
public Text text;
public float showTime = 4;
private float showTimer;
// Start is called before the first frame update
void Start()
{
tipImage.SetActive(true);
dialogImage.SetActive(false);
showTimer = -1;
}
// Update is called once per frame
void Update()
{
showTimer -= Time.deltaTime;
if(showTimer<0)
{
tipImage.SetActive(true);
dialogImage.SetActive(false);
}
}
public void ShowDialog()
{
//调用PlayerControl脚本的标记,判断任务是否完成,完成则修改文本
if(PlayerControl.showflag==1)
{
text.text= "青蛙先生:\n任务已完成,你现在可以进入城堡了!";
}
showTimer = showTime;
dialogImage.SetActive(true);
tipImage.SetActive(false);
}
}
MissionShow
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MissionShow: MonoBehaviour
{
public GameObject MissionBar;
private float showTimer=3;
// Start is called before the first frame update
void Awake()
{
MissionBar.SetActive(true);
}
// Update is called once per frame
void Update()
{
showTimer -= Time.deltaTime;
if (showTimer < 0)
{
MissionBar.SetActive(false);
}
}
}
PlayerControl
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class PlayerControl : MonoBehaviour
{
//主角速度
public float speed = 8f;
//定义2d刚体
Rigidbody2D rbody;
//定义动画
Animator anim;
//初始屏幕看
Vector2 lookDirection = new Vector2(0, -1);
//定义一个机器人对象
GameObject robotObj;
//最大健康值
private int maxHealth = 2;//改为2;
//当前健康值
private int currentHealth;
//其他脚本可获取最大健康值
public int MyMaxHealth { get { return maxHealth; } }
//其他脚本可获取当前健康值
public int MyCurrentHealth { get { return currentHealth; } }
//无敌时间
private float wuditime = 2f;
//无敌计时器
private float wuditimer;
//无敌状态
private bool isWudi;
//子弹数量
[SerializeField]
private int curBulletCount=1;
private int maxBulletCount=6;
public float gametime=0;
public int MyCurBulletCount { get { return curBulletCount; } }
public int MyMaxBulletCount { get { return maxBulletCount; } }
//子弹预制体
public GameObject bulletPrefab;
//敌人剩余数量
public static int enemyleft;
//public int curEnemyleft { get { return enemyleft; } }
//游戏结束提示
public GameObject GameOver;
//指定为主角并销毁
public GameObject GameOverPlayer;
//任务完成提示
public GameObject MissionCompleted;
//无法实现在其他脚本中销毁提示,故在此脚本中限时销毁
public float showTime = 3;
private float showTimer;
//智能提示框计时器
public float tiptime = 6;
private float tiptimer;
//任务完成标记,显示标志,一场游戏中只显示一次
public static int showflag=0;
//玩家音效
public AudioClip hitClip;
public AudioClip launchClip;
//智能提示文本
public Text tips;
public GameObject tipsframe;
// Start is called before the first frame update
void Start()
{
//操作方式选择
ctrlway();
//任务完成状态的初始化
showflag = 0;
//隐藏任务完成提示
MissionCompleted.SetActive(false);
//提示计时器设置为负值
showTimer = -1;
tiptimer = -1;
//开始游戏后初始化敌人数量
SetEnemyNum();
//获取2d刚体
rbody = GetComponent<Rigidbody2D>();
//获取动画组件
anim = GetComponent<Animator>();
//初始化生命值
currentHealth = 1;
//初始化无敌时间
wuditimer = 0;
//更新生命条与子弹数量
UIManager.instance.UpdateHealthBar(currentHealth, maxHealth);
UIManager.instance.UpdateBulletCount(curBulletCount, maxBulletCount);
}
// Update is called once per frame
void Update()
{
if(showflag!=1)
{
UpdateTime();
}
UpdateEnemyLeft();
//任务完成显示 计时器
showTimer -= Time.deltaTime;
//提示框显示 计时器
tiptimer -= Time.deltaTime;
//超过时间就取消显示任务完成
if (showTimer < 0)
{
MissionCompleted.SetActive(false);
}
/*//超过时间就取消显示提示文本
if(tiptimer<0)
{
tipsframe.SetActive(false);
}*/
//取消显示提示文本
if(showTimer+5<0)
{
tipsframe.SetActive(false);
}
//敌人数量为0,并且是第一次达到0,之前没有完成任务,任务完成
if (enemyleft==0 && showflag != 1)
{
//给倒计时器赋值,以此显示提示
showTimer = showTime;
//已经完成任务了,标记一下,之后不再显示
showflag = 1;
//显示任务完成提示
MissionCompleted.SetActive(true);
//显示前往下一关的提示
tip();
}
//如果生命值为0,显示游戏结束字样,并销毁主角
if (currentHealth==0)
{
GameOver.SetActive(true);
tip();
GameObject.Find("Ruby").GetComponent<PlayerControl>().enabled = false;
//gameObject.SetActive(false);
//Destroy(GameOverPlayer);
}
//如果没有子弹了,则进行提示
if(curBulletCount==0 && currentHealth!=0)
{
tip();
}
//如果游戏超时,则进行提示
if (gametime > 180)
{
tip();
}
float moveX=0;
float moveY=0;
//如果没有禁用键盘
if (UIEntry.ctrlflag != 2)
{
//获取运动矢量
moveX = Input.GetAxisRaw("Horizontal");
moveY = Input.GetAxisRaw("Vertical");
}
Vector2 moveVector = new Vector2(moveX, moveY);
if(moveX!=0||moveY!=0)
{
lookDirection = moveVector;
}
Vector2 position = transform.position;
position.x += moveX * speed * Time.deltaTime;
position.y += moveY * speed * Time.deltaTime;
transform.position = new Vector2(position.x, position.y);
//用刚体移动可以消除画面抖动
rbody.MovePosition(position);
//为动画集赋值
//状态集的切换由运动矢量决定
anim.SetFloat("move X",lookDirection.x);
anim.SetFloat("move Y",lookDirection.y);
anim.SetFloat("runspeed", moveVector.magnitude);
//赋值必须与状态集命名一样
//如果处于无敌状态,就计时
if(isWudi)
{
wuditimer -= Time.deltaTime;
//计时器归0,就不处于无敌状态了
if(wuditimer<0)
{
isWudi = false;
}
}
//当按下J,并且子弹大于0,开火(没有禁用键盘)
if(Input.GetKeyDown(KeyCode.J) && curBulletCount>0 && UIEntry.ctrlflag != 2)
{
ChangeBulletCount(-1);
anim.SetTrigger("Launch");
AudioManager.instance.AudioPlay(launchClip);
GameObject bullet = Instantiate(bulletPrefab, rbody.position+Vector2.up*0.5f, Quaternion.identity);
BulletControl bc = bullet.GetComponent<BulletControl>();
if(bc!=null)
{
bc.Shoot(lookDirection, 300);
}
}
//按下鼠标左键,开枪(没有禁用鼠标)
if (Input.GetMouseButtonDown(1) && curBulletCount > 0 && UIEntry.ctrlflag != 3)
{
RaycastHit2D raycast = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
Vector2 hitPoint = new Vector2(raycast.point.x, raycast.point.y);
Vector2 curposition = new Vector2(gameObject.transform.position.x, gameObject.transform.position.y);
Vector2 ShootDirection = hitPoint - curposition;
ChangeBulletCount(-1);
anim.SetTrigger("Launch");
AudioManager.instance.AudioPlay(launchClip);
GameObject bullet = Instantiate(bulletPrefab, rbody.position + Vector2.up * 0.5f, Quaternion.identity);
BulletControl bc = bullet.GetComponent<BulletControl>();
if (bc != null)
{
bc.Shoot(ShootDirection, 300);//lookDirection
}
}
//按E与npc交互
if (Input.GetKeyDown(KeyCode.E))
{
RaycastHit2D hit = Physics2D.Raycast(rbody.position, lookDirection, 2f, LayerMask.GetMask("NPC"));
if(hit.collider!=null)
{
Debug.Log("hit npc!");
NPCManager npc = hit.collider.GetComponent<NPCManager>();
if(npc!=null)
{
npc.ShowDialog();
}
}
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
//踩到雷区,自动生成僵尸
float robotX = Random.Range(-3, 5);
float robotY = Random.Range(-3, 5);
Vector2 position_robot = new Vector2(robotX, robotY);
if(collision.gameObject.tag=="bomb")//如果碰到雷区
{
print("other.tag=" + collision.gameObject.tag);//这句一定要有,不然出不来
robotObj = Instantiate(Resources.Load("Prefabs/Robot"),position_robot,Quaternion.identity)as GameObject;//加载资源,生成一个预制体,做成一个对象
}
}
private void OnTriggerExit2D(Collider2D collision)
{
//离开雷区,僵尸消失
if(collision.gameObject.tag=="bomb")
{
Destroy(robotObj);
}
}
public void changeHealth(int amount)
{
//可能是受到伤害,也可能是加血
if(amount<0)
{
//如果是受伤,设置无敌状态,则2秒内不能受伤
if(isWudi==true)
{
return;
}
isWudi = true;
//播放受伤动画
anim.SetTrigger("Hit");
//播放受伤音效
AudioManager.instance.AudioPlay(hitClip);
//为无敌计时器赋值
wuditimer = wuditime;
}
//更改健康值
currentHealth = Mathf.Clamp(currentHealth+amount,0,maxHealth);
//更新血条
UIManager.instance.UpdateHealthBar(currentHealth, maxHealth);
//调试
Debug.Log(currentHealth + "/" + maxHealth);
}
public void ChangeBulletCount(int amount)
{
//改变子弹数
curBulletCount = Mathf.Clamp(curBulletCount + amount, 0, maxBulletCount);
//改变UI子弹数
UIManager.instance.UpdateBulletCount(curBulletCount, maxBulletCount);
}
public void UpdateTime()
{
//游戏时间
gametime += Time.deltaTime;
//Debug.Log((int)gametime);
//改变UI时间
UIManager.instance.UpdateTimeBar((int)gametime);
}
public void UpdateEnemyLeft()
{
UIManager.instance.UpdateEnemyLeft(enemyleft);
}
void SetEnemyNum()
{
int index = SceneManager.GetActiveScene().buildIndex;
if(index==1)
{
enemyleft = 2;
}
if(index==2)
{
enemyleft = 3;
}
}
public void ctrlway()
{
if(UIEntry.ctrlflag==1)
{
GameObject.Find("Ruby").GetComponent<PlayerControl>().enabled = true;
GameObject.Find("Ruby").GetComponent<Follow>().enabled = true;
}
else if(UIEntry.ctrlflag == 2)
{
//不能禁用这个脚本,改为在if语句中利用UIEntry.ctrlflag禁用操作
//GameObject.Find("Ruby").GetComponent<PlayerControl>().enabled = false;
GameObject.Find("Ruby").GetComponent<Follow>().enabled = true;
}
else if(UIEntry.ctrlflag == 3)
{
GameObject.Find("Ruby").GetComponent<PlayerControl>().enabled = true;
GameObject.Find("Ruby").GetComponent<Follow>().enabled = false;
}
}
public void tip()
{
//tiptimer = tiptime;
if(showflag==1)
{
int index = SceneManager.GetActiveScene().buildIndex;
if(index==1)
{
tipsframe.SetActive(true);
tips.text = "Tips:\n快前往城堡,进行下一关吧!";
}
else if(index==2)
{
tipsframe.SetActive(true);
tips.text = "Tips:\n亲亲、你已经通关了,请五星好评哦!";
}
}
else if(currentHealth==0)
{
tipsframe.SetActive(true);
int index = SceneManager.GetActiveScene().buildIndex;
if(index==1)
{
tips.text = "Tips:\n该长教训了吧,哈哈!";
}
else if(index==2 && RobotControl.isFixed%2==1)
{
tips.text = "Tips:\n机器人被激怒,会主动攻击人哦。尝试着对它连续完成两次攻击吧!";
}
else if (index == 2 && RobotControl.isFixed % 2 != 1)
{
tips.text = "Tips:\n你可长点教训吧,哈哈!";
}
else
{
return;
}
}
else if(curBulletCount==0 && currentHealth!=0)
{
tipsframe.SetActive(true);
tips.text = "Tips:\n没有子弹了,快去拾取子弹吧!";
}
else if(gametime>180)
{
tipsframe.SetActive(true);
tips.text = "Tips:\n游戏超时了哦";
}
}
}
RobotControl
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using Pathfinding;
using System.Runtime.CompilerServices;
public class RobotControl : MonoBehaviour
{
public float speed = 3f;//机器人要运动肯定要有一个速度
Animator anim;//创建一个动画状态集的变量,用来设定动画状态集的值,利用Animator参数,要决定到底播放左边的动画还是右边的动画
Rigidbody2D rbody;//为了防止角色抖动,用刚体的moveposition来确定位置
public bool isVertical;//设定一个参数,判断是水平移动还是垂直移动
Vector2 moveDirection;//设定一个运动方向的变量
float changeTimer;//设定一个计时器,每过一段时间,机器人换一个方向运动
public float changeDirectionTime = 2f;//计时器长度
public static int isFixed;
public ParticleSystem brokeneffect;
public AudioClip fixedClip;
GameObject collectObject;
//public static GameObject AIObject;
// Start is called before the first frame update
void Start()
{
anim = GetComponent<Animator>();//获取动画组件,一会儿要修改它的参数
rbody = GetComponent<Rigidbody2D>();//获取2d刚体,一会儿要修改它的位置
moveDirection = isVertical ? Vector2.up : Vector2.right;//初始状态下判断运动方向
changeTimer = changeDirectionTime;//计时器初始化
isFixed = 2;
}
// Update is called once per frame
void Update()
{
//if (isFixed==0) return;//如果被修复则不执行以下代码//这句导致第二个机器人不能运行
changeTimer -= Time.deltaTime;//计时器每一帧都减小
if(changeTimer<0)
{
moveDirection *= -1;//如果小于0,改变方向
changeTimer = changeDirectionTime;//改变方向后计时器重新初始化
}
Vector2 position = rbody.position;//定义一个二维向量位置坐标来获取刚体坐标
position.x += moveDirection.x * speed * Time.deltaTime;//改变水平方向位置
position.y += moveDirection.y * speed * Time.deltaTime;//改变垂直方向位置
rbody.MovePosition(position);//为了防止抖动,调用刚体的moveposition,里面是二维向量的位置
//设定动画状态集的参数,把moveDirection的值传递给move X/Y
anim.SetFloat("move X", moveDirection.x);
anim.SetFloat("move Y", moveDirection.y);
}
private void OnCollisionEnter2D(Collision2D collision)
{
PlayerControl pc = collision.gameObject.GetComponent<PlayerControl>();
if(pc!=null)
{
pc.changeHealth(-1);//在括号内打分号自动打在括号后
}
}
public void Fixed()
{
isFixed--;
Debug.Log("IsFixed:" + isFixed);
if (isFixed%2==0)
{
AIoff();
//ScriptSelect.closeAI();
if (brokeneffect.isPlaying == true)
{
brokeneffect.Stop();
}
AudioManager.instance.AudioPlay(fixedClip);
rbody.simulated = false;//禁用物理
anim.SetTrigger("fix");//播放被修复(打)的动画
//加载预制体资源,掉落子弹
RandomDrop();
}
}
//随机掉落子弹或者草莓
public void RandomDrop()
{
//草莓与子弹掉落概率为1:2。12~19生成草莓,20~31生成子弹,
int num = (int)(Random.Range(12,31)/10);
if(num%2==0)
{
collectObject = Instantiate(Resources.Load("Prefab/BulletGet"), gameObject.transform.position, Quaternion.identity) as GameObject;
}
if(num%2==1)
{
collectObject = Instantiate(Resources.Load("Prefab/CollectibleHealth"), gameObject.transform.position, Quaternion.identity) as GameObject;
}
}
public void AIoff()
{
gameObject.GetComponent<AIPath>().enabled = false;
gameObject.GetComponent<RobotControl>().enabled = true;
}
public static void AIon()//无法在此实现
{
//gameObject.GetComponent<AIPath>().enabled = true;
//gameObject.GetComponent<RobotControl>().enabled = false;
}
}
ScriptSelect
using Pathfinding;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScriptSelect: MonoBehaviour
{
float changeTimer;//设定一个计时器,每过一段时间,机器人换一个方向运动
public float changeTime = 10f;//计时器长度
private bool status=true;
// Start is called before the first frame update
void Start()
{
change();
}
// Update is called once per frame
void Update()
{
changeTimer -= Time.deltaTime;//计时器每一帧都减小
if (changeTimer < 0)
{
change();
changeTimer = changeTime;//改变方向后计时器重新初始化
}
}
public void change()
{
if(status==true)
{
GameObject.Find("Robot/Robot").GetComponent<AIPath>().enabled = true;
GameObject.Find("Robot/Robot").GetComponent<RobotControl>().enabled = false;
status = false;
return;
}
if (status == false)
{
GameObject.Find("Robot/Robot").GetComponent<AIPath>().enabled = false;
GameObject.Find("Robot/Robot").GetComponent<RobotControl>().enabled = true;
status = true;
return;
}
}
public static void changestatus()
{
GameObject.Find("Robot/Robot").GetComponent<AIPath>().enabled = true;
GameObject.Find("Robot/Robot").GetComponent<RobotControl>().enabled = false;
}
public static void closeAI()
{
GameObject.Find("Robot/Robot").GetComponent<AIPath>().enabled = false;
GameObject.Find("Robot/Robot").GetComponent<RobotControl>().enabled = true;
}
}
UIEntry
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class UIEntry : MonoBehaviour
{
public GameObject about;
private bool flag;
public static int ctrlflag=1;
public Text ctrltext;
// Start is called before the first frame update
void Start()
{
flag = false;
}
// Update is called once per frame
void Update()
{
}
public void StartGame()
{
SceneManager.LoadScene(1);
}
public void About()
{
if (flag == false)
{
about.SetActive(true);
flag = true;
return;
}
if (flag == true)
{
about.SetActive(false);
flag = false;
return;
}
}
public void ctrl()
{
Debug.Log(ctrlflag);
if(ctrlflag==1)
{
ctrlflag=2;
ctrltext.text = "鼠标";
ctrltext.fontSize = 49;
}
else if (ctrlflag == 2)
{
ctrlflag=3;
ctrltext.text = "键盘";
ctrltext.fontSize = 49;
}
else if (ctrlflag == 3)
{
ctrlflag=1;
ctrltext.text = "鼠标+键盘";
ctrltext.fontSize = 28;
}
}
}
UIManager
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class UIManager : MonoBehaviour
{
public static UIManager instance { get; private set; }
//静音设置
public GameObject music;
public Text musicstatus;
private bool musicflag=true;
//音量控制
public AudioSource volume;
public Scrollbar volumebar;
//帮助按钮
public GameObject help;
private bool helpflag;
//显示Bar:血量、子弹、时间、敌人
public Image healthBar;
public Text bulletCountText;
public Text TimeBar;
public Text EnemyLeftBar;
private void Awake()
{
instance = this;
}
//更新血条
public void UpdateHealthBar(int curAmount,int maxAmount)
{
healthBar.fillAmount = (float)curAmount / (float)maxAmount;
}
//更新子弹
public void UpdateBulletCount(int curAmount,int maxAmount)
{
bulletCountText.text = curAmount.ToString()+"/"+maxAmount.ToString();
}
//刷新时间
public void UpdateTimeBar(int curtime)
{
int min = curtime / 60;
int sec = curtime % 60;
if (sec < 10 && min<10)
{
TimeBar.text = "0"+min.ToString() + ":0" + sec.ToString();
}
if (sec > 10 && min < 10)
{
TimeBar.text = "0" + min.ToString() + ":" + sec.ToString();
}
}
//更新敌人数
public void UpdateEnemyLeft(int enemyleft)
{
EnemyLeftBar.text = enemyleft.ToString();
}
//退出游戏
public void ExitGame()
{
Application.Quit();
}
//静音
public void MusicButton()
{
if (musicflag == false)
{
volume.volume = 1;
//music.SetActive(true);
musicflag = true;
musicstatus.text = "音乐:开";
return;
}
if (musicflag == true)
{
volume.volume = 0;
//music.SetActive(false);
musicflag = false;
musicstatus.text = "音乐:关";
return;
}
}
//重新开始游戏
public void ReStart()
{
int index = SceneManager.GetActiveScene().buildIndex;
SceneManager.LoadScene(index);
}
//返回菜单
public void Menu()
{
SceneManager.LoadScene(0);
}
//调节音量
public void AdjustVolume(Scrollbar volumebar)
{
volume.volume = volumebar.value;
}
//弹出帮助
public void helpButton()
{
if (helpflag == false)
{
help.SetActive(true);
helpflag = true;
return;
}
if (helpflag == true)
{
help.SetActive(false);
helpflag = false;
return;
}
}
}
原博地址
https://blog.csdn.net/weixin_43673589