Advertisement
Guest User

Photon Chat Room

a guest
Jul 14th, 2011
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.37 KB | None | 0 0
  1. /**
  2.  *
  3.  * Thomas Elvey & Exit Games ~ Photon
  4.  * Based of Hello World 1
  5.  * Photon Connect & Chat
  6.  *
  7.  **/
  8. using UnityEngine;
  9. using System.Collections;
  10. using ExitGames.Client.Photon;
  11.  
  12. /*
  13.  * Extend both Unity's MonoBehaviour and Photon's IPhotonPeerListener *
  14.  */
  15. public class PhotonConnect : MonoBehaviour, IPhotonPeerListener
  16. {
  17.     #region Members/Variables  
  18.    
  19.     protected LitePeer Peer; //Define LitePeer
  20.  
  21.     public string serverAddress = "locahost:5055"; //Photon Server Address
  22.     public string serverApplication = "Lite"; //Photon Application
  23.  
  24.     public string chatText; //Text shown in the chat box
  25.     public string consoleText; //Text shown in the consolebox
  26.    
  27.     string chatLine = string.Empty;  //The string sent to other users
  28.    
  29.     bool connected2Photon; //Are we connected to Photon?
  30.    
  31.     enum chatEventCode : byte { Message = 50 } //The chat message event
  32.     enum chatEventKey : byte { PlayerMessage = 150 } //The key of the chat event code so we know where the message is in the hashtable
  33.  
  34.     #endregion
  35.    
  36.     #region Start, Update, GUI
  37.  
  38.     /*
  39.      * Use this for initialization
  40.      */
  41.     void Start()
  42.     {
  43.         Peer = new LitePeer(this); //Add LitePeer
  44.     }
  45.  
  46.     /*
  47.      * Update is called once per frame
  48.      */
  49.     void Update()
  50.     {
  51.         Peer.Service(); //Call the Photon Service to update
  52.     }
  53.  
  54.     /*
  55.      * When the game/unity application closes this is called
  56.      */
  57.     void OnApplicationQuit()
  58.     {
  59.         Peer.Disconnect(); //Kill the connection to photon
  60.     }
  61.  
  62.     /*
  63.      * GUI routine
  64.      */
  65.     void OnGUI()
  66.     {
  67.         if (connected2Photon) //Are we connceted to Photon? We are!
  68.         {
  69.             chatLine = GUI.TextField(new Rect(210, 0, 100, 20), chatLine); //Define the input box which we will send out messages  
  70.  
  71.             GUI.TextArea(new Rect(0, 100, 300, 300), chatText); //Define the chat area which chat messages will be displayed in
  72.  
  73.             if (GUI.Button(new Rect(310, 0, 100, 20), "Send Message")) //Send message button which will send the message of the string "chatLine"
  74.             {
  75.                 sendChatMessage(chatLine); //Pass the string chatLine into the sendChatMessage subroutine, which will then pass to photon
  76.  
  77.                 chatText += ("- I said: " + chatLine + "\n"); //Add the chat message to our screen locally, otherwise we will not see it
  78.                 chatLine = string.Empty; //Clear the string so it is ready to use again
  79.             }
  80.  
  81.             if (GUI.Button(new Rect(0, 0, 100, 20), "Disconnect")) //Define a disconnect button
  82.             {
  83.                 Peer.Disconnect(); //Kill the conenction to photon
  84.             }
  85.         }
  86.         else //We must not be conencted to photon
  87.         {
  88.             if (GUI.Button(new Rect(0, 0, 100, 20), "Connect")) //Show a connect button
  89.             {
  90.                 Peer.Connect(serverAddress, serverApplication); //Connected to photon using the correct address and application
  91.             }      
  92.         }
  93.        
  94.         GUI.TextArea(new Rect(400, 100, 300, 300), consoleText); //Define a textarea to use as a console
  95.     }
  96.  
  97.     #endregion
  98.  
  99.     #region Custom Subroutines 
  100.  
  101.     /*
  102.      * subRoutine to handle chat messages
  103.      */
  104.     public void sendChatMessage(string cLine) //Define the string passed arugment as cLine
  105.     {
  106.         Hashtable eventContent = new Hashtable(); //Create a new hashtable which will be sent to the photon sevrer
  107.         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
  108.        
  109.         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
  110.     }
  111.  
  112.     #endregion
  113.    
  114.     #region IPhotonPeerListener Members
  115.    
  116.     /*
  117.      * Routines from IPhotonPeerListener
  118.      */
  119.  
  120.     public void DebugReturn(DebugLevel level, string message)
  121.     {
  122.         //throw new NotImplementedException();
  123.     }
  124.    
  125.     //Controls the recieving of Events
  126.     public void EventAction(byte eventCode, System.Collections.Hashtable photonEvent)
  127.     {
  128.         consoleText += ("-- EventAction: " + (LiteEventCode)eventCode + "(" + eventCode + ")"); //Add debug information to the console
  129.        
  130.         switch(eventCode) //When an event is recieved, unity needs to know what to do with it :D
  131.         {
  132.             case (byte)chatEventCode.Message: //Is the event a chatMessage?
  133.                 int sourceActorNr = (int)photonEvent[(byte)LiteEventKey.ActorNr]; //get the senders number
  134.                 Hashtable evData = (Hashtable)photonEvent[(byte)LiteEventKey.Data]; //define a hashtable and grab the hashtable from the photon packet(s)
  135.                 chatText += ("- Player " + sourceActorNr + " say's: " + evData[(byte)chatEventKey.PlayerMessage] + "\n"); //Add a chat message to the chat text
  136.                 break;
  137.         }
  138.     }
  139.    
  140.     //Control the recieving of Operations
  141.     public void OperationResult(byte opCode, int returnCode, System.Collections.Hashtable returnValues, short invocID)
  142.     {
  143.         if(returnCode == 0)
  144.             consoleText += ("-- Op Result: OK - " + (LiteOpCode)opCode + "(" + opCode + ")"); //Debuging information
  145.        
  146.         else
  147.         {
  148.             consoleText += ("-- Op Result: NOT OK - " + (LiteOpCode)opCode + "(" + opCode + ") ->returnCode=" + returnCode + " DebugString=" + returnValues[(byte)LiteOpKey.DebugString]);
  149.             return;
  150.         }
  151.  
  152.         switch (opCode) //When an operation is recieved, unity needs to know what to do with it :D
  153.         {
  154.             case (byte)LiteOpCode.Join: //We joined a "Lite" room
  155.                 int myActorNr = (int)returnValues[(byte)LiteOpKey.ActorNr]; //Get YOUR actor number
  156.                 consoleText += ("-- My PlayerNr is: " + myActorNr); //print your actors number
  157.                 break;     
  158.         }
  159.     }
  160.    
  161.     //Controls photon base communication
  162.     public void PeerStatusCallback(StatusCode statusCode)
  163.     {
  164.         //consoleText += ("PeerStatusCallback:" + statusCode.ToString());  
  165.         switch(statusCode)
  166.         {
  167.             case StatusCode.Connect: //Did we connected to photon?
  168.                 consoleText += ("Connected to Photon Server!"); //We did connect to photon!
  169.                 Hashtable opParams = new Hashtable(); //We now need to send an operation to join a room, do we make a new hashtable
  170.                 opParams[(byte)LiteOpKey.RoomName] = "Room"; //We place the room name we wish to join in the hashtable
  171.                 Peer.OpCustom((byte)LiteOpCode.Join, opParams, true); //we sent the hashtable
  172.                 connected2Photon = true; //We are now connected :D
  173.                 break;
  174.            
  175.             case StatusCode.Disconnect: //We disconnected from Photom :(
  176.                 consoleText += ("Disconnected from Photon Server!"); //Print
  177.                 connected2Photon = false; //We are not connected
  178.                 break;
  179.            
  180.             default:
  181.                 consoleText += ("StatusCode not handled ");
  182.                 break;
  183.         }
  184.     }
  185.    
  186.     #endregion
  187.    
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement