Unity Mirror: 在与调用 clientRpcs 的命令结合使用自定义列表时出现问题

huangapple go评论78阅读模式
英文:

Unity Mirror: Problem with using custom lists in combination with commands that call clientRpcs

问题

重要信息:

  • 1 个客户端和 1 个主机
  • 两者都有一个自定义类的对象列表(不同的列表)
  • 任务是将主机的一个对象添加到客户端的一个对象之前,并使客户端和主机都具有此版本
  • 客户端调用命令以启动主机,主机接收到后填充自己的列表,然后调用 clientRpc 并将自己的列表传递给客户端

最后一步是问题所在:主机在命令中填充的列表是正确的(使用 Debug.Log() 进行观察),但是在客户端参数中接收到的列表具有相同的长度,但内容不同,所有对象都为空白(对象的变量值为 null,即使在主机端不是这样)。

我不知道如何解决这个问题,除非将变量值分开发送。但这样做的问题是数据量会过大,所以我正在寻找一个更好的替代方案。

(对不起我的英文不好。如果你们中的任何人会说德语,我可以用德语给你们提供一个更好的问题替代方案。)
谢谢你的帮助!

我的代码:

using Mirror;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Übermittlung : NetworkBehaviour
{
    [SerializeField] private Judoka_speicher Js;

    void Start()
    {
        if (isClientOnly) 
        {
            skipframe();
            Debug.Log("Sync-Start(c)");
            liste_synchronisieren_t1_command();
        }
    }

    [Command]
    private void liste_synchronisieren_t1_command() 
    {
        if (isServer && isClient) 
        {
            Debug.Log("Sync_Start(s)");
            List<Judokaklassenhalter.Judoka> judokas = Js.GetJudokas();
            Debug.Log(judokas[0].GetVorname());
            liste_synchronisieren_teil1(judokas);
        }
    }

    [ClientRpc]
    private void liste_synchronisieren_teil1(List<Judokaklassenhalter.Judoka> alteListe) 
    {
        Debug.Log(Js.GetJudokas()[0].GetVorname());
        if (isClientOnly) 
        {
            List<Judokaklassenhalter.Judoka> liste = alteListe;
            Debug.Log("1.Sync-Befehl");
            Debug.Log(liste[0]);
            List<Judokaklassenhalter.Judoka> alteListe2 = Js.addListVorne(alteListe);
            liste_synchronisieren_t2_command(alteListe2);
        }
    }
}
英文:

important information:

  • 1 client and 1 host
  • both have a list of objects of a custom class (different lists)
  • task is to add the one of the host in front of the one of the client and to have this version on the client and the host
  • client calls command to start host recieves it and fills in his list and then calls a clientRpc and passes his list on to the client

The last step is the problem: The list that the host fills in in the command is correct (used Debug.Log() to observe) but the list recieved in the parameter on the client has the same length but the content is diffrent the objects are all blank (the variables of the objects have the value null even it wasn't like that on the hosts side).

I have no ideas how to solve the problem but to send the variable values seperated. The problem with this is that the data amount would be to great so that I am looking for a better alternative.

(Sorry for my bad english. If anyone of you speaks german, I can give you a better alternative of the problem in german.)
Thanks for your help!

my code:

using Mirror;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class &#220;bermittlung : NetworkBehaviour
{
    [SerializeField] private Judoka_speicher Js;

void Start()
    {
        if (isClientOnly) 
        {
            skipframe();
            Debug.Log(&quot;Sync-Start(c)&quot;);
            liste_synchronisieren_t1_command();
        }
    }

    [Command]
    private void liste_synchronisieren_t1_command() 
    {
        if (isServer &amp;&amp; isClient) 
        {
            Debug.Log(&quot;Sync_Start(s)&quot;);
            List&lt;Judokaklassenhalter.Judoka&gt; judokas = Js.GetJudokas();
            Debug.Log(judokas[0].GetVorname());
            liste_synchronisieren_teil1(judokas);
        }
    }

    [ClientRpc]
    private void liste_synchronisieren_teil1(List&lt;Judokaklassenhalter.Judoka&gt; alteListe) 
    {
        Debug.Log(Js.GetJudokas()[0].GetVorname());
        if (isClientOnly) 
        {
            List&lt;Judokaklassenhalter.Judoka&gt; liste = alteListe;
            Debug.Log(&quot;1.Sync-Befehl&quot;);
            Debug.Log(liste[0]);
            List&lt;Judokaklassenhalter.Judoka&gt; alteListe2 = Js.addListVorne(alteListe);
            liste_synchronisieren_t2_command(alteListe2);
        }
    }
}

答案1

得分: 0

对我有效的解决方案是将列表中对象的类中的变量(字符串和整数)设置为公共的。

英文:

The solution that worked for me is to make the variables (strings and ints) in the class of the objects in the list public.

huangapple
  • 本文由 发表于 2023年7月31日 21:00:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76803888.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定