using System;
using ExitGames.Client.Photon;
using UnityEngine;
public class MMOEngine : MonoBehaviour, IGameListener
{
private static Game _engine;
public static Game Engine
{
get { return _engine; }
}
// Use this for initialization
void Awake()
{
Application.runInBackground = true;
_engine = new Game(this);
}
// Update is called once per frame
void Update()
{
try
{
_engine.Update();
}
catch (Exception e)
{
Debug.Log(e);
}
}
///
/// The on application quit.
///
public void OnApplicationQuit()
{
try
{
_engine.Disconnect();
}
catch (Exception e)
{
Debug.Log(e);
}
}
#region Inherited Interfaces
#region IGameListener
public bool IsDebugLogEnabled
{
get { return true; }
}
public void LogDebug(Game game, string message)
{
Debug.Log(message);
}
public void LogError(Game game, string message)
{
Debug.Log(message);
}
public void LogError(Game game, Exception exception)
{
Debug.Log(exception.ToString());
}
public void LogInfo(Game game, string message)
{
Debug.Log(message);
}
public void OnConnect(Game game)
{
Debug.Log("connected");
}
public void OnDisconnect(Game game, StatusCode returnCode)
{
Debug.Log("disconnected");
}
#endregion
#endregion
}
The new code for Login.cs is this:
using System.Collections;
using UnityEngine;
using ExitGames.Client.Photon;
using AegisBornCommon;
public class Login : MonoBehaviour
{
private Game _engine;
public void Start()
{
_engine = MMOEngine.Engine;
}
public void OnGUI()
{
if (GUI.Button(new Rect(100, 60, 100, 30), "Connect"))
{
var peer = new PhotonPeer(_engine, false);
_engine.Initialize(peer, "localhost:5055", "AegisBorn");
}
if (GUI.Button(new Rect(200, 60, 100, 30), "Send Operation"))
{
_engine.SendOp((OperationCode)100, new Hashtable(), false, 0);
}
GUI.Label(new Rect(100, 100, 300, 300), _engine.State.ToString());
}
}
Now that we have them separated, we want to create a new game object called MMOEngine and attach our script to it. This now makes it like our SmartFoxServer code where we had the connection handler get a connection to the SmartFoxConnection class.
Nothing too dramatic this time. Next time we will be pulling over our GUI code from our other project and sending an operation that asks to log in. Enjoy!
No comments:
Post a Comment