UNET - Sync. script for color on non-player object

余生颓废 提交于 2019-12-12 03:05:32

问题


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.

Here is the sequence:

  1. On-DragStart a) AssignNetworkAuthority b) Change Color to red
  2. Drag
  3. On_DragEnd a) Repaint to white 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.

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 ();

}

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;
}
}

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.


回答1:


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!

Here is the new sync-code:

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;
}

}


来源:https://stackoverflow.com/questions/39859576/unet-sync-script-for-color-on-non-player-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!