Ejemplo n.º 1
0
 public static void InitializeNearby(Action <INearbyConnectionClient> callback)
 {
     Debug.Log((object)"Calling InitializeNearby!");
     if (PlayGamesPlatform.sNearbyConnectionClient == null)
     {
         NearbyConnectionClientFactory.Create((Action <INearbyConnectionClient>)(client =>
         {
             Debug.Log((object)"Nearby Client Created!!");
             PlayGamesPlatform.sNearbyConnectionClient = client;
             if (callback != null)
             {
                 callback(client);
             }
             else
             {
                 Debug.Log((object)"Initialize Nearby callback is null");
             }
         }));
     }
     else if (callback != null)
     {
         Debug.Log((object)"Nearby Already initialized: calling callback directly");
         callback(PlayGamesPlatform.sNearbyConnectionClient);
     }
     else
     {
         Debug.Log((object)"Nearby Already initialized");
     }
 }
        public static void InitializeNearby(Action<INearbyConnectionClient> callback)
        {
            Debug.Log("Calling InitializeNearby!");
            if (sNearbyConnectionClient == null)
            {
#if UNITY_ANDROID && !UNITY_EDITOR
                NearbyConnectionClientFactory.Create(client => {
                    Debug.Log("Nearby Client Created!!");
                    sNearbyConnectionClient = client;
                    if (callback != null) {
                        callback.Invoke(client);
                    }
                    else {
                        Debug.Log("Initialize Nearby callback is null");
                    }
                });
#else
                sNearbyConnectionClient = new DummyNearbyConnectionClient();
                if (callback != null)
                {
                    callback.Invoke(sNearbyConnectionClient);
                }

#endif
            }
            else if (callback != null)
            {
                Debug.Log("Nearby Already initialized: calling callback directly");
                callback.Invoke(sNearbyConnectionClient);
            }
            else
            {
                Debug.Log("Nearby Already initialized");
            }
        }
 public static void InitializeNearby(Action <INearbyConnectionClient> callback)
 {
     Debug.Log((object)"Calling InitializeNearby!");
     if (sNearbyConnectionClient == null)
     {
         NearbyConnectionClientFactory.Create(delegate(INearbyConnectionClient client)
         {
             Debug.Log((object)"Nearby Client Created!!");
             sNearbyConnectionClient = client;
             if (callback != null)
             {
                 callback(client);
             }
             else
             {
                 Debug.Log((object)"Initialize Nearby callback is null");
             }
         });
     }
     else if (callback != null)
     {
         Debug.Log((object)"Nearby Already initialized: calling callback directly");
         callback(sNearbyConnectionClient);
     }
     else
     {
         Debug.Log((object)"Nearby Already initialized");
     }
 }
Ejemplo n.º 4
0
        public static void CreateObject(INearbyConnectionClient client)
        {
            if (instance != null)
            {
                return;
            }

            mClient = client;
            if (Application.isPlaying)
            {
                // add an invisible game object to the scene
                GameObject obj = new GameObject("PlayGames_NearbyHelper");
                DontDestroyOnLoad(obj);
                instance = obj.AddComponent <NearbyHelperObject>();
            }
            else
            {
                instance = new NearbyHelperObject();
            }
        }
Ejemplo n.º 5
0
        public static void InitializeNearby(Action<INearbyConnectionClient> callback)
        {
            Debug.Log("Calling InitializeNearby!");
            if (sNearbyConnectionClient == null)
            {
            #if UNITY_ANDROID && !UNITY_EDITOR
                NearbyConnectionClientFactory.Create(client => {
                    Debug.Log("Nearby Client Created!!");
                    sNearbyConnectionClient = client;
                    if (callback != null) {
                        callback.Invoke(client);
                    }
                    else {
                        Debug.Log("Initialize Nearby callback is null");
                    }
                });
            #else
                sNearbyConnectionClient = new DummyNearbyConnectionClient();
                if (callback != null)
                {
                    callback.Invoke(sNearbyConnectionClient);
                }

            #endif
            }
            else if (callback != null)
            {
                Debug.Log("Nearby Already initialized: calling callback directly");
                callback.Invoke(sNearbyConnectionClient);
            }
            else
            {
                Debug.Log("Nearby Already initialized");
            }
        }
Ejemplo n.º 6
0
        // Update is called once per frame
        internal void OnGUI()
        {
            INearbyConnectionClient client = PlayGamesPlatform.Nearby;

            if (Input.touchCount > 0)
            {
                Touch touch = Input.touches[0];

                // TODO: handle the endpoint scroll vs. the message scroll.
                if (touch.phase == TouchPhase.Moved)
                {
                    mMessageViewVector.y += touch.deltaPosition.y;
                }
            }

            // count down the timers
            if (mAdvertisingRemaining > 0)
            {
                mAdvertisingRemaining -= Time.deltaTime;
            }

            if (mDiscoveryRemaining > 0)
            {
                mDiscoveryRemaining -= Time.deltaTime;
            }

            mOwner.DrawTitle("Nearby Connections");

            if (client == null)
            {
                mOwner.Status = "Nearby client is null!";
                mOwner.DrawStatus();
                if (GUI.Button(CalcGrid(1, 4), "Back"))
                {
                    mOwner.SetUI(MainGui.Ui.Main);
                }
                return;
            }

            string topStatus = "Nearby: " + client.GetServiceId();
            string advertButton;
            bool   advertising = mAdvertisingRemaining > 0;
            string discoveryButton;
            bool   discovering = mDiscoveryRemaining > 0;

            if (advertising)
            {
                topStatus   += string.Format(" Advertising({0})", (int)mAdvertisingRemaining);
                advertButton = "Stop\nAdvertising";
            }
            else
            {
                advertButton = "Start\nAdvertising";
            }

            if (discovering)
            {
                topStatus      += string.Format(" Disovering({0})", (int)mDiscoveryRemaining);
                discoveryButton = "Stop\nDiscovery";
            }
            else
            {
                discoveryButton = "Start\nDiscovery";
            }

            if (GUI.Button(CalcGrid(0, 1), advertButton))
            {
                if (!advertising)
                {
                    // always call stop to make sure it is clear before calling start.
                    client.StopAdvertising();

                    // use a name of null to use the default name
                    string        nearbyName    = null;
                    List <string> nearbyAppsIds = new List <string>();
                    Debug.Log("Advertising: " + client.GetAppBundleId());
                    nearbyAppsIds.Add(client.GetAppBundleId());
                    TimeSpan advertisingTimeSpan = TimeSpan.FromMilliseconds(timeoutMillis);
                    mAdvertisingRemaining = timeoutMillis / 1000f;
                    client.StartAdvertising(
                        nearbyName,
                        nearbyAppsIds,
                        advertisingTimeSpan,
                        OnAdvertisingResult,
                        OnConnectionRequest);
                }
                else
                {
                    client.StopAdvertising();
                    mNearbyStatus         = "Advertising stopped";
                    mAdvertisingRemaining = 0f;
                }
            }
            else if (GUI.Button(CalcGrid(1, 1), discoveryButton))
            {
                string nearbyServiceId = client.GetServiceId();
                client.StopDiscovery(nearbyServiceId);

                if (!discovering)
                {
                    TimeSpan advertisingTimeSpan = TimeSpan.FromMilliseconds(timeoutMillis);
                    mDiscoveryRemaining = timeoutMillis / 1000f;
                    client.StartDiscovery(nearbyServiceId, advertisingTimeSpan, this);
                    mNearbyStatus = "Discovery started for " + nearbyServiceId;
                }
                else
                {
                    mNearbyStatus       = "Discovery stopped";
                    mDiscoveryRemaining = 0f;
                }
            }
            else if (GUI.Button(CalcGrid(2, 1), "Stop All"))
            {
                client.StopAllConnections();
                mEndpoints.Clear();
                mKnownEndpoints.Clear();
                mMessageLog.Clear();
                mNearbyStatus         = "Stopped all connections";
                mAdvertisingRemaining = 0;
                mDiscoveryRemaining   = 0;
            }
            else if (GUI.Button(CalcGrid(0, 2), "Send All\nReliable"))
            {
                List <string> dest = new List <string>();
                foreach (EndpointHolder ep in mEndpoints.Values)
                {
                    if (ep.State == EndpointState.CONNECTED)
                    {
                        dest.Add(ep.Endpoint.EndpointId);
                    }
                }

                string msg = "Reliable from " + client.GetServiceId() + " " +
                             ((int)Time.realtimeSinceStartup);
                client.SendReliable(dest, System.Text.Encoding.UTF8.GetBytes(msg));
            }
            else if (GUI.Button(CalcGrid(1, 2), "Send All\nUnreliable"))
            {
                List <string> dest = new List <string>();
                foreach (EndpointHolder ep in mEndpoints.Values)
                {
                    if (ep.State == EndpointState.CONNECTED)
                    {
                        dest.Add(ep.Endpoint.EndpointId);
                    }
                }

                string msg = "Unreliable from " + client.GetServiceId() + " " +
                             ((int)Time.realtimeSinceStartup);
                client.SendUnreliable(dest, System.Text.Encoding.UTF8.GetBytes(msg));
            }
            else if (GUI.Button(CalcGrid(2, 2), "Back"))
            {
                mOwner.SetUI(MainGui.Ui.Main);
            }

            // Discovered endpoints
            mEndpointsViewVector = GUI.BeginScrollView(
                CalcGrid(0, 3, 3, 2),
                mEndpointsViewVector,
                CalcGrid(0, 0, 3, 1 + mEndpoints.Count));
            if (mEndpoints.Count == 0)
            {
                GUI.Label(CalcGrid(0, 0), "No Endpoints Discovered");
            }
            else
            {
                mOwner.Status = "Found " + mEndpoints.Count + " endpoints";
            }

            int           index        = 0;
            GUIStyle      style        = mOwner.GuiSkin.GetStyle("box");
            List <string> keysToRemove = new List <string>();

            foreach (EndpointHolder endpt in mEndpoints.Values)
            {
                if (index % 2 == 0)
                {
                    GUI.BeginGroup(CalcGrid(0, index * 2, 3, 2), style);
                }
                else
                {
                    GUI.BeginGroup(CalcGrid(0, index * 2, 3, 2));
                }

                GUI.Label(CalcGrid(0, 0), endpt.ToString());
                if (endpt.State == EndpointState.DISCOVERED ||
                    endpt.State == EndpointState.DISCONNECTED ||
                    endpt.State == EndpointState.REJECTED)
                {
                    if (GUI.Button(CalcGrid(1, 0), "Connect"))
                    {
                        string name    = null;
                        byte[] payload = new byte[0];
                        client.SendConnectionRequest(
                            name,
                            endpt.Endpoint.EndpointId,
                            payload,
                            OnConnectionResponse,
                            this);
                    }
                }

                if (endpt.State == EndpointState.REQUESTED)
                {
                    if (GUI.Button(CalcGrid(1, 1), "Accept"))
                    {
                        client.AcceptConnectionRequest(endpt.Endpoint.EndpointId, new byte[0], this);
                        endpt.State = EndpointState.CONNECTED;
                    }
                    else if (GUI.Button(CalcGrid(2, 1), "Reject"))
                    {
                        client.RejectConnectionRequest(endpt.Endpoint.EndpointId);
                        keysToRemove.Add(endpt.Endpoint.EndpointId);
                    }
                }

                if (endpt.State == EndpointState.CONNECTED)
                {
                    if (GUI.Button(CalcGrid(1, 0), "Disconnect"))
                    {
                        client.DisconnectFromEndpoint(endpt.Endpoint.EndpointId);
                        endpt.State = EndpointState.DISCONNECTED;
                    }

                    if (GUI.Button(CalcGrid(1, 1), "Send\nReliable"))
                    {
                        List <string> dest = new List <string>();
                        dest.Add(endpt.Endpoint.EndpointId);
                        string msg = "Reliable from " + client.GetServiceId() + " " +
                                     ((int)Time.realtimeSinceStartup);
                        client.SendReliable(dest, System.Text.Encoding.UTF8.GetBytes(msg));
                    }

                    if (GUI.Button(CalcGrid(2, 1), "Send\nUnreliable"))
                    {
                        // client.SendReliable
                        List <string> dest = new List <string>();
                        dest.Add(endpt.Endpoint.EndpointId);
                        string msg = "Unreliable from " + client.GetServiceId() + " " +
                                     ((int)Time.realtimeSinceStartup);
                        client.SendReliable(dest, System.Text.Encoding.UTF8.GetBytes(msg));
                    }
                }
                GUI.EndGroup();
                index++;
            }

            GUI.EndScrollView();

            foreach (string key in keysToRemove)
            {
                mEndpoints.Remove(key);
            }

            mMessageViewVector = GUI.BeginScrollView(
                CalcGrid(0, 6, 3, 2),
                mMessageViewVector,
                CalcGrid(0, 0, 3, 1 + mMessageLog.Count));
            if (mMessageLog.Count == 0)
            {
                GUI.Label(CalcGrid(0, 0), "No Messages");
            }

            for (int i = 0; i < mMessageLog.Count; i++)
            {
                GUI.Label(CalcGrid(0, i, 3, 1), mMessageLog[i]);
            }

            GUI.EndScrollView();
            if (mNearbyStatus != null)
            {
                GUI.Label(CalcGrid(0, 8, 3, 2), mNearbyStatus);
            }
        }