简体   繁体   English

如何在 Unity 中将画布与 Photon Engine 同步

[英]How to synchronized a canvas with Photon Engine in Unity

I'm trying to synchronize a canvas with photon engine so every player can see it.我正在尝试将画布与光子引擎同步,以便每个玩家都能看到它。 This canvas will be kind of a tv that any player can turn on and the rest can watch it.这个画布将是一种任何玩家都可以打开而其他人可以观看的电视。 I could synchronized a cube adding the PhotonView and the PhotonRigidBody components to the prefab but when I tried the same with the canvas it didn't work at all.我可以同步一个立方体,将 PhotonView 和 PhotonRigidBody 组件添加到预制件中,但是当我在画布上尝试相同时,它根本不起作用。 Can anyone tell me what components are required to do this and if it needed it what should I handle with an extra script (ie transfer ownership).谁能告诉我需要哪些组件才能做到这一点,如果需要它,我应该用额外的脚本处理什么(即转移所有权)。

There is nothing special about the canvas, but it could be locked in place.画布没有什么特别之处,但它可以锁定到位。

There are two solutions I have for you:我为您提供了两种解决方案:

Observable Component : 可观察组件

You could write a custom observable component, and add it to the PhotonView:您可以编写一个自定义 observable 组件,并将其添加到 PhotonView:

To make use of this function, the script has to implement the IPunObservable interface.为了使用这个函数,脚本必须实现 IPunObservable 接口。

public class CustomObservable : MonoBehaviourPunCallbacks, IPunObservable
{
    [SerializeField] PlayerController playerController;

    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
        if (stream.IsWriting)
        {
            stream.SendNext(playerController.playerNumber);
            stream.SendNext(playerController.playerScore);
        }
        else
        {
            playerController.playerNumber = (int)stream.ReceiveNext();
            playerController.playerScore = (float)stream.ReceiveNext();
        }
    }
}

Custom Properties : 自定义属性

You could also use custom properties to sync the data across all players.您还可以使用自定义属性在所有玩家之间同步数据。

Photon's Custom Properties consist of a key-values Hashtable which you can fill on demand. Photon 的自定义属性由一个键值哈希表组成,您可以按需填写。 The values are synced and cached on the clients, so you don't have to fetch them before use.这些值同步并缓存在客户端上,因此您不必在使用前获取它们。 Changes are pushed to the others by SetCustomProperties(). SetCustomProperties() 将更改推送给其他人。

How is this useful?这有什么用? Typically, rooms and players have some attributes that are not related to a GameObject: The current map or the color of a player's character (think: 2d jump and run).通常,房间和玩家有一些与 GameObject 无关的属性:当前地图或玩家角色的颜色(想想:2d 跳跃和奔跑)。 Those can be sent via Object Synchronization or RPC, but it is often more convenient to use Custom Properties.这些可以通过对象同步或 RPC 发送,但使用自定义属性通常更方便。

PhotonNetwork.CurrentRoom.SetCustomProperties(Hashtable propsToSet)

You can write a script that uses Photons callback, and updated the UI elements.您可以编写一个使用 Photons 回调的脚本,并更新 UI 元素。

OnRoomPropertiesUpdate(Hashtable propertiesThatChanged)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM