Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- * Thomas Elvey & Exit Games ~ Photon
- * Based of Hello World 1
- * Photon Connect & Chat
- *
- **/
- using UnityEngine;
- using System.Collections;
- using ExitGames.Client.Photon;
- /*
- * Extend both Unity's MonoBehaviour and Photon's IPhotonPeerListener *
- */
- public class PhotonConnect : MonoBehaviour, IPhotonPeerListener
- {
- #region Members/Variables
- protected LitePeer Peer; //Define LitePeer
- public string serverAddress = "locahost:5055"; //Photon Server Address
- public string serverApplication = "Lite"; //Photon Application
- public string chatText; //Text shown in the chat box
- public string consoleText; //Text shown in the consolebox
- string chatLine = string.Empty; //The string sent to other users
- bool connected2Photon; //Are we connected to Photon?
- enum chatEventCode : byte { Message = 50 } //The chat message event
- enum chatEventKey : byte { PlayerMessage = 150 } //The key of the chat event code so we know where the message is in the hashtable
- #endregion
- #region Start, Update, GUI
- /*
- * Use this for initialization
- */
- void Start()
- {
- Peer = new LitePeer(this); //Add LitePeer
- }
- /*
- * Update is called once per frame
- */
- void Update()
- {
- Peer.Service(); //Call the Photon Service to update
- }
- /*
- * When the game/unity application closes this is called
- */
- void OnApplicationQuit()
- {
- Peer.Disconnect(); //Kill the connection to photon
- }
- /*
- * GUI routine
- */
- void OnGUI()
- {
- if (connected2Photon) //Are we connceted to Photon? We are!
- {
- chatLine = GUI.TextField(new Rect(210, 0, 100, 20), chatLine); //Define the input box which we will send out messages
- GUI.TextArea(new Rect(0, 100, 300, 300), chatText); //Define the chat area which chat messages will be displayed in
- if (GUI.Button(new Rect(310, 0, 100, 20), "Send Message")) //Send message button which will send the message of the string "chatLine"
- {
- sendChatMessage(chatLine); //Pass the string chatLine into the sendChatMessage subroutine, which will then pass to photon
- chatText += ("- I said: " + chatLine + "\n"); //Add the chat message to our screen locally, otherwise we will not see it
- chatLine = string.Empty; //Clear the string so it is ready to use again
- }
- if (GUI.Button(new Rect(0, 0, 100, 20), "Disconnect")) //Define a disconnect button
- {
- Peer.Disconnect(); //Kill the conenction to photon
- }
- }
- else //We must not be conencted to photon
- {
- if (GUI.Button(new Rect(0, 0, 100, 20), "Connect")) //Show a connect button
- {
- Peer.Connect(serverAddress, serverApplication); //Connected to photon using the correct address and application
- }
- }
- GUI.TextArea(new Rect(400, 100, 300, 300), consoleText); //Define a textarea to use as a console
- }
- #endregion
- #region Custom Subroutines
- /*
- * subRoutine to handle chat messages
- */
- public void sendChatMessage(string cLine) //Define the string passed arugment as cLine
- {
- Hashtable eventContent = new Hashtable(); //Create a new hashtable which will be sent to the photon sevrer
- eventContent.Add((byte)chatEventKey.PlayerMessage, cLine); //Add the string cLine to the hashtable, with the correct eventkey so it can be extracted correctly on the other clients
- Peer.OpRaiseEvent((byte)chatEventCode.Message, eventContent, true); //Raise the event with photon sending the hashtable and witht he cirrect event code so you know how to handle the hastable on the other end
- }
- #endregion
- #region IPhotonPeerListener Members
- /*
- * Routines from IPhotonPeerListener
- */
- public void DebugReturn(DebugLevel level, string message)
- {
- //throw new NotImplementedException();
- }
- //Controls the recieving of Events
- public void EventAction(byte eventCode, System.Collections.Hashtable photonEvent)
- {
- consoleText += ("-- EventAction: " + (LiteEventCode)eventCode + "(" + eventCode + ")"); //Add debug information to the console
- switch(eventCode) //When an event is recieved, unity needs to know what to do with it :D
- {
- case (byte)chatEventCode.Message: //Is the event a chatMessage?
- int sourceActorNr = (int)photonEvent[(byte)LiteEventKey.ActorNr]; //get the senders number
- Hashtable evData = (Hashtable)photonEvent[(byte)LiteEventKey.Data]; //define a hashtable and grab the hashtable from the photon packet(s)
- chatText += ("- Player " + sourceActorNr + " say's: " + evData[(byte)chatEventKey.PlayerMessage] + "\n"); //Add a chat message to the chat text
- break;
- }
- }
- //Control the recieving of Operations
- public void OperationResult(byte opCode, int returnCode, System.Collections.Hashtable returnValues, short invocID)
- {
- if(returnCode == 0)
- consoleText += ("-- Op Result: OK - " + (LiteOpCode)opCode + "(" + opCode + ")"); //Debuging information
- else
- {
- consoleText += ("-- Op Result: NOT OK - " + (LiteOpCode)opCode + "(" + opCode + ") ->returnCode=" + returnCode + " DebugString=" + returnValues[(byte)LiteOpKey.DebugString]);
- return;
- }
- switch (opCode) //When an operation is recieved, unity needs to know what to do with it :D
- {
- case (byte)LiteOpCode.Join: //We joined a "Lite" room
- int myActorNr = (int)returnValues[(byte)LiteOpKey.ActorNr]; //Get YOUR actor number
- consoleText += ("-- My PlayerNr is: " + myActorNr); //print your actors number
- break;
- }
- }
- //Controls photon base communication
- public void PeerStatusCallback(StatusCode statusCode)
- {
- //consoleText += ("PeerStatusCallback:" + statusCode.ToString());
- switch(statusCode)
- {
- case StatusCode.Connect: //Did we connected to photon?
- consoleText += ("Connected to Photon Server!"); //We did connect to photon!
- Hashtable opParams = new Hashtable(); //We now need to send an operation to join a room, do we make a new hashtable
- opParams[(byte)LiteOpKey.RoomName] = "Room"; //We place the room name we wish to join in the hashtable
- Peer.OpCustom((byte)LiteOpCode.Join, opParams, true); //we sent the hashtable
- connected2Photon = true; //We are now connected :D
- break;
- case StatusCode.Disconnect: //We disconnected from Photom :(
- consoleText += ("Disconnected from Photon Server!"); //Print
- connected2Photon = false; //We are not connected
- break;
- default:
- consoleText += ("StatusCode not handled ");
- break;
- }
- }
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement