Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Errors
- Assets/Game/Scripts/LobbyGUI.cs(87,27): error CS0122: `templateExtensionResponse._cmd' is inaccessible due to its protection level
- Assets/Game/Scripts/LobbyGUI.cs(89,56): error CS0122: `templateExtensionResponse.err' is inaccessible due to its protection level
- Assets/Game/Scripts/LobbyGUI.cs(90,33): error CS0122: `templateExtensionResponse._cmd' is inaccessible due to its protection level
- Assets/Game/Scripts/LobbyGUI.cs(91,57): error CS0122: `templateExtensionResponse.err' is inaccessible due to its protection level
- //Code
- using UnityEngine;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Runtime.InteropServices;
- using System.Security.Permissions;
- using System.Text;
- using SmartFoxClientAPI;
- using SmartFoxClientAPI.Data;
- public class templateExtensionResponse : System.Object{
- String _cmd = "";
- String err = "";
- }
- public class LobbyGUI : MonoBehaviour
- {
- private SmartFoxClient smartFox;
- private string zone = "simpleChat";
- private string username = "";
- private string password = "";
- private string loginErrorMessage = "";
- private bool isLoggedIn;
- private bool roomListReceived = false;
- private string newMessage = "";
- private ArrayList messages = new ArrayList();
- // Locker to use for messages collection to ensure its cross-thread safety
- private System.Object messagesLocker = new System.Object();
- private Vector2 chatScrollPosition, userScrollPosition;
- private int roomSelection = 0;
- private string [] roomStrings;
- public GUISkin gSkin;
- void Start()
- {
- if (SmartFox.initialized)
- {
- smartFox = SmartFox.Connection;
- }
- else
- {
- Application.LoadLevel("connection");
- }
- // Register callbacks
- SFSEvent.onLogin += OnLogin;
- SFSEvent.onLogout += OnLogout;
- SFSEvent.onConnectionLost += OnDisconnect;
- SFSEvent.onRoomListUpdate += OnRoomList;
- SFSEvent.onJoinRoom += OnJoinRoom;
- SFSEvent.onPublicMessage += OnPublicMessage;
- SFSEvent.onDebugMessage += OnDebugMessage;
- SFSEvent.onExtensionResponse += OnExtensionResponse;
- }
- private void UnregisterSFSSceneCallbacks() {
- // This should be called when switching scenes, so callbacks from the backend do not trigger code in this scene
- SFSEvent.onLogin -= OnLogin;
- SFSEvent.onLogout -= OnLogout;
- SFSEvent.onConnectionLost -= OnDisconnect;
- SFSEvent.onRoomListUpdate -= OnRoomList;
- SFSEvent.onJoinRoom -= OnJoinRoom;
- SFSEvent.onPublicMessage -= OnPublicMessage;
- SFSEvent.onDebugMessage -= OnDebugMessage;
- SFSEvent.onExtensionResponse -= OnExtensionResponse;
- }
- // Various SFS callbacks
- void OnLogin(bool success, string name, string error)
- {
- Debug.Log("On Login callback got: " + success + " : " + error + " : " + name);
- if (success) {
- isLoggedIn = true;
- } else {
- loginErrorMessage = error;
- Debug.Log("Login error: "+error);
- }
- }
- void OnExtensionResponse(System.Object resObj, String type){
- templateExtensionResponse result = new templateExtensionResponse();
- result = (templateExtensionResponse)resObj;
- if(result._cmd == "loginOK"){
- //login okay.
- OnLogin(true, username, result.err);
- }else if(result._cmd == "loginKO"){
- OnLogin(false, username, result.err);
- }
- }
- void OnLogout()
- {
- Debug.Log("OnLogout");
- isLoggedIn = false;
- roomListReceived = false;
- }
- void OnDisconnect()
- {
- Debug.Log("OnDisconnect");
- isLoggedIn = false;
- roomListReceived = false;
- UnregisterSFSSceneCallbacks();
- }
- public void OnDebugMessage(string message)
- {
- Debug.Log("[SFS DEBUG] " + message);
- }
- void OnRoomList(Hashtable roomList)
- {
- try {
- List<string> rooms = new List<string>();
- foreach (int roomId in roomList.Keys)
- {
- Room room = (Room)roomList[roomId];
- if (room.IsPrivate())
- {
- continue;
- }
- Debug.Log("Room id: " + roomId + " has name: " + room.GetName());
- rooms.Add (room.GetName());
- }
- roomListReceived = true;
- roomStrings = rooms.ToArray();
- // Users always have to be in a room, so lets go to the Hall as our main "lobby"
- if (smartFox.GetActiveRoom() == null) {
- smartFox.JoinRoom("The Hall");
- }
- }
- catch (Exception e) {
- Debug.Log("Room list error: "+e.Message+" "+e.StackTrace);
- }
- }
- void OnJoinRoom(Room room)
- {
- Debug.Log("Room " + room.GetName() + " joined successfully");
- lock (messagesLocker) {
- messages.Clear();
- }
- }
- void OnPublicMessage(string message, User sender, int roomId)
- {
- // We use lock here to ensure cross-thread safety on the messages collection
- lock (messagesLocker) {
- messages.Add(sender.GetName() + " said " + message);
- }
- chatScrollPosition.y = Mathf.Infinity;
- Debug.Log("User " + sender.GetName() + " said: " + message);
- }
- // Finally draw all the lobby GUI
- void OnGUI()
- {
- GUI.skin = gSkin;
- GUI.Label(new Rect(2, -2, 680, 70), "", "SFSLogo");
- // Login
- if (!isLoggedIn)
- {
- GUI.Label(new Rect(10, 90, 100, 100), "Zone: ");
- zone = GUI.TextField(new Rect(100, 90, 200, 20), zone, 25);
- GUI.Label(new Rect(10, 116, 100, 100), "Userame: ");
- username = GUI.TextField(new Rect(100, 116, 200, 20), username, 25);
- GUI.Label(new Rect(10, 142, 100, 100), "Password: ");
- password = GUI.TextField(new Rect(100, 142, 200, 20), password, 25);
- GUI.Label(new Rect(10, 218, 100, 100), loginErrorMessage);
- if (GUI.Button(new Rect(100, 166, 100, 24), "Login") || (Event.current.type == EventType.keyDown && Event.current.character == '\n'))
- {
- smartFox.Login(zone, username, password);
- }
- }
- else
- {
- // Standard view
- if (GUI.Button(new Rect(580, 478, 90, 24), "Logout"))
- {
- smartFox.Logout();
- }
- // Basic info
- //GUI.Label(new Rect(10, 40, 200, 100), "Logged in as " + smartFox.myUserName);
- Room currentActiveRoom = smartFox.GetActiveRoom();
- if (currentActiveRoom != null)
- {
- GUI.Label(new Rect(498, 248, 180, 40), "Current room: " + currentActiveRoom.GetName());
- }
- // Room list
- if (roomListReceived)
- {
- GUI.Box(new Rect(490, 80, 180, 170), "Room List");
- GUILayout.BeginArea (new Rect(500, 110, 150, 130));
- roomSelection = GUILayout.SelectionGrid (roomSelection, roomStrings, 1, "RoomListButton");
- if (roomStrings[roomSelection] != currentActiveRoom.GetName())
- {
- smartFox.JoinRoom(roomStrings[roomSelection]);
- }
- GUILayout.EndArea();
- }
- // Room chat window
- if (currentActiveRoom != null)
- {
- // User list
- GUI.Box(new Rect(490, 270, 180, 200), "Users");
- GUILayout.BeginArea (new Rect(500, 300, 150, 160));
- userScrollPosition = GUILayout.BeginScrollView (userScrollPosition, GUILayout.Width (150), GUILayout.Height (160));
- GUILayout.BeginVertical();
- foreach (User user in currentActiveRoom.GetUserList().Values)
- {
- GUILayout.Label(user.GetName());
- }
- GUILayout.EndVertical();
- GUILayout.EndScrollView ();
- GUILayout.EndArea();
- // Chat history
- GUI.Box(new Rect(10, 80, 470, 390), "Chat");
- GUILayout.BeginArea (new Rect(20, 110, 450, 350));
- chatScrollPosition = GUILayout.BeginScrollView (chatScrollPosition, GUILayout.Width (450), GUILayout.Height (350));
- GUILayout.BeginVertical();
- // We use lock here to ensure cross-thread safety on the messages collection
- lock (messagesLocker) {
- foreach (string message in messages)
- {
- GUILayout.Label(message);
- }
- }
- GUILayout.EndVertical();
- GUILayout.EndScrollView ();
- GUILayout.EndArea();
- // Send message
- newMessage = GUI.TextField(new Rect(10, 480, 370, 20), newMessage, 50);
- if (GUI.Button(new Rect(390, 478, 90, 24), "Send") || (Event.current.type == EventType.keyDown && Event.current.character == '\n'))
- {
- smartFox.SendPublicMessage(newMessage, currentActiveRoom.GetId());
- newMessage = "";
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement