DMeville

Untitled

Apr 18th, 2018
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.88 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using UdpKit;
  5. using Bolt;
  6.  
  7. namespace Photon
  8. {
  9.  
  10.     [BoltGlobalBehaviour("PhotonTutorial")]
  11.     public class PhotonInit : Bolt.GlobalEventListener
  12.     {
  13.         string ip;
  14.         bool done = false;
  15.  
  16.         // helper enum and attribute to hold which mode application is running on
  17.         enum State
  18.         {
  19.             SelectMode,
  20.             ModeServer,
  21.             ModeClient
  22.         }
  23.         State _state;
  24.  
  25.         public override void BoltStartBegin()
  26.         {
  27.             BoltNetwork.RegisterTokenClass<RoomProtocolToken>();
  28.             BoltNetwork.RegisterTokenClass<ServerAcceptToken>();
  29.             BoltNetwork.RegisterTokenClass<ServerConnectToken>();
  30.         }
  31.  
  32.         public override void BoltStartDone()
  33.         {
  34.             BoltConsole.Write(ip);
  35.  
  36.             //sometimes this call to SetServerHostInfo doesn't get through?
  37.             //resulting in a client showing 0 sessions
  38.  
  39.             Invoke("SetServerHostInfo", 1f);
  40.         }
  41.  
  42.         private void SetServerHostInfo() {
  43.             BoltNetwork.SetHostInfo("MyPhotonGame", new RoomProtocolToken {
  44.                 ArbitraryData = ip,
  45.             });
  46.         }
  47.  
  48.  
  49.  
  50.         private IEnumerator Start()
  51.         {
  52.             string url = ("http://checkip.dyndns.org");
  53.  
  54.             using (WWW www = new WWW(url))
  55.             {
  56.                 yield return www;
  57.  
  58.                 ip = www.text;
  59.  
  60.             }
  61.  
  62.  
  63.  
  64.         }
  65.  
  66.         void Awake()
  67.         {
  68.  
  69.  
  70.  
  71.             // Set Bolt to use Photon as transport layer
  72.             // this will connect to Photon using config values from Bolt's settings window
  73.             BoltLauncher.SetUdpPlatform(new PhotonPlatform());
  74.  
  75.             // Optionally, you may want to config the Photon transport layer programatically:
  76.  
  77.             //BoltLauncher.SetUdpPlatform(new PhotonPlatform(new PhotonPlatformConfig
  78.             //{
  79.             //    AppId = "<your-app-id>",
  80.             //    RegionMaster = "<your-region>",
  81.             //    UsePunchThrough = false // set to true, to use PunchThrough
  82.             //}));
  83.         }
  84.  
  85.         void OnGUI()
  86.         {
  87.             switch (_state)
  88.             {
  89.                 // starting Bolt is the same regardless of the transport layer
  90.                 case State.SelectMode:
  91.                     if (GUILayout.Button("Start Client"))
  92.                     {
  93.                         BoltLauncher.StartClient();
  94.                         _state = State.ModeClient;
  95.                     }
  96.  
  97.                     if (GUILayout.Button("Start Server"))
  98.                     {
  99.                         BoltLauncher.StartServer();
  100.                         _state = State.ModeServer;
  101.                     }
  102.  
  103.                     break;
  104.  
  105.                 // Publishing a session into the matchmaking server
  106.                 case State.ModeServer:
  107.                     //if (BoltNetwork.isRunning && BoltNetwork.isServer)
  108.                     //{
  109.                     //    if (GUILayout.Button("Publish HostInfo And Load Map"))
  110.                     //    {
  111.                     //        BoltNetwork.SetHostInfo("MyPhotonGame", new RoomProtocolToken
  112.                     //        {
  113.                     //            ArbitraryData = ip,
  114.                     //        });
  115.  
  116.                     //        //BoltNetwork.LoadScene("PhotonInGame");
  117.                     //        BoltNetwork.LoadScene("SP_RooftopBar");
  118.                     //    }
  119.                     //}
  120.                     break;
  121.                 // for the client, after Bolt is innitialized, we should see the list
  122.                 // of available sessions and join one of them
  123.                 case State.ModeClient:
  124.                     if (done == false)
  125.                         if (BoltNetwork.isRunning && BoltNetwork.isClient)
  126.                         {
  127.                             GUILayout.Label("Session List");
  128.  
  129.                             foreach (var session in BoltNetwork.SessionList)
  130.                             {
  131.                                 RoomProtocolToken token = session.Value.GetProtocolToken() as RoomProtocolToken;
  132.  
  133.  
  134.                                 if (token != null)
  135.                                     if (token.ArbitraryData == ip)
  136.                                     {
  137.  
  138.  
  139.                                         ServerConnectToken connectToken = new ServerConnectToken
  140.                                         {
  141.                                             data = "ConnectTokenData"
  142.                                         };
  143.  
  144.                                         BoltNetwork.Connect(session.Value, connectToken);
  145.                                         done = true;
  146.                                         break;
  147.                                     }
  148.                             }
  149.                         }
  150.                     break;
  151.             }
  152.         }
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment