客户端不会从服务器获取生成的游戏对象,反之亦然
Client doesnt get spawned gameObjects from server, and the other way around
所以,我正在与整个 Unet 系统作斗争。现在,我只想从一端到另一端生成对象。但即使那样也行不通。它在每个服务器和客户端上生成对象。但不同步。我搜索了那么多地方,看了那么多视频 - none 帮了大忙。这是代码和检查员的详细信息
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class NetworkController : NetworkBehaviour
{
public GameObject[] Spawns;
GameObject Canvas;
GameObject spawn;
[SyncVar]
public NetworkInstanceId ParentId;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
[Command]
public void CmdspawnPrefab()
{
Transform trns = GameObject.Find("Canvas").transform;
trns.position = trns.position + new Vector3(100, 200, 0);
GameObject go = Instantiate(Spawns[0], trns);
NetworkServer.Spawn(go);
}
}
我错过了什么?
看来,我的整个工作流程都不正确。解决方案是首先将监听器附加到按钮,而不是在检查器上使用 onclick 函数 -
Button b = BoardController.Buttons[0].GetComponent<Button>();
b.onClick.AddListener(delegate () { Cmd_ButtonPressed(1); });
第二个是创建游戏对象,包含一个控制你的棋盘的脚本 "GameManager",以及一个包含 Rpc 函数的 "RpcManager"。然后在播放器对象脚本中,使用命令。
这是我用过的 类,更新图像而不是生成对象,想法基本相同 - 理解 Unet 的基础知识和传递命令。
public class PlayerObject : NetworkBehaviour {
public BoardManager BoardController;
public ClientRPCmanager ClientRPCManager;
// Use this for initialization
void Start () {
ClientRPCManager = GameObject.FindGameObjectWithTag("ClientRPCmanager").GetComponent<ClientRPCmanager>();
BoardController = ClientRPCManager.BoardController;
Button b = BoardController.Buttons[0].GetComponent<Button>();
b.onClick.AddListener(delegate () { Cmd_ButtonPressed(1); });
}
// Update is called once per frame
void Update () {
}
public void SetButton(int i)
{
BoardController.SetButton(i);
}
[Command]
public void Cmd_ButtonPressed(int i)
{
ClientRPCManager.Rpc_ButtonPressed(i);
}
董事会经理
public class BoardManager : NetworkBehaviour
{
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public GameObject[] Buttons;
public Sprite[] Sprites;
public void SetButton(int index)
{
GameObject img = GameObject.Find("X");
img.GetComponent<Image>().sprite = Sprites[0];
}
RpcManager
public class ClientRPCmanager : NetworkBehaviour {
public BoardManager BoardController;
public NetworkManager Manager;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
[ClientRpc]
public void Rpc_ButtonPressed(int index)
{
BoardController.SetButton(index);
}
希望这对人们了解 Unet 有所帮助。
我通过仔细查看这个项目了解到这一点 https://www.youtube.com/watch?v=8Kd2RAfgzW0
所以,我正在与整个 Unet 系统作斗争。现在,我只想从一端到另一端生成对象。但即使那样也行不通。它在每个服务器和客户端上生成对象。但不同步。我搜索了那么多地方,看了那么多视频 - none 帮了大忙。这是代码和检查员的详细信息
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class NetworkController : NetworkBehaviour
{
public GameObject[] Spawns;
GameObject Canvas;
GameObject spawn;
[SyncVar]
public NetworkInstanceId ParentId;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
[Command]
public void CmdspawnPrefab()
{
Transform trns = GameObject.Find("Canvas").transform;
trns.position = trns.position + new Vector3(100, 200, 0);
GameObject go = Instantiate(Spawns[0], trns);
NetworkServer.Spawn(go);
}
}
我错过了什么?
看来,我的整个工作流程都不正确。解决方案是首先将监听器附加到按钮,而不是在检查器上使用 onclick 函数 -
Button b = BoardController.Buttons[0].GetComponent<Button>();
b.onClick.AddListener(delegate () { Cmd_ButtonPressed(1); });
第二个是创建游戏对象,包含一个控制你的棋盘的脚本 "GameManager",以及一个包含 Rpc 函数的 "RpcManager"。然后在播放器对象脚本中,使用命令。
这是我用过的 类,更新图像而不是生成对象,想法基本相同 - 理解 Unet 的基础知识和传递命令。
public class PlayerObject : NetworkBehaviour {
public BoardManager BoardController;
public ClientRPCmanager ClientRPCManager;
// Use this for initialization
void Start () {
ClientRPCManager = GameObject.FindGameObjectWithTag("ClientRPCmanager").GetComponent<ClientRPCmanager>();
BoardController = ClientRPCManager.BoardController;
Button b = BoardController.Buttons[0].GetComponent<Button>();
b.onClick.AddListener(delegate () { Cmd_ButtonPressed(1); });
}
// Update is called once per frame
void Update () {
}
public void SetButton(int i)
{
BoardController.SetButton(i);
}
[Command]
public void Cmd_ButtonPressed(int i)
{
ClientRPCManager.Rpc_ButtonPressed(i);
}
董事会经理
public class BoardManager : NetworkBehaviour
{
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public GameObject[] Buttons;
public Sprite[] Sprites;
public void SetButton(int index)
{
GameObject img = GameObject.Find("X");
img.GetComponent<Image>().sprite = Sprites[0];
}
RpcManager
public class ClientRPCmanager : NetworkBehaviour {
public BoardManager BoardController;
public NetworkManager Manager;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
[ClientRpc]
public void Rpc_ButtonPressed(int index)
{
BoardController.SetButton(index);
}
希望这对人们了解 Unet 有所帮助。 我通过仔细查看这个项目了解到这一点 https://www.youtube.com/watch?v=8Kd2RAfgzW0