私は、Unity の新しい Unet 機能を使用して簡単な三目並べゲームを作成し、Unet 機能の使用方法を学んでいます。
ボードを構成するために 9 つのスプライトを生成するブレイン コードがあり、クリックするとこれらが x に設定されるように取り組んでいます。[synchvar] を使用してスプライトの状態を追跡し、フックを使用して変更時に画像を変更し[SyncVar]
ます。サーバーを制御している場合、ゲームは完全に動作しますが、クライアントを制御している場合は何もしません。私が理解していることから、オブジェクトにローカル プレイヤー権限を追加し、[コマンド] を使用してこれを修正しますが、これは問題を修正していないようです。以下のコードは、私のスプライトに添付されているものです。質問には回答があり、以下のコードは修正を反映しています。
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine.Networking;
public class spacescript : NetworkBehaviour {
public Sprite x;
public Sprite o;
SpriteRenderer sr;
public int state;
NetworkIdentity myNetId;
void Awake ()
{
myNetId = GetComponent<NetworkIdentity>();
}
public void setstate(int newstate)
{
state=newstate;
if (state == 1)
{
gameObject.GetComponent<SpriteRenderer> ().sprite = x;
}
}
void OnMouseDown()
{
GameObject whatplayer = braincode.singleton.GetComponent<braincode> ().returnplayer();
whatplayer.GetComponent<playerscript> ().thingwasclicked (myNetId, gameObject);
}
}
私のタイルは次のコードによって生成されます
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine.Networking;
public class braincode : NetworkBehaviour {
public GameObject spaceprefab;
static public braincode singleton;
GameObject players;
public static GameObject brain;
void Start ()
{
singleton = this;
brain = gameObject;
}
public GameObject returnplayer()
{
return players;
}
public void add_player(GameObject player)
{
players = player;
}
public override void OnStartServer()
{
for (int x=0; x<3; x++) {
for (int y=0; y<3; y++) {
GameObject space = (GameObject)GameObject.Instantiate(spaceprefab, transform.position, Quaternion.identity);
space.transform.position = new Vector3 (-3 + (2 * x), -3+ (2*y), 0f);
NetworkServer.Spawn (space);
}
}
}
}
私のプレーヤーオブジェクトには次のコードがあります
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine.Networking;
public class playerscript : NetworkBehaviour {
void Start ()
{
if (isLocalPlayer)
{
braincode.brain.GetComponent<braincode> ().add_player (gameObject);
}
}
public void thingwasclicked(NetworkIdentity tiles , GameObject whoclicked)
{
Cmdassignstate (tiles,whoclicked);
}
[Command]
public void Cmdassignstate(NetworkIdentity tile, GameObject whoclicked)
{
RpcUpdateState (1,whoclicked);
}
[ClientRpc]
void RpcUpdateState(int newState, GameObject tile)
{
tile.GetComponent<spacescript> ().setstate (1);
}
}