本文主要是介绍Unity问题与解决方案:Photon实例化(PhotonNetwork.Instantiate参数详解),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Photon不知道为什么文档是真的少…只能自己读源码了,先来看一下这个方法声明吧:
/// <summary>
/// Instantiate a prefab over the network. This prefab needs to be located in the root of a "Resources" folder.
/// </summary>
/// <remarks>Instead of using prefabs in the Resources folder, you can manually Instantiate and assign PhotonViews. See doc.</remarks>
/// <param name="prefabName">Name of the prefab to instantiate.</param>
/// <param name="position">Position Vector3 to apply on instantiation.</param>
/// <param name="rotation">Rotation Quaternion to apply on instantiation.</param>
/// <param name="group">The group for this PhotonView.</param>
/// <param name="data">Optional instantiation data. This will be saved to it's PhotonView.instantiationData.</param>
/// <returns>The new instance of a GameObject with initialized PhotonView.</returns>
public static GameObject Instantiate(string prefabName, Vector3 position, Quaternion rotation, byte group, object[] data)
参数 | 说明 |
---|---|
prefabName | prefab的名字,要求该预制物必须在一个Resources文件夹的根目录下,而且该预制物必须附有PhotonView 脚本 |
position | 实例化的位置 |
rotation | 实例化的角度 |
group | 群组,好像可以限制只对某一些客户端实例化,这个我还没有搞明白,,一般都填0 |
data | 实例化时候传递的数据,该值保存在PhotonView.instantiationData 变量中,要注意传递的每一个Object必须是可序列化的,否则会报错,该值可以不填,默认为null |
于是一些初始化的变量就可以在data中传递过去
这里再附上官方Demo中推荐的每个GameObject的PhotonView的获取方式:
private PhotonView pvCache = null;public PhotonView photonView{get{if (pvCache == null){pvCache = PhotonView.Get(this);}return pvCache;}}
这篇关于Unity问题与解决方案:Photon实例化(PhotonNetwork.Instantiate参数详解)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!