我正在製作一個簡單的局域網遊戲,以便在Unity網絡中獲得我的支持。所有它應該做的事情是,當玩家點擊網格中的方塊時,它會將其改變爲藍色。我的問題是當LAN主機點擊一個正方形時,它只在本地更新並且不更新客戶端。當客戶點擊廣場時,它在本地更新並且LAN主機得到更新,但其他客戶不會更新。我所有的網格塊都附有網絡標識簡單的Unity5局域網遊戲,服務器更新客戶端不是
任何想法?
繼承人的代碼:
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class Player_Paint : NetworkBehaviour {
[SyncVar]GameObject syncGridPiece;
GameObject gridPiece;
void Update() {
Paint();
TransmitGridColours();
}
void Paint(){
if(isLocalPlayer && Input.GetMouseButtonDown(0)){
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
if(hit.collider != null){
print(GameObject.Find (hit.transform.name));
gridPiece = hit.collider.transform.gameObject;
gridPiece.GetComponent<SpriteRenderer>().color = Color.blue;
}
}
}
[Command]
void CmdProvideGridColourToServer(GameObject gridPiece){
if(gridPiece){
syncGridPiece = gridPiece;
syncGridPiece.GetComponent<SpriteRenderer>().color = Color.blue;
}
}
[Client]
void TransmitGridColours(){
if(isLocalPlayer){
CmdProvideGridColourToServer(gridPiece);
}
}
}
「if(gridPiece){」?這是一個遊戲對象...順便說一句,你只需要更新同步變量,所有的屬性/值/等被推到本地播放器的服務器(即改變顏色,然後推高) – 2016-11-17 15:19:25
「if( gridPiece){「當遊戲開始時GO是空的。只有在點擊時纔會填充。只是把它放在那裏,以阻止它引發我參考錯誤。我沒有完全理解你的意思。當我做「syncGridPiece = gridPiece;」是不是更新變量? – justinC19
[Command] ==用本地播放器調用它向服務器提交數據(通過[SyncVar]標記的成員變量)。 [客戶端**連接**] ==通過標有[命令]前綴 – 2016-11-17 16:28:32