Simple Example to begin with Photon PUN multiplayer game development

This simple script is to connect the game to Photon server and instantiate a player prefab on your Resources folder. Make sure your player prefab has PhotonView component in it.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;

public class SimpleLauncher : MonoBehaviourPunCallbacks
{
	
	public PhotonView playerPrefab;
	
    // Start is called before the first frame update
    void Start()
    {
        PhotonNetwork.ConnectUsingSettings();
    }

	public override void OnConnectedToMaster(){
		Debug.Log("Connected to Master");
		PhotonNetwork.JoinRandomOrCreateRoom();
	}
	
	public override void OnJoinedRoom(){
		Debug.Log("Joined a room.");
		PhotonNetwork.Instantiate(playerPrefab.name, Vector3.zero, Quaternion.identity);
	}
	
}

Check out this video on how to use it:

Ready to develop more? Watch out the next steps of what we can do with Photon PUN 2 in this video tutorial playlist: https://www.youtube.com/playlist?list=PLtZUQWWVMRK3IKODhhxo-WBzTeIgWTszW

Leave a Reply

Your email address will not be published. Required fields are marked *