Mirror 和 unity 上的多人同步

问题描述

我正在尝试制作一个应用程序,我将输入从我的 android 设备发送到服务器,即连接 Arduino 的我的电脑。所以PC(服务器)将输入发送到Arduino。但是使用镜像,我在数据同步方面遇到了一些困难。我能够连接服务器和客户端,但我无法将数据从客户端发送到服务器或从服务器发送到客户端。 我也尝试过 YouTube 教程和 [Command],但我想我错过了一些东西。 我只想将命令从客户端发送到服务器,以便它发送到 Arduino。如果有人可以提供帮助,那就太好了。 脚本代码如下

using System.Collections.Generic;
using UnityEngine;
using Mirror;
using TMPro;
public class syncData : NetworkBehavIoUr
{

    public GameObject DFA;

    public TMP_Text data1;
    public TMP_Text data2;
    public TMP_Text data3;
    [SyncVar]
   
    string receivedString;

    public delegate void StringChangedDelegate(string receivedString);


    /*[SyncEvent]*/
    public event StringChangedDelegate EventStringChanged;

    private void SetString(string receivedString)
    {
        receivedString = DFA.GetComponent<Comunicacion>().receivedstring;


    }
    public override void OnStartServer()
    {
        SetString(receivedString);


    }
    // Start is called before the first frame update
    void Start()
    {
        //  DFA = GameObject.FindWithTag("dfa"); // tag,not name
    }
    /*    [Command]
        private void changeString() => SetString(receivedString);
    */

    // Update is called once per frame
 //   [ClientCallBack]
    void Update()
    {
    //    Debug.Log("Client is sending information.");
    //    receivedString = DFA.GetComponent<Comunicacion>().receivedstring;
        refresh(receivedString);
    }

    [Server]
    void FunctionServer()
    {
        Debug.Log("Running the program in Server mode.");
    }
    [ClientRpc]
    void refresh(string receivedString)
    {

        //   receivedString = DFA.GetComponent<Comunicacion>().receivedstring;

        if (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.B))
        {
            Debug.Log(" Right Arrow Button was pressed on Vuzix");

        }
        else if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.X))
        {
            Debug.Log(" Left Arrow Button was pressed on Vuzix");
        }
        refresher();
    //    string[] datos = receivedString.Split(':'); //My arduino script returns a 3 part value (IE: 12,30,18)
     ////   data1.SetText(datos[0]);//       Try to forcefully re-enter values from DFA
     //   data2.SetText(datos[1]);
     //   data3.SetText(datos[2]);
    }

    [Command]
    void refresher()
    {
     //   receivedString = DFA.GetComponent<Comunicacion>().receivedstring;
        refresh(receivedString);
        Debug.Log("A command has been sent");

        //    gm.Update()
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...