PUN Classic (v1), PUN 2 and Bolt are in maintenance mode. PUN 2 will support Unity 2019 to 2022, but no new features will be added. Of course all your PUN & Bolt projects will continue to work and run with the known performance in the future. For any upcoming or new projects: please switch to Photon Fusion or Quantum.

EnableLanBroadcast & 예제

아래의 예제는 클라이언트와 서버간의 자동-연결 목적을 위한 두 개의 안드로이드 기기간 동작을 위한 EnableLanBroadcast를 얻는 방법에 대해서 설명하고 있습니다.

제가 가졌었던 문제들은 메뉴 코드 내부에서 EnableLanBroadcast와 SetHostInfo를 호출하려고 했을때였습니다. 전 이게 너무 빨랐다고 믿고 있습니다.

제가 필요했던것은 ClientCallbacks과 ServerCallbacks 스크립트에서 BoltStarted() 콜백을 집어넣는 것 이었습니다.

또한 안드로이드의 메니페스트 파일에 추가 해야 했습니다:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE">

동일한 기기에서 클라이언트와 서버를 동시에 수행하지 못하므로 두개의 다른 기기를 사용하여 테스트를 수행해야한다는 것 입니다.

다음은 자동 연결에 대한 간단한 LAN 메뉴 입니다:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UdpKit;

public class MenuLan : MonoBehaviour {
      
   private bool _findLanServer;   
   private int _lanServerPort = 27000;
   
   void OnGUI() {

      GUILayout.BeginArea(new Rect(10, 10, Screen.width - 20, Screen.height - 20));
  
         //display sessions
         //foreach(var session in BoltNetwork.SessionList) {
         //   if(GUILayout.Button("Server: " + session.Value.HostName)) {            
         //      UdpKit.UdpEndPoint endpoint = UdpKit.UdpEndPoint.Parse(session.Value.LanEndPoint.Address + ":" + _lanServerPort);
         //      BoltNetwork.Connect(endpoint);                                                      
         //   }
         //}      

         //if we have a session then auto-connect
         if(BoltNetwork.SessionList.Count > 0)
         {
            UdpSession session = null;
            foreach(var s in BoltNetwork.SessionList) {
               session = s.Value;
               break;
            }
            if(session != null)
            {
               UdpKit.UdpEndPoint endpoint = UdpKit.UdpEndPoint.Parse(session.LanEndPoint.Address + ":" + _lanServerPort);
               BoltNetwork.Connect(endpoint);               
            }
         }

         if (GUILayout.Button ("<- Back", GUILayout.ExpandWidth (true), GUILayout.ExpandHeight (true))) {
            _findLanServer = false;                              
         }
      }
      else{                  
         if (GUILayout.Button ("Start LAN Game", GUILayout.ExpandWidth (true), GUILayout.ExpandHeight (true))) {         
            StartServer (_lanServerPort, "Main_Menu_Lan_Lobby");                                       
         }
         
         if (GUILayout.Button ("Find LAN Game", GUILayout.ExpandWidth (true), GUILayout.ExpandHeight (true))) {
            StartClient(_lanServerPort);
         }
      }         
      GUILayout.EndArea();
   }
      
   void StartServer(int serverPort, string map) {
      
      BoltLauncher.StartServer(new UdpKit.UdpEndPoint(UdpKit.UdpIPv4Address.Any, (ushort)serverPort));
      BoltNetwork.LoadScene(map);
   }

   void StartClient(int serverPort)
   {
  
    BoltLauncher.StartClient (new UdpKit.UdpEndPoint(UdpKit.UdpIPv4Address.Any, (ushort)serverPort));
      _findLanServer = true;
   }
}

ServerCallbacks 스크립트는 다음과 같습니다:

using UnityEngine;
using System.Collections;

[BoltGlobalBehaviour(BoltNetworkModes.Server)]

public class ServerCallbacks : Bolt.GlobalEventListener
{

   public override void BoltStarted()
   {

      BoltNetwork.EnableLanBroadcast();
      BoltNetwork.SetHostInfo("MyGame", null);
      Debug.Log("Bolt started, enabling LAN boardcast - setting host info");
   }   
}

ClientCallbacks 는 다음과 같습니다:

using UnityEngine;
using System.Collections;

[BoltGlobalBehaviour(BoltNetworkModes.Client)]

public class ClientCallbacks : Bolt.GlobalEventListener
{
   public override void BoltStarted()
   {

      BoltNetwork.EnableLanBroadcast();
      Debug.Log("Bolt started on Client, enabling LAN boardcast");
   }
}

기술문서 TOP으로 돌아가기