【问题标题】:Using Unity and Photon PUN, Is there a way to synchronize alterations to a tilemap changed at runtime with SetTile()?使用 Unity 和 Photon PUN,有没有办法在运行时使用 SetTile() 同步更改对瓷砖地图的更改?
【发布时间】:2021-09-08 06:44:52
【问题描述】:

我遇到这个问题已经有一段时间了,想知道如何解决它。基本上我有一个树图块,当我在鼠标悬停在树上时左键单击时,它的图块设置为 null 并产生了一个项目。现在,项目生成工作正常,但树图块 st 为 null 不会同步到其他客户端。有什么修复吗?

void BreakDecor()
{
    GameObject spawnedItem = PhotonNetwork.Instantiate("Item", new Vector3(selectedTile.x + 0.5f, selectedTile.y + 0.5f), Quaternion.identity);
    spawnedItem.GetComponent<ItemObjectHolder>().item = ItemDirectory.instance.itemDirectory[decor.GetTile<TileBase>(selectedTile).name];
    decor.SetTile(selectedTile, null);
}
void Update()
{
    Vector3 point = Camera.main.ScreenToWorldPoint(Input.mousePosition);

    if (Input.GetMouseButtonDown(0))
    {
        if (point.x - player.transform.position.x < distanceFromPlayer && player.transform.position.x - point.x < distanceFromPlayer && point.y - player.transform.position.y < distanceFromPlayer && player.transform.position.y - point.y < distanceFromPlayer)
        {
            selectedTile = decor.WorldToCell(point);
            if (decor.GetTile(selectedTile) != null)
            {
                BreakDecor();
            }
        }
    }

【问题讨论】:

    标签: c# unity3d photon


    【解决方案1】:

    您可以使用[PunRPC] 告诉其他人将其设置为空。除非你告诉它,否则光子不会知道同步某些东西。 阅读here了解更多信息。

    void BreakDecor()
    {
        GameObject spawnedItem = PhotonNetwork.Instantiate("Item", new Vector3(selectedTile.x + 0.5f, selectedTile.y + 0.5f), Quaternion.identity);
        spawnedItem.GetComponent<ItemObjectHolder>().item = ItemDirectory.instance.itemDirectory[decor.GetTile<TileBase>(selectedTile).name];
        PhotonView.RPC("RPC_DecorBrocken", RpcTarget.All, selectedTile.ID);
    }
    
    [PunRPC]
    private void RPC_DecorBrocken(string _tileID)
    {
        //Set selectedTile from the _tileID
        selectedTile = decor.getTileFromID(_tileID);
        decor.SetTile(selectedTile, null);
    }
    

    【讨论】:

    • 嗯,是的,也不是.. 在 PUN 中同步内容有多种方式.. 使用 RPC 就是其中之一.. 还有例如OnPhotonSerializeView 可能更适合频繁更新!
    • @derHugo 是的,你是对的,我知道我的回答措辞很糟糕。
    • 我的 tilemap 应该也有一个 photonview 组件使用它吗?我应该使用 tielmap.photonview.RPC 还是 player.PhotonVieww.RPC 发送 rpc?
    • @NivMizzer 都可以工作,您只需要有一个系统来确保客户端和主机访问相同的对象。如果 tilemap 处理这种信息,它可能会更有意义。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-10
    • 1970-01-01
    • 1970-01-01
    • 2020-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多