Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

Third Party [SOLVED] Photon: Instantiating prefabs without putting them in a "Resources" folder?

Discussion in 'Multiplayer' started by Deleted User, Jan 27, 2015.

  1. Deleted User

    Deleted User

    Guest

    How can I PhotonNetwork.Instantiate prefabs that are not located in the root of the "Resources" folder?
     
    marcospgp likes this.
  2. flaminghairball

    flaminghairball

    Joined:
    Jun 12, 2008
    Posts:
    868
    If your folder structure looks like so:

    Assets
    - Resources
    - - Player.prefab

    You instantiate it with:
    PhotonNetwork.Instantiate("Player");

    You can also make your folder structure like this:
    Assets
    - Actors
    - - Player Controlled
    - - - Resources
    - - - - Player.prefab <--- Will still load with the above code, as it's still top-level resources

    Or, you can do this:
    Assets
    - Resources
    - - Actors
    - - - PlayerControlled
    - - - - Player.prefab

    And load it like so:

    PhotonNetwork.Instantiate("Actors/PlayerControlled/Player")
    The forward slashes act as telling Unity to navigate the folder hierarchy inside a resources folder. Note that you can also have multiple resources folders if you so desire.
     
  3. Deleted User

    Deleted User

    Guest

    What if I don't want to have a Resources folder at all?
     
  4. flaminghairball

    flaminghairball

    Joined:
    Jun 12, 2008
    Posts:
    868
  5. Hr-Orange

    Hr-Orange

    Joined:
    Aug 25, 2018
    Posts:
    1
    does anybody have a link that still works or an example of how to spawn without having the resources folder at all?
     
  6. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,013
    PUN 2 is using a PrefabPool to get a GameObject instance. You can write your own but you can also very easily fill the existing pool of type DefaultPool.

    The attached MonoBehaviour has a list of "prefabs", which can be edited in the Inspector. This reference means that the assets don't have to be in the Resources folder. In Start(), the list is used to "fill" the DefaultPool. That's enough.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3. using Photon.Pun;
    4.  
    5.  
    6. public class PreparePool : MonoBehaviour
    7. {
    8.     public List<GameObject> Prefabs;
    9.  
    10.     void Start ()
    11.     {
    12.         DefaultPool pool = PhotonNetwork.PrefabPool as DefaultPool;
    13.         if (pool != null && this.Prefabs != null)
    14.         {
    15.             foreach (GameObject prefab in this.Prefabs)
    16.             {
    17.                 pool.ResourceCache.Add(prefab.name, prefab);
    18.             }
    19.         }
    20.     }
    21. }
     
  7. Psyco92

    Psyco92

    Joined:
    Nov 15, 2013
    Posts:
    22
    Thank you so much for this Tobiass!
     
    tobiass likes this.
  8. Neddymond

    Neddymond

    Joined:
    Oct 17, 2018
    Posts:
    1
    tobiass likes this.
  9. shehanj27

    shehanj27

    Joined:
    Mar 23, 2020
    Posts:
    1
    Greate help...But can you explain how should I instantiate this ResourceCache items
     
  10. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,013
    The ResourceCache can be used by PUN 2, if you assign the pool accordingly.
    When that's setup, you just create networked objects as usual in PUN: PhotonNetwork.Instantiate.
     
  11. darmova

    darmova

    Joined:
    Nov 27, 2019
    Posts:
    10
    Hi, I am having trouble combining IPunPrefabPool with Manual Instantiation (from asset bundles - in my case) as on this page from PUN 2 documentation. In the documentation page, it seems like the manual instantiation is a solution instead of using IPunPrefabPool. Since I no longer use PhotonNetwork.Instantiate, I can't make use of IPunPrefabPool (or at least that seems to be the case based on what the documentation is saying). However, I found this forum, where it said that I should use IPunPrefabPool to instantiate asset bundles - but I don't know how. Any help would be much appreciated.
     
  12. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,013
    Manual Instantiation means you don't call PhotonNetwork.Instantiate. In that case, the pool is also not used.
    You basically got your own code that instantiates / fetches the objects that should be networked. So pooling is also done by that solution...
     
  13. kamlakshksingh

    kamlakshksingh

    Joined:
    Jun 22, 2021
    Posts:
    1
    Iam not able to use pun in unity 2021. It says ' There is no argument given that corresponds to the required formal parameter 'gameVersion' of 'PhotonNetwork.ConnectUsingSettings(string)'. Plesase help!!
     
  14. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,013
    You need to use PUN 2, not PUN Classic.
     
  15. mariappannainar10

    mariappannainar10

    Joined:
    Mar 28, 2022
    Posts:
    1
    Should I implement IPunPrefabPool to spawn networked objects or just use PhotonNetwork.Instantiate
    ?
     
  16. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,013
  17. LlGHTER

    LlGHTER

    Joined:
    May 1, 2018
    Posts:
    1
    Hey guys, I found a great way that helped me with this problem. Solution
     
  18. Ravikiran1435

    Ravikiran1435

    Joined:
    Dec 9, 2019
    Posts:
    1
    Where can I paste the above code, in the DefaultPool class or Should I need to make a separate CS File. Kindly explain me more.