一.角色相关
1.使用光线追踪进行碰撞检测
RaycastHit2D hit = Physics2D.Raycast(射线发出位置, 射线发出方向,射线发射距离, LayerMask.GetMask(“要销毁的游戏物体层级”)); //发出射线
if (hit.collider != null){
GameObject target = hit.collider.gameObject; }//获取碰撞到的游戏物体
二.UI相关
1.游戏时间流速减缓与恢复
//时间流速变慢
Time.timeScale -= Time.deltaTime;
//当时间流速慢到小于目标值将其固定为目标值
if (Time.timeScale <= timeScaleTargetValue)
Time.timeScale = timeScaleTargetValue;
//时间流速恢复为正常
Time.timeScale = timeScaleOriginalValue;
2.血条UI改变
public void ChangeBar(float HP,float HPMax,float tempHP,Image hpBar,Image hpBarBuffer,bool addHP)
{
//取得当前血量百分比
tempHP = HP / HPMax;
//判断为扣血时或者血条已经到达准确值时
if(tempHP<=hpBar.fillAmount)
{
//扣血时直接修改血条
hpBar.fillAmount = tempHP;
//加血完成标志为false时,增快缓冲血条减少速度(用于扣血后缓冲血条还在减少而后马上加血且加血后血量值小于缓冲血条值)
if(!addHP)
{
hpBarBuffer.fillAmount -= 0.03f;
//判断缓冲血条减少完成并准确设置值同时修改加血完成标志
if (hpBarBuffer.fillAmount < hpBar.fillAmount)
{
hpBarBuffer.fillAmount = hpBar.fillAmount;
addHP = true;
}
}
//用于缓冲血条值等于血条值的时候的扣血
hpBarBuffer.fillAmount -= 0.003f;
if(hpBarBuffer.fillAmount<hpBar.fillAmount)
{
hpBarBuffer.fillAmount = hpBar.fillAmount;
}
}
//判断为加血
else if(tempHP > hpBar.fillAmount)
{
//让血条缓慢增加
hpBar.fillAmount += 0.03f;
//如果血条增加到加血后的值且缓冲血条还大于血条时,设置加血完成标志还未完成以到扣血条件里面继续减少缓冲血条同时准确设置血条值
if(hpBar.fillAmount >= tempHP && hpBar.fillAmount < hpBarBuffer.fillAmount)
{
hpBar.fillAmount = tempHP;
addHP = false;
}
//如果血条增加到加血后的值且缓冲血条被血条覆盖,准确设置血条值以及让缓冲血条值等于血条值
if(hpBar.fillAmount >= tempHP && hpBar.fillAmount > hpBarBuffer.fillAmount)
{
hpBar.fillAmount = tempHP;
hpBarBuffer.fillAmount = hpBar.fillAmount;
}
}
}
3.屏幕震动
if (test)
{
transform.position = new Vector3((Random.Range(0f, cameraShake)) - cameraShake * 0.5f, (Random.Range(0f, cameraShake)) - cameraShake * 0.5f, -10);
cameraShake = cameraShake / 1.05f;
if (cameraShake < 0.05f)
{
cameraShake = 5.0f;
test = false;
}
}
4.UI渐变出现与消失
canvasGroup.alpha -= Time.deltaTime;//画布渐隐
canvasGroup.alpha += Time.deltaTime;//画布渐显
三.AI相关
四.其他
1.存档读档相关
(1)存档
public void Save()
{
//序列化过程(将Save对象转换为字节流)
//创建Save对象并保存当前游戏状态
Save save = PlayerController.player.CreateSave();
//创建一个二进制格式化程序
BinaryFormatter bf = new BinaryFormatter();
//创建一个文件流
FileStream fileStream = File.Create(Application.dataPath + "/Save" + "/" + PlayerController.player.savePosition.ToString() + ".txt");
//用二进制格式化程序的序列化方法来序列化Save对象,参数:创建的文件流和需要序列化的对象
bf.Serialize(fileStream, save);
//关闭流
fileStream.Close();
}
(2)读档
public void Load(int savePosition)
{
//判断存档是否存在
if(File.Exists(Application.dataPath + "/Save" + "/" + savePosition.ToString() + ".txt"))
{
//反序列化过程
//创建一个二进制格式化程序
BinaryFormatter bf = new BinaryFormatter();
//打开一个文件流
FileStream fileStream = File.Open(Application.dataPath + "/Save" + "/" + savePosition.ToString() + ".txt", FileMode.Open);
//调用格式化程序的反序列化方法,将文件流转换为一个Save对象
Save save = (Save)bf.Deserialize(fileStream);
//关闭文件流
fileStream.Close();
//根据读取到的Save对象设置游戏信息
PlayerController.player.SetGame(save);
}
}
(3)创建Save类
public Save CreateSave()
{
//实例化Sava类
Save save = new Save();
//存储当前游戏信息
save.syringeMax = syringeMax;
save.catStatus = catStatus;
save.spiderStatus = spiderStatus;
save.player_position_x = player_rigdbody2D.position.x;
save.player_position_y = player_rigdbody2D.position.y;
save.livingBoss = enemyManager.livingBoss;
save.yao = enemyManager.yao;
return save;
}
(4)根据存档设置游戏信息
public void SetGame(Save save)
{
//按Save类设置当前游戏信息
syringeMax = save.syringeMax;
catStatus = save.catStatus;
spiderStatus = save.spiderStatus;
player_rigdbody2D.position = new Vector2(save.player_position_x, save.player_position_y);
for(int i=0;i<5;i++)
{
if(save.livingBoss[i]==0)
{
enemyManager.bossList[i].SetActive(false);
}
}
for(int j=0;j<4;j++)
{
if(save.yao[j]==0)
{
enemyManager.yaolist[j].SetActive(false);
}
}
}
Q.E.D.