Skip to content

Instantly share code, notes, and snippets.

@cnsoft
Created November 15, 2013 10:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cnsoft/7482208 to your computer and use it in GitHub Desktop.
Save cnsoft/7482208 to your computer and use it in GitHub Desktop.
Auto Enter Room Viking Demo!!
using UnityEngine;
using System.Collections;
public class MainMenuVik : MonoBehaviour
{
void Awake()
{
//PhotonNetwork.logLevel = NetworkLogLevel.Full;
//Connect to the main photon server. This is the only IP and port we ever need to set(!)
if (!PhotonNetwork.connected)
PhotonNetwork.ConnectUsingSettings("v1.0"); // version of the game/demo. used to separate older clients from newer ones (e.g. if incompatible)
//Load name from PlayerPrefs
PhotonNetwork.playerName = PlayerPrefs.GetString("playerName", "Guest" + Random.Range(1, 9999));
//Set camera clipping for nicer "main menu" background
Camera.main.farClipPlane = Camera.main.nearClipPlane + 0.1f;
}
private string roomName = "eCommerceDemo_gx";
private Vector2 scrollPos = Vector2.zero;
private string _roomStatus = "empty";
private float _lastTick = 0;
bool checkTimeOut()
{
if (_lastTick ==0 )
{
_lastTick = Time.realtimeSinceStartup;
return false;
}
if (( Time.realtimeSinceStartup - _lastTick ) > 1)
return true;
return false;
}
/// <summary>
/// Autos the enter room.
/// </summary>
/// <param name='created'>
/// Created if not find rooms.
/// </param>
void AutoEnterRoom(bool created)
{
//first get rooms if not inRoom.
//not find rooms ,create it..(auto inRoom then)
//find room,join it. hide ui.
if (_roomStatus == "empty")
{
//_roomStatus = "Find room";
if (PhotonNetwork.GetRoomList().Length == 0)
{
Debug.Log("..no games available..");
//GUILayout.Label("..create room ..");
if ( checkTimeOut())//||GUILayout.Button("GO")) //why we must click it to create? for waiting connected ?
{
PhotonNetwork.CreateRoom(roomName,true, true, 20);
_roomStatus = "Find room";
}//
}
else
{
Debug.Log("..enter room ..");
PhotonNetwork.JoinRoom(roomName);
_roomStatus = "inroom";
}
}//end if.
this.ShowPromptGUI();
}
void OnGUI()
{
if (!PhotonNetwork.connected)
{
ShowConnectingGUI();
return; //Wait for a connection
}
if (PhotonNetwork.room != null)
return;
AutoEnterRoom(true);//enter room or created first.
//_roomStatus = "inRoom";
/* GUILayout.BeginArea(new Rect((Screen.width - 400) / 2, (Screen.height - 300) / 2, 400, 300));
GUILayout.Label("Main Menu");
//Player name
GUILayout.BeginHorizontal();
GUILayout.Label("Player name:", GUILayout.Width(150));
PhotonNetwork.playerName = GUILayout.TextField(PhotonNetwork.playerName);
if (GUI.changed)//Save name
PlayerPrefs.SetString("playerName", PhotonNetwork.playerName);
GUILayout.EndHorizontal();
GUILayout.Space(15);
//Join room by title
GUILayout.BeginHorizontal();
GUILayout.Label("JOIN ROOM:", GUILayout.Width(150));
roomName = GUILayout.TextField(roomName);
if (GUILayout.Button("GO"))
{
PhotonNetwork.JoinRoom(roomName);
}
GUILayout.EndHorizontal();
//Create a room (fails if exist!)
GUILayout.BeginHorizontal();
GUILayout.Label("CREATE ROOM:", GUILayout.Width(150));
roomName = GUILayout.TextField(roomName);
if (GUILayout.Button("GO"))
{
PhotonNetwork.CreateRoom(roomName, true, true, 10);
}
GUILayout.EndHorizontal();
//Join random room
GUILayout.BeginHorizontal();
GUILayout.Label("JOIN RANDOM ROOM:", GUILayout.Width(150));
if (PhotonNetwork.GetRoomList().Length == 0)
{
GUILayout.Label("..no games available...");
}
else
{
if (GUILayout.Button("GO"))
{
PhotonNetwork.JoinRandomRoom();
}
}
GUILayout.EndHorizontal();
GUILayout.Space(30);
GUILayout.Label("ROOM LISTING:");
if (PhotonNetwork.GetRoomList().Length == 0)
{
GUILayout.Label("..no games available..");
}
else
{
//Room listing: simply call GetRoomList: no need to fetch/poll whatever!
scrollPos = GUILayout.BeginScrollView(scrollPos);
foreach (RoomInfo game in PhotonNetwork.GetRoomList())
{
GUILayout.BeginHorizontal();
GUILayout.Label(game.name + " " + game.playerCount + "/" + game.maxPlayers);
if (GUILayout.Button("JOIN"))
{
PhotonNetwork.JoinRoom(game.name);
}
GUILayout.EndHorizontal();
}
GUILayout.EndScrollView();
}
GUILayout.EndArea();
*/
}
void ShowPromptGUI()
{
GUILayout.BeginArea(new Rect((Screen.width - 400) / 2, (Screen.height - 300) / 2, 400, 300));
GUILayout.Label(_roomStatus);
GUILayout.Label("Hint: This demo uses a settings file and logs the server address to the console.");
GUILayout.EndArea();
}
void ShowConnectingGUI()
{
GUILayout.BeginArea(new Rect((Screen.width - 400) / 2, (Screen.height - 300) / 2, 400, 300));
GUILayout.Label("Connecting to Photon server.");
GUILayout.Label("Hint: This demo uses a settings file and logs the server address to the console.");
GUILayout.EndArea();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment