在统一中从其他场景重生敌人



我可能走错了路,但我遇到了一个障碍。在我的游戏中,坐在像营火一样的灵魂处应该重生所有带有特定类型的敌人,我想不出一种方法可以在玩家休息时从其他场景中获得这种效果。

我已经创建了重生脚本,但它只在与敌人在同一个房间休息时有效。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyManager : MonoBehaviour
{
public GameObject enemyCore;
private Core core;
enum DeathType { respawnOnEnter, respawnOnRest, neverRespawn}
[SerializeField] DeathType deathType;
private Death Death => death ? death : core.GetCoreComponent(ref death);
private Death death;
private bool canRespawn = true;
private bool justRested = false;
private bool justRespawned = false;

public void Start()
{
core = enemyCore.GetComponent<Core>();

if (ES3.KeyExists(core.transform.parent.gameObject.name + "canRespawn"))
canRespawn = ES3.Load<bool>(core.transform.parent.gameObject.name + "canRespawn");
if (ES3.KeyExists("justRested"))
canRespawn = ES3.Load<bool>("justRested");

switch (deathType)
{
case DeathType.respawnOnEnter:
canRespawn = true;
break;
case DeathType.respawnOnRest:
if(justRested || justRespawned)
{
canRespawn = true;
justRespawned = false;
}

break;
}
ES3.Save(core.transform.parent.gameObject.name + "canRespawn", canRespawn);
if (!canRespawn)
{
core.transform.parent.gameObject.SetActive(false);
}
}
public void Rested()
{
justRested = true;
justRespawned = true;
ES3.Save("justRested", justRested);
}
public void Update()
{
if(Death.JustDied)
{
switch (deathType)
{
case DeathType.respawnOnEnter:
canRespawn = true;
break;
case DeathType.respawnOnRest:
canRespawn = false;
break;
case DeathType.neverRespawn:
canRespawn = false;
break;
}

ES3.Save(core.transform.parent.gameObject.name + "canRespawn", canRespawn);
Death.JustDied = false;
}
switch (deathType)
{
case DeathType.respawnOnRest:
if (justRested)
{
canRespawn = true;
justRested = false;

ES3.Save(core.transform.parent.gameObject.name + "canRespawn", canRespawn);
}
break;
}
}
private void OnEnable()
{
PlayerRestState.RestAction += Rested;
}
private void OnDisable()
{
PlayerRestState.RestAction -= Rested;
}

}
这是我写的代码,脚本应该分别针对每个敌人。谢谢你的帮助。

固定使用脚本对象意味着我可以从任何场景的任何敌人访问数据。如果有人想知道。

最新更新