本文介绍了UNET-同步.非玩家对象上颜色的脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试修复了数周,但没有成功,真的需要帮助.

I have tried to fix this for weeks now without succeeding and REALLY need help.

当我在实际播放器对象上启动颜色同步时,我能够在非播放器对象上同步颜色.

I am able to sync color on my non-player objects when i initiate the color sync on the actual player object.

但是,我的问题是我想将脚本附加到管理颜色同步的非玩家对象上.对于位置和旋转同步,我也有相同的效果.

However, the problem I have is that i want to attach a script to the non-player object that manage the color sync. I have the same for position and rotation sync that works.

当我拖动非玩家对象时,我将其着色为红色,而当我拖放该拖动时,我将其重新着色为白色.但是,在拖动远程客户端时,授权出现问题,这就是为什么我认为我无法解决.

When i drag the non-player object i color it red and when i drop the drag i re-color it back to white. However, i get problem with authorization when dragging on the remote client and that is why i think I am not able to solve.

以下是顺序:

  1. 在拖动开始a)AssignNetworkAuthority b)将颜色更改为红色
  2. 拖动
  3. On_DragEnd a)重新绘制为白色b)ReleaseNetworkAuthority

当我从远程客户端执行此操作时,出现权限错误.我的结论(可能是错误的)是,由于某些原因,分配删除权限语句与绘画不同步.

When I do this from the remote-client i get an authority error. My conclusion, probably wrong, is that the assign-remove authority statements get out of sync with the painting for some reason.

这是我使用的代码(childGameObject之前已分配):

Here is the code i use (childGameObject is assigned earlier):

public void On_DragStart (Gesture gesture) {

    if (!isLocalPlayer)
        return;

    // Assign Network Authority
    myNetID = childGameObject.GetComponent<NetworkIdentity> ();

    // >>>> HERE I ASSIGN AUTHORITY
    Cmd_AssignNetworkAuthority (myNetID.netId, childGameObject);

    // >>>> HERE I CHANGE THE COLOR TO RED
    childGameObject.GetComponent<Renderer> ().material.color = Color.red;

}

public void On_Drag(Gesture gesture) {

    if (!hasAuthority)
        return;

    if (firstRun) {
        firstRun = false;
    }

    myTransform.transform.position = Camera.main.ScreenToWorldPoint (new Vector3 (gesture.position.x, gesture.position.y, 1f));

}

public void On_DragEnd (Gesture gesture) {

    if (!isLocalPlayer)
        return;

    // >>>> HERE I CHANGE THE COLOR BACK TO WHITE
    //      gesture.pickedObject.GetComponent<Renderer> ().material.color = Color.white;
    childGameObject.GetComponent<Renderer> ().material.color = Color.white;

    // >>>> HERE I RELEASE AUTHORITY
    Cmd_ReleaseNetworkAuthority ();

}

这是附加到非玩家对象上的实际sync.script:

Here is the actual sync.script that is attached to the non-player object:

using UnityEngine;
using UnityEngine.Networking;
using System.Collections;

public class Object_ColorSync : NetworkBehaviour {

[SyncVar] private Color syncColor;
public GameObject myGO;

void Start () {
    syncColor = myGO.GetComponent<Renderer> ().material.color;
}

void Update () {
    DoColor ();
}

void DoColor() {
    Cmd_DoColor (myGO.GetComponent<Renderer> ().material.color);
}

[Command]
void Cmd_DoColor(Color _myColor) {

    syncColor = _myColor;
    Rpc_DoColor (_myColor);
}

[ClientRpc]
void Rpc_DoColor(Color _syncColor) {
    myGO.GetComponent<Renderer> ().material.color = _syncColor;
}
}

同步.脚本是用于测试的,因此在我启动Cmd_DoColor时并未真正进行优化.最后,我将添加一个if语句,并检查颜色是否已更改.

The sync. script is for testing so not really optimized in regards when i fire off the Cmd_DoColor. In final i will add an if statement and check if color have been changed.

推荐答案

问题已解决 :-)我在发布此线程并改用SyncVar钩子后重新考虑了此问题的方法问题就解决了!

Problem solved :-) I was re-thinking my approach to this problem after i posted this thread and did a SyncVar hook instead and problem solved!

这是新的同步代码:

using UnityEngine;
using UnityEngine.Networking;
using System.Collections;

public class Object_ColorSync : NetworkBehaviour {

[SyncVar (hook = "OnColorChange")] Color newColor;
private Color currentColor;


void OnColorChange(Color _myColor) {

    gameObject.GetComponent<Renderer> ().material.color = _myColor;
    currentColor = _myColor;

}

void Update () {

    if (!hasAuthority)
        return;

    if (currentColor != gameObject.GetComponent<Renderer> ().material.color) {
        Cmd_DoColor (gameObject.GetComponent<Renderer> ().material.color);
    }
}

[Command]
void Cmd_DoColor(Color _theColor) {
    newColor = _theColor;
}

}

这篇关于UNET-同步.非玩家对象上颜色的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 22:34