How to use IPunPrefabPool

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

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).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

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

kk99
2020-11-05 08:35:33

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