How to use IPunPrefabPool
The whole answer can be found below.
Try Our
Documentation
Please check if you can find an answer in our extensive documentation on PUN.
Join Us
on Discord
Meet and talk to our staff and the entire Photon-Community via Discord.
Read More on
Stack Overflow
Find more information on Stack Overflow (for Circle members only).
How to use IPunPrefabPool
kk99
2020-11-04 23:46:16
Hello,
i am trying to use IPunPrefabPool to instantiate objects from AssetBundles,
but so far it is not working (other players cant see anything, the function is not triggered)
public class PhotonCustomPool : MonoBehaviour, IPunPrefabPool {
public void Destroy(GameObject gameObject) {
}
public GameObject Instantiate(string prefabId, Vector3 position, Quaternion rotation) {
AssetBundle bundle = StreamingAssetManager.LoadAsset(prefabId);
GameObject pet2 = null;
if (bundle) {
var loadAsset = new AssetBundleRequest();
foreach (var assetBundleName in bundle.GetAllAssetNames()) {
pet2 = bundle.LoadAsset<GameObject>(assetBundleName);
if (pet2 != null) break;
}
pet2 = UnityEngine.Object.Instantiate(pet2) as GameObject;
bundle.Unload(false);
}
return pet2;
}
}
Some other Class
public class PlayerNetwork_Pets : MonoBehaviour
{
public PhotonCustomPool photonCustomPool;
public void makePet() {
GameObject pet2 = photonCustomPool.Instantiate("1", new Vector3(), Quaternion.Euler(new Vector3()));
}
}
any help or hint would be appreciated
Comments
After hours or trying I found a work around
https://doc.photonengine.com/en-us/pun/v1/gameplay/instantiation
void SpawnPlayerEverywhere()
{
// You must be in a Room already
// Manually allocate PhotonViewID
int id1 = PhotonNetwork.AllocateViewID();
PhotonView photonView = this.GetComponent<PhotonView>();
photonView.RPC("SpawnOnNetwork", PhotonTargets.AllBuffered, transform.position, transform.rotation, id1);
}
public Transform playerPrefab; //set this in the inspector
[PunRPC]
void SpawnOnNetwork(Vector3 pos, Quaternion rot, int id1)
{
Transform newPlayer = Instantiate(playerPrefab, pos, rot) as Transform;
// Set player's PhotonView
PhotonView[] nViews = newPlayer.GetComponentsInChildren<PhotonView>();
nViews[0].viewID = id1;
}
That seems to be much easier =)
I believe the previous code could work if I used
private void Awake() {
PhotonNetwork.PrefabPool = photonCustomPool;
}
public void makePet() {
GameObject pet2 = PhotonNetwork.Instantiate("1", new Vector3(), Quaternion.Euler(new Vector3()));
}
However, this would screw up my existing code because it looks like there can be only one Pool in Photon.
Back to top