Unity c# Photon - 设置 PhotonNetwork.Instantiate 对象的父级
Unity c# Photon - Set Parent of PhotonNetwork.Instantiate object
我想做的是设置新工具的父级并使其在服务器上可见,例如 'PhotonNetwork.Instantiate'
var drop = PhotonNetwork.Instantiate("Tools/Weapons/" + Tool.name, transform.position, Quaternion.identity);
drop.transform.SetParent(collider.gameObject.transform.GetChild(0).GetChild(0));
使用drop.transform.SetParent只对本地玩家可见
如有任何问题,请提问:)
我认为这段代码
drop.transform.SetParent(collider.gameObject.transform.GetChild(0).GetChild(0));
不会在任何其他客户端或服务器中执行。所以我会创建一个附加到对象的脚本,以便在创建对象时设置父级;
using UnityEngine;
public class SetParent : MonoBehaviour
{
public GameObject Tool;
// Start is called before the first frame update
void Awake()
{
gameObject.transform.parent = FindParent();
}
private Transform FindParent()
{
Transform parent;
//Find object should be parent
return parent;
}
}
示例:为父对象“Parent”设置标签。然后可以通过这段代码找到对象:parent = GameObject.FindGameObjectWithTag("Parent").transform;
Photon 希望您使用的正确方法是使用“自定义实例化数据”
https://doc.photonengine.com/en-us/pun/current/gameplay/instantiation
例如...
//1) The script where we you instantiate the object will pass the relevant data (The parent View ID):
int parentViewID = PhotonView.Find(this.gameObject.getComponent<PhotonView>().ViewID);
string word = "example";
object[] myCustomInitData = new object[3];
myCustomInitData[0] = parentViewID;
myCustomInitData[1] = word;
PhotonNetwork.Instantiate(Path.Combine("Players", "Player" transform.position, Quaternion.identity, 0, myCustomInitData);
//_______________________________________________________________________
//2) The script that attached to our new Instantiate object:
//Make sure the prefab has PhotonView component on it !
using Photon.Pun;
public class RandomCall : MonoBehaviourPun, IPunInstantiateMagicCallback
{
GameObject parent;
public void OnPhotonInstantiate(PhotonMessageInfo info)
{
object[] instantiationData = info.photonView.InstantiationData;
parent = PhotonView.Find((int)this.customInstantiateData[0]).gameObject;
string word = (string)instantiationData[1];
this.transform.setParent(parentObject)
}
}
我想做的是设置新工具的父级并使其在服务器上可见,例如 'PhotonNetwork.Instantiate'
var drop = PhotonNetwork.Instantiate("Tools/Weapons/" + Tool.name, transform.position, Quaternion.identity);
drop.transform.SetParent(collider.gameObject.transform.GetChild(0).GetChild(0));
使用drop.transform.SetParent只对本地玩家可见
如有任何问题,请提问:)
我认为这段代码 drop.transform.SetParent(collider.gameObject.transform.GetChild(0).GetChild(0)); 不会在任何其他客户端或服务器中执行。所以我会创建一个附加到对象的脚本,以便在创建对象时设置父级;
using UnityEngine;
public class SetParent : MonoBehaviour
{
public GameObject Tool;
// Start is called before the first frame update
void Awake()
{
gameObject.transform.parent = FindParent();
}
private Transform FindParent()
{
Transform parent;
//Find object should be parent
return parent;
}
}
示例:为父对象“Parent”设置标签。然后可以通过这段代码找到对象:parent = GameObject.FindGameObjectWithTag("Parent").transform;
Photon 希望您使用的正确方法是使用“自定义实例化数据” https://doc.photonengine.com/en-us/pun/current/gameplay/instantiation
例如...
//1) The script where we you instantiate the object will pass the relevant data (The parent View ID):
int parentViewID = PhotonView.Find(this.gameObject.getComponent<PhotonView>().ViewID);
string word = "example";
object[] myCustomInitData = new object[3];
myCustomInitData[0] = parentViewID;
myCustomInitData[1] = word;
PhotonNetwork.Instantiate(Path.Combine("Players", "Player" transform.position, Quaternion.identity, 0, myCustomInitData);
//_______________________________________________________________________
//2) The script that attached to our new Instantiate object:
//Make sure the prefab has PhotonView component on it !
using Photon.Pun;
public class RandomCall : MonoBehaviourPun, IPunInstantiateMagicCallback
{
GameObject parent;
public void OnPhotonInstantiate(PhotonMessageInfo info)
{
object[] instantiationData = info.photonView.InstantiationData;
parent = PhotonView.Find((int)this.customInstantiateData[0]).gameObject;
string word = (string)instantiationData[1];
this.transform.setParent(parentObject)
}
}