Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System;
- using UdpKit;
- using Bolt;
- namespace Photon
- {
- [BoltGlobalBehaviour("PhotonTutorial")]
- public class PhotonInit : Bolt.GlobalEventListener
- {
- string ip;
- bool done = false;
- // helper enum and attribute to hold which mode application is running on
- enum State
- {
- SelectMode,
- ModeServer,
- ModeClient
- }
- State _state;
- public override void BoltStartBegin()
- {
- BoltNetwork.RegisterTokenClass<RoomProtocolToken>();
- BoltNetwork.RegisterTokenClass<ServerAcceptToken>();
- BoltNetwork.RegisterTokenClass<ServerConnectToken>();
- }
- public override void BoltStartDone()
- {
- BoltConsole.Write(ip);
- //sometimes this call to SetServerHostInfo doesn't get through?
- //resulting in a client showing 0 sessions
- Invoke("SetServerHostInfo", 1f);
- }
- private void SetServerHostInfo() {
- BoltNetwork.SetHostInfo("MyPhotonGame", new RoomProtocolToken {
- ArbitraryData = ip,
- });
- }
- private IEnumerator Start()
- {
- string url = ("http://checkip.dyndns.org");
- using (WWW www = new WWW(url))
- {
- yield return www;
- ip = www.text;
- }
- }
- void Awake()
- {
- // Set Bolt to use Photon as transport layer
- // this will connect to Photon using config values from Bolt's settings window
- BoltLauncher.SetUdpPlatform(new PhotonPlatform());
- // Optionally, you may want to config the Photon transport layer programatically:
- //BoltLauncher.SetUdpPlatform(new PhotonPlatform(new PhotonPlatformConfig
- //{
- // AppId = "<your-app-id>",
- // RegionMaster = "<your-region>",
- // UsePunchThrough = false // set to true, to use PunchThrough
- //}));
- }
- void OnGUI()
- {
- switch (_state)
- {
- // starting Bolt is the same regardless of the transport layer
- case State.SelectMode:
- if (GUILayout.Button("Start Client"))
- {
- BoltLauncher.StartClient();
- _state = State.ModeClient;
- }
- if (GUILayout.Button("Start Server"))
- {
- BoltLauncher.StartServer();
- _state = State.ModeServer;
- }
- break;
- // Publishing a session into the matchmaking server
- case State.ModeServer:
- //if (BoltNetwork.isRunning && BoltNetwork.isServer)
- //{
- // if (GUILayout.Button("Publish HostInfo And Load Map"))
- // {
- // BoltNetwork.SetHostInfo("MyPhotonGame", new RoomProtocolToken
- // {
- // ArbitraryData = ip,
- // });
- // //BoltNetwork.LoadScene("PhotonInGame");
- // BoltNetwork.LoadScene("SP_RooftopBar");
- // }
- //}
- break;
- // for the client, after Bolt is innitialized, we should see the list
- // of available sessions and join one of them
- case State.ModeClient:
- if (done == false)
- if (BoltNetwork.isRunning && BoltNetwork.isClient)
- {
- GUILayout.Label("Session List");
- foreach (var session in BoltNetwork.SessionList)
- {
- RoomProtocolToken token = session.Value.GetProtocolToken() as RoomProtocolToken;
- if (token != null)
- if (token.ArbitraryData == ip)
- {
- ServerConnectToken connectToken = new ServerConnectToken
- {
- data = "ConnectTokenData"
- };
- BoltNetwork.Connect(session.Value, connectToken);
- done = true;
- break;
- }
- }
- }
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment