IPunPrefabPool Working Sample with SmartPool(free)

Hi Everyone,

I created a working sample of IPunPrefabPool for effectivly use a pooling system for your network object instances. works from Unity 4.7 and Unity 5.

It's available on github: https://github.com/jeanfabre/Exitgames--PUN--Pooling_u4

You can download it directly here if you don't want to clone the rep.

It's very simple, straight to the point and the main script you are interested in really is this one: PunSmartPoolBridge.cs

It's using a free pooling system called SmartPool but really, once you understand the basics, you can switch to any pooling system.

Watch out for the few things to know:

- You need to keep your prefab inside a Resources folder anyway, It's important for PUN to load that prefab and analyze photonViews to assign viewIds. Note that there are no instantiation, it's only loaded
- On the Prefab instance, use OnPhotonInstantiate() to catch initialization, Start() and OnEnabled() are not suitable for reliable data across the various cases of instantiation and pool usage accross the network.

Let me know if you have questions :)

Bye,

Jean

Comments

  • NomadicWarrior
    edited September 2019
    Hello Mr. Jean. I don't know what is IPunPrefabPool and what it does. I'll try it later. I'm busy right know with FPS project. Right know I'm having a little problem with getting a player list in room. I would like to have a player list inside room until count down numbers will be "0". I'm using Unity's new UI. Could you help me please? I can send you demo project or just explanation. I can show whole code.

    
    
    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine.UI;
    using System.Text;
    
    public class LoobyHook : Photon.MonoBehaviour {
    
    
    	[SerializeField] GameObject MainPanel;
    	[SerializeField] GameObject WaitingPanel;
    	[SerializeField] InputField PlayerName;
    	[SerializeField] GameObject playerInfo;
    	[SerializeField] Transform Hover;
    
    	public  GameObject P1;
    	public  GameObject P2;
    	public  GameObject P3;
    
    
    	public static Transform[] pl;
    	public Vector3[] lp;
    
    	public static int playerID;
    	public int pCount;
    
    	public string roomName = "testRoom";
    
    	private static PhotonView ScenePhotonView;
    
    	void Awake () 
    	{
    		if (PhotonNetwork.connectionState == ConnectionState.Connected) 
    		{
    
    		}
    
    		if (PhotonNetwork.connectionState == ConnectionState.Disconnected) 
    		{
    			MultiplayerConnector.Instance.Connect ();
    		}
    	}
    
    	public void StartMatch ()
    	{
    		if (PhotonNetwork.connected == true) 
    		{
    
    			PhotonNetwork.JoinRandomRoom ();
    
    			MainPanel.SetActive (false);
    			WaitingPanel.SetActive (true);
    		}
    	}
    
    
    	public void OnPhotonRandomJoinFailed ()
    	{
    		PhotonNetwork.CreateRoom (null);
    		CreateRoom();
    
    	}
    
    	public void CreateRoom ()
    	{
    		RoomOptions roomOptions = new RoomOptions() {IsVisible = true, maxPlayers = 10};
    		PhotonNetwork.JoinOrCreateRoom (roomName + System.Guid.NewGuid().ToString("N"), roomOptions, TypedLobby.Default);
    		Debug.Log ("room created" + roomName);
    
    	}
    
    	private bool m_blocalPlayerJoined = false;
    
    	private int joinedPlayer = 0;
    
    	 void OnJoinedRoom ()
    	{
    		if (!m_blocalPlayerJoined) 
    		{
    			UpdatePlayerList ();
    			m_blocalPlayerJoined = true;
    			joinedPlayer = joinedPlayer + 1;
    		}
    
    			playerID = PhotonNetwork.player.ID;
    
    		Debug.Log ("playerID: " + playerID);
    
    	}
    
    	private void UpdatePlayerList ()
    	{
    		foreach (PhotonPlayer player in PhotonNetwork.playerList) 
    		{
    			
    				GameObject temp = (GameObject)Instantiate (playerInfo, lp [0], Quaternion.identity);
    				temp.transform.SetParent (Hover.transform, false);
    				temp.GetComponentInChildren<Text> ().text = "" + player.name;
    
    
    		}
    	}
    
    
    
    	void Update () 
    	{
    		PhotonNetwork.player.name = PlayerName.text;
    
    
    	}
    
    
    
    	/*void OnGUI()
    	{
    		GUILayout.Label (PhotonNetwork.connectionStateDetailed.ToString ());
    
    		foreach (RoomInfo game in PhotonNetwork.GetRoomList()) {
    			if (GUILayout.Button (game.name + " " + game.playerCount + "/" + game.maxPlayers)) {
    				PhotonNetwork.JoinRoom (game.name);
    				MainPanel.SetActive (false);
    				WaitingPanel.SetActive (true);
    				//SpawnPlayerName ();
    
    			}
    
    		}
    
    		foreach (PhotonPlayer pl in PhotonNetwork.playerList) 
    		{
    			GUILayout.Box (pl.name);
    
    		}
    	}*/
    
    }
    
    
  • Hi,

    can you make a dedicated post for this, as your question is off topic, thanks :)

    Bye,

    Jean
  • legend411
    legend411 ✭✭
    edited January 2019
    Hey @jeanfabre ! Sorry for necroing this but I have a quick question. I'm using my own custom pooling solution but the bridge script made it super clear as to what I'm supposed to do...

    but am I correct in assuming that if you use a custom pool (by setting PhotonNetwork.PrefabPool), then you must make sure _all_ prefabs you intend to PhotonNetwork.Instantiate() are set up for pooling in your pooling solution?
  • yes,

    It's your responsability then to make sure you have all the prefabs you need.

    Bye,

    Jean