0

我正在尝试制作一个应用程序,将输入从我的 android 设备发送到服务器,即连接 Arduino 的我的电脑。因此,PC(服务器)将输入发送到 Arduino。但是使用 Mirror,我在数据同步方面遇到了一些困难。我能够连接服务器和客户端,但我无法将数据从客户端发送到服务器或从服务器发送到客户端。我也尝试过 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()
    }
}
4

0 回答 0