上一部分玩家摧毁的方法是设置其enable为false,那么玩家的复活就是在玩家死后,显示复活的按钮,点击按钮后把相关的enable再次设置为true即可
新建脚本Player_Respawn
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
using UnityEngine.UI;
public class Player_Respawn : NetworkBehaviour {
private Player_Health healthScript;
private Image crossHairImage;
private GameObject respawnButton;
// Use this for initialization
void Start ()
{
healthScript = GetComponent<Player_Health>();
healthScript.EventRespawn += EnablePlayer;
crossHairImage = GameObject.Find("crossHairImage").GetComponent<Image>();
SetRespawnButton();
}
void SetRespawnButton()
{
if (isLocalPlayer)
{
respawnButton = GameObject.Find("GameManager").GetComponent<GameManager_References>().respawnButton;
respawnButton.GetComponent<Button>().onClick.AddListener(CommenceRespawn);
respawnButton.SetActive(false);
}
}
void OnDisable()
{
healthScript.EventRespawn -= EnablePlayer;
}
void EnablePlayer()
{
GetComponent<CharacterController>().enabled = true;
GetComponent<Player_Shoot>().enabled = true;
GetComponent<BoxCollider>().enabled = true;
Renderer[] renderers = GetComponentsInChildren<Renderer>();
foreach (Renderer ren in renderers)
{
ren.enabled = true;
}
if (isLocalPlayer)
{
GetComponent<UnityStandardAssets.Characters.FirstPerson.FirstPersonController>().enabled = true;
crossHairImage.enabled = true;
respawnButton.SetActive(false);
}
}
void CommenceRespawn()
{
CmdRespawnOnServer();
}
[Command]
void CmdRespawnOnServer()
{
healthScript.ResetHealth();
}
}
public delegate void RespawnDelegate();在Player_Death中的预留位置添加设置按钮活动的一行代码
public event RespawnDelegate EventRespawn;
void CheckCondition()
{
if (health <= 0 && !shouldDie&&!isDead)
{
shouldDie = true;
}
if (health <= 0 && shouldDie)
{
if (EventDie != null)
{
EventDie();
}
shouldDie = false;
}
if (health > 0 && isDead)
{
if (EventRespawn != null)
{
EventRespawn();
}
isDead = false;
}
}
public void ResetHealth()
{
health = 100;
}
void DisablePlayer()
{
GetComponent<CharacterController>().enabled=false;
GetComponent<Player_Shoot>().enabled = false;
GetComponent<BoxCollider>().enabled = false;
Renderer[] renderers = GetComponentsInChildren<Renderer>();
foreach (Renderer ren in renderers)
{
ren.enabled = false;
}
healthScript.isDead = true;
if (isLocalPlayer)
{
GetComponent<UnityStandardAssets.Characters.FirstPerson.FirstPersonController>().enabled = false;
crossHairImage.enabled = false;
GameObject.Find("GameManager").GetComponent<GameManager_References>().respawnButton.SetActive(true);
}
}
= =反正真的没什么人看。。我也就懒得注释了