UNET更改客户端的子对象的纹理

时间:2016-03-21 14:56:25

标签: scripting multiplayer unity3d-unet

我尝试了多种技术,但仍然没有结果。这个 post似乎很有希望,但我仍然有同样的问题。主持人能够改变颜色,但不能改变客户端玩家。代码看起来像这样。

public List<GameObject> bases = new List<GameObject>();
[SyncVar]
public Texture redTexture;
[SyncVar]
public Texture blueTexture;
[SyncVar]
GameObject objID;
NetworkIdentity objNetID;
public void CheckTexture()
{
    if (isLocalPlayer)
    {
        for (int i = 0; i < bases.Count; ++i)
        {
            var tempScript = bases[i].GetComponent<BaseScoreScript>();
            if (tempScript.owner == "blue")
            {
                objID = bases[i];
                CmdCheckText(objID, blueTexture);
            }
        }
    }
}
 [ClientRpc]
void RpcChangeTexture(GameObject obj, Texture text)
{
    obj.GetComponentInChildren<MeshRenderer>().material.mainTexture = text;
}
[Command]
void CmdCheckText(GameObject obj, Texture texture)
{
    objNetID = obj.GetComponent<NetworkIdentity>();
    objNetID.AssignClientAuthority(connectionToClient);
    RpcChangeTexture(obj, texture);
    objNetID.RemoveClientAuthority(connectionToClient);
}

任何建议都会有所帮助。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

感谢任何看过这个的人。我解决了这个问题。可能不是最有效的,我可以稍后再努力。但是我向非玩家对象添加了一个[Command](它有一个NetworkIdentidy并设置为本地玩家),在这个命令中我引用场景中的玩家列表并在playerscript中设置一个公共变量(这是一个SyncVar &lt; - 那是什么呢)。在playercript中我引用了非Player对象并将其纹理设置为我想要的纹理。不担心它是否是本地人,因为我希望所有玩家都能看到变化。

[SyncVar]
public string baseOwner;

void BaseStatus()
{
    var blue = Color.blue;
    var red = Color.red;

    foreach(GameObject obj in bases)
    {
        if (baseOwner == "blue")
        {
            centBaseStatus.image.color = blue;
            centBaseStatus.GetComponentInChildren<Text>().text = "Center Base : "+ baseOwner;
            GameObject.Find("CenterBase").GetComponentInChildren<Renderer>().material.mainTexture = BlueBase;
        }

    }
}

这是非播放器脚本中的代码,并且:

{{1}}

为playercrip。 我知道它不漂亮。但是,如果您对更好的方法有任何建议,请告诉我。如果这对某人有帮助......真棒酱!