🎮
5-1 synchronization
变换同步 动画同步 刚体同步 甚至同步一些自定义数据
用到的组件是预制体上的photon view组件 将
observable 改成manual
然后将自己的transform拖进observed components内
(同时会自动添加一个photon transform view组件 这个是网络同步的核心)
运行演示效果 在unity里修改坐标 头显里可以看到运动
但用手柄控制头显里的,在unity端看不到运动
👇
5-2 photon官方提供的脚本
MultiplayerVRSynchronization.cs
🚪下载地址https://www.aliyundrive.com/s/DP1p7TLz5Su
移除photon transform view
挂载 MultiplayerVRSynchronization.cs
将MultiplayerVRSynchronization组件拖拽进 photon view 的observed components内
以及其他的公共变量
5-3 网络抓取
抓取一个物体能被大家看到
主要用的是 交互脚本上的event 告诉其他玩家该物体被抓到
写好触发事件之后
给触发物体添加一个photon view 同样设置👇,并把transform拖进
但是演示出现了一些震动
5-4 解决震动
首先应当转移对象所有权 配置photon view
ownership transfer 改成 request
然后在NetworkedGrabbing.cs脚本中 创建一个私有方法,用以创建转移权
👇完整代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| using System; using System.Collections; using System.Collections.Generic; using Photon.Pun; using Photon.Realtime; using UnityEngine;
public class NetworkedGrabbing : MonoBehaviourPunCallbacks,IPunOwnershipCallbacks { private PhotonView m_photonView;
private void Awake() { m_photonView = GetComponent<PhotonView>(); }
void Start() { }
void Update() { }
private void TransferOwnership() { m_photonView.RequestOwnership(); } public void OnSelectEntered() { Debug.Log("Grabbed"); TransferOwnership(); }
public void OnSelectedExited() { }
public void OnOwnershipRequest(PhotonView targetView, Player requestingPlayer) { Debug.Log("Ownership Requested for:"+ targetView.name+"from"+requestingPlayer.NickName); m_photonView.TransferOwnership(requestingPlayer); }
public void OnOwnershipTransfered(PhotonView targetView, Player previousOwner) { Debug.Log("Ownership Tranferred to:"+ targetView.name+"from"+previousOwner.NickName); }
public void OnOwnershipTransferFailed(PhotonView targetView, Player senderOfFailedRequest) { } }
|
5-5 继续改进 (RPC)
在NetworkedGrabbing.cs 创建一个公共方法
完整代码👇
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| using System; using System.Collections; using System.Collections.Generic; using Photon.Pun; using Photon.Realtime; using UnityEngine;
public class NetworkedGrabbing : MonoBehaviourPunCallbacks, IPunOwnershipCallbacks { private PhotonView m_photonView; private Rigidbody rb; private bool isBeingHeld; private void Awake() { m_photonView = GetComponent<PhotonView>(); }
void Start() { rb = GetComponent<Rigidbody>(); }
void Update() { if (isBeingHeld) { rb.isKinematic = true; gameObject.layer = 11; } else { rb.isKinematic = false; gameObject.layer = 9; } }
private void TransferOwnership() { m_photonView.RequestOwnership(); }
public void OnSelectEntered() { Debug.Log("Grabbed"); m_photonView.RPC("StartNetworkGrabbing", RpcTarget.AllBuffered); if (m_photonView.Owner == PhotonNetwork.LocalPlayer) { Debug.Log("意味真的拿住了"); } else { TransferOwnership(); }
}
public void OnSelectedExited() { Debug.Log("Released"); m_photonView.RPC("StopNetworkGrabbing", RpcTarget.AllBuffered); }
public void OnOwnershipRequest(PhotonView targetView, Player requestingPlayer) { if (targetView != m_photonView) { return; } Debug.Log("Ownership Requested for:" + targetView.name + "from" + requestingPlayer.NickName); m_photonView.TransferOwnership(requestingPlayer); }
public void OnOwnershipTransfered(PhotonView targetView, Player previousOwner) { Debug.Log("Ownership Tranferred to:" + targetView.name + "from" + previousOwner.NickName); }
public void OnOwnershipTransferFailed(PhotonView targetView, Player senderOfFailedRequest) { }
[PunRPC] public void StartNetworkGrabbing() { isBeingHeld = true; }
[PunRPC] public void StopNetworkGrabbing() { isBeingHeld = false; } }
|