Networking Player Health
网络玩家健康
Changes to the player’s current health should only be applied on the Server.
对播放器当前健康状况的更改应该只应用于服务器。
These changes are then synchronized on the Clients.
然后这些更改在客户端上同步。
This is called Server Authority.
这称为服务器权限。
For more information on Server Authority please see the page on Network System Concepts.
有关服务器权限的更多信息,请参阅有关网络系统概念的页面。
To make our current health and damage system network aware and working under Server authority, we need to use State Synchronization and a special member variable on networked objects called SyncVars.
为了使我们当前的健康和损害系统网络意识到并在服务器权限下工作,我们需要在称为SyncVars的联网对象上使用状态同步和一个特殊的成员变量。
Network synchronized variables, or SyncVars, are indicated with the attribute [SyncVar].
网络同步变量(或SyncVars)用属性[SyncVar]表示。
For more information on SyncVars, please see the page on State Synchronization.
有关SyncVars的更多信息,请参阅状态同步页面。
Open the Health script for editing.
打开用于编辑的健康脚本。
Add the namespace UnityEngine.Networking.
UnityEngine.Networking添加名称空间。
using UnityEngine.Networking;
Change script to derive from NetworkBehaviour.
从网络行为派生的更改脚本。
public class Health : NetworkBehaviour
Make currentHealth a [SyncVar].
使currentHealth[SyncVar]。
[SyncVar]
public int currentHealth = maxHealth;
Add a check for “isServer” to the TakeDamage function, so that damage will only be applied on the Server.
在TakeDamage函数中添加一个“isServer”的检查,这样就只会在服务器上应用。
if (!isServer)
{
return;
}
The final script should look like this:
Health
C#
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using System.Collections;
public class Health : NetworkBehaviour {
public const int maxHealth = 100;
[SyncVar]
public int currentHealth = maxHealth;
public RectTransform healthBar;
public void TakeDamage(int amount)
{
if (!isServer)
{
return;
}
currentHealth -= amount;
if (currentHealth <= 0)
{
currentHealth = 0;
Debug.Log("Dead!");
}
healthBar.sizeDelta = new Vector2(currentHealth, healthBar.sizeDelta.y);
}
}
Save the script.
保存脚本。
Return to Unity.
回到Unity。
Build and Run this scene as a standalone application.
构建并运行这个场景作为一个独立的应用程序。
Click the Host button from the in-game UI to start this game as a Host.
单击游戏内UI中的主机按钮以作为主机启动此游戏。
Move the Player GameObject.
玩家GameObject移动。
Return to Unity.
回到Unity。
Enter Play Mode.
进入播放模式。
Click the LAN Client button from the in-game UI to connect to the Host as a Client.
单击游戏内UI中的LAN客户端按钮以连接到主机作为客户端。
Now the player’s current health is only being applied on the Server and being synchronized on all of the Clients.
现在,玩家的当前健康状态只应用在服务器上,并在所有客户端上同步。
This is difficult to see on all clients, however, because the Healthbar is not working on all of the instances of the game.
然而,这在所有客户端都很难看到,因为Healthbar并没有处理所有的游戏实例。
The variable currentHealth is public and can be seen in the editor.
变量currentHealth是公共的,可以在编辑器中看到。
If the editor is being run as the connected client, not the host, the current health on the Player GameObjects should be easy to see in the Inspector.
如果编辑器是作为连接的客户机运行的,而不是主机,那么在检查器中应该很容易看到当前的游戏对象的健康状况。
It should also be easy to see that the Healthbar is working, but only working on the Host Client attached to the Server and not on any of the other Clients.
它还应该很容易看到Healthbar正在工作,但是只在服务器上的主机客户机上工作,而不是在任何其他客户机上。
This is because we are not synchronizing the value of the Foreground GameObject’s RectTransform across the network and the code to set the Healthbar’s Size Delta is only being run on the Server.
这是因为我们没有在整个网络上同步前台GameObject的RectTransform的值,而设置Healthbar的大小增量的代码只在服务器上运行。
The reason the Healthbar works on the Host Client is because it is local to the Server.
Healthbar在主机客户机上工作的原因是它是本地服务器。
The Host Client does not have data serialized to it because it shares the same scene with the Server.
主机客户端没有数据序列化,因为它与服务器共享相同的场景。
We now need to synchronize the RectTransform on the Healthbar's Foreground GameObject.
现在我们需要在Healthbar的前景游戏对象上同步RectTransform。
Close the standalone player.
关闭独立的球员。
Return to Unity.
回到Unity。
Exit Play Mode.
退出播放模式。
This brings us to another tool for State Synchronization: the SyncVar hook.
这给我们带来了另一个状态同步的工具:SyncVar钩子。
SyncVar hooks will link a function to the SyncVar.
SyncVar钩子将一个函数链接到SyncVar。
These functions are invoked on the Server and all Clients when the value of the SyncVar changes.
当SyncVar的值发生变化时,这些函数将在服务器和所有客户机上调用。
For more information on SyncVars and SyncVar hooks, please see the page on State Synchronization.
有关SyncVars和SyncVar钩子的更多信息,请参阅状态同步页面。
Open the Health script for editing.
打开用于编辑的健康脚本。
Move the code that changes the Healthbar into it’s own function called OnChangeHealth.
将更改Healthbar的代码移动到它自己的函数OnChangeHealth中。
void OnChangeHealth (int currentHealth)
{
healthBar.sizeDelta = new Vector2(health, currentHealth.sizeDelta.y);
}
It is worth noting that this function must have a parameter of the same type as the variable with the [SyncVar] attribute, in this case int currentHealth, and that the current value of the SyncVar will be sent to the hooked function as an argument.
值得注意的是,该函数必须具有与具有[SyncVar]属性的变量相同类型的参数,在本例中是int currentHealth,并且将SyncVar的当前值作为参数发送到钩子函数。
Set a hook to this new function in the SyncVar attribute for currentHealth.
在SyncVar属性中为currentHealth设置一个钩子。
[SyncVar(hook = "OnChangeHealth")]
The final script should look like this:
Health
C#
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using System.Collections;
public class Health : NetworkBehaviour {
public const int maxHealth = 100;
[SyncVar(hook = "OnChangeHealth")]
public int currentHealth = maxHealth;
public RectTransform healthBar;
public void TakeDamage(int amount)
{
if (!isServer)
return;
currentHealth -= amount;
if (currentHealth <= 0)
{
currentHealth = 0;
Debug.Log("Dead!");
}
}
void OnChangeHealth (int health)
{
healthBar.sizeDelta = new Vector2(health, healthBar.sizeDelta.y);
}
}
Now when the value of currentHealth changes, OnChangedHealth will be called on the Server and all Clients to update the Healthbar.
现在,当currentHealth的值发生变化时,将在服务器和所有客户端调用OnChangedHealth,以更新Healthbar。
Save the script.
保存脚本。
Return to Unity.
回到Unity。
Build and Run this scene as a standalone application.
构建并运行这个场景作为一个独立的应用程序。
Click the Host button from the in-game UI to start this game as a Host.
单击游戏内UI中的主机按钮以作为主机启动此游戏。
Move the Player GameObject.
玩家GameObject移动。
Return to Unity.
回到Unity。
Enter Play Mode.
进入播放模式。
Click the LAN Client button from the in-game UI to connect to the Host as a Client.
单击游戏内UI中的LAN客户端按钮以连接到主机作为客户端。
When the players shoot each other, all of the Healthbars should now reflect the value of player GameObject’s Current Health.
当玩家互相射击时,所有的Healthbars现在应该反映玩家的当前健康的价值。
The Healthbar is now synchronized and works in all instances of the game.
Healthbar现在已经同步并在游戏的所有实例中工作。
Close the standalone player.
关闭独立的球员。
Return to Unity.
回到Unity。
Exit Play Mode.
退出播放模式。