PhotonNetwork.InstantiateSceneObject

Options
Hello,

I have a prefab inside an assetBundle that im trying to instantiate using PhotonNetwork.InstantiateSceneObject

How do I use PhotonNetwork.InstantiateSceneObject to instantiate a prefab that is inside an asset bundle?

For example here is existing code:

AssetBundle ab=AssetBundle.LoadFromFile("c:\assets\bundlename.android");
var prefab=ab.LoadAsset("networked_monster.prefab");

PhotonNetwork.InstantiateSceneObject(???????);

Thanks

Comments

  • jeanfabre
    Options
    Hi,

    I am assuming you are on PUN 2./

    Pun usually load network prefabs by the name of the prefab, which must be in a Resources folder.

    So in this case I don't think it will work, unless asset bundles can generate content to be inside a Resources folder. in which case it's fine.

    otherwise, you'll need to implement the IPunPrefabPool interface and then you can decide how to instantiate the prefab and where you can get it.

    Please check the default implementation ( DefaultPool class in punclasses.cs) for an example.

    Bye,

    Jean
  • Baja
    Options
    jeanfabre wrote: »
    Hi,

    I am assuming you are on PUN 2./

    Pun usually load network prefabs by the name of the prefab, which must be in a Resources folder.

    So in this case I don't think it will work, unless asset bundles can generate content to be inside a Resources folder. in which case it's fine.

    otherwise, you'll need to implement the IPunPrefabPool interface and then you can decide how to instantiate the prefab and where you can get it.

    Please check the default implementation ( DefaultPool class in punclasses.cs) for an example.

    Bye,

    Jean

    Could you please elaborate?
  • Hi,

    In order to take control over what you instantiate over the network, you need to implement your own IPunPrefabPool class. This IPunPrefabPool interface is actually used by pun itself to load prefabs from resources, so look like the DefaultPool.cs class in the pun code and check how this class is used, to understand how IPunPrefabPool interface works. Create your own implemenation, that instantiate the prefab coming from the asset bundle.

    Bye,

    Jean
  • MorningStarSanty
    edited January 2021
    Options
    Hi ,
    you can achieve this in both pun1 and pun2 without using their ipunprefabpool. I have done this on my project..


    call => PN.Instantiate(prefab.name, lol,lol,lol, prefab);


    in photonNetwork.cs

    public GO objTOInstantiate ;
    public static GameObject Instantiate(string prefabName, Vector3 position, Quaternion rotation, byte group, GameObject prefab = null)
    {
    objTOInstantiate = prefab;
    return Instantiate(prefabName, position, rotation, group, null,prefab);
    }


    2.
    public static GameObject Instantiate(string prefabName, Vector3 position, Quaternion rotation, byte group, object[] data, GameObject prefab)
    {
    objTOInstantiate = prefab;
    if (!connected || (InstantiateInRoomOnly && !inRoom))
    {
    Debug.LogError("Failed to Instantiate prefab: " + prefabName + ". Client should be in a room. Current connectionStateDetailed: " + PhotonNetwork.connectionStateDetailed);
    return null;
    }

    ...
    ...
    ...

    etc....

    and now you will get few errors , just pass this gameobj at last parameter in other methods as well ... and you will find one line where its loading from Resources folder:
    prefabGo = (GameObject)Resources.Load(prefabName, typeof(GameObject));

    just comment this line and pass your prefab which you have in parameter.

    prefabGo = prefab;

    And same thing to do with PN.InstantiateSceneObject();

    Hope you got an idea how to do it. And it works with assetbundle as well. pass the GO from inspector to your public GO variable and use PN.Instantiate(go.name,,,); And during assetbundle making process it will take the reference of go itself.