Example #1
0
        /// <summary>
        /// Change scene for all players. This function is called by a user.
        /// </summary>
        /// <param name="_sceneName">The name of the scene to change to</param>
        /// <example><code>
        /// void SomeFunction()
        /// {
        ///     ASL.ASLHelper.SendAndSetNewScene("YourSceneName");
        /// }
        /// </code></example>
        public static void SendAndSetNewScene(string _sceneName)
        {
            byte[]    scene   = Encoding.ASCII.GetBytes(_sceneName);
            RTMessage message = GameLiftManager.GetInstance().CreateRTMessage(GameLiftManager.OpCode.LoadScene, scene);

            GameLiftManager.GetInstance().m_Client.SendMessage(message);
        }
Example #2
0
        /// <summary>
        /// Sends a packet out to all players to spawn a prefab object
        /// </summary>
        /// <param name="_prefabName">The name of the prefab to be used</param>
        /// <param name="_position">The position of where the object will be spawned</param>
        /// <param name="_rotation">The rotation orientation of the object upon spawn</param>
        /// <param name="_parentID">The id of the parent object</param>
        /// <param name="_componentAssemblyQualifiedName">The full name of the component to be added to this object upon creation.</param>
        /// <param name="_instantiatedGameObjectClassName">The name of the class that contains the user provided function detailing what to do with this object after creation</param>
        /// <param name="_instantiatedGameObjectFunctionName">The name of the user provided function that contains the details of what to do with this object after creation</param>
        /// <param name="_claimRecoveryClassName">The name of the class that contains the user provided function detailing what to do if a claim for this object is rejected</param>
        /// <param name="_claimRecoveryFunctionName">The name of the user provided function that contains the details of what to do with this object if a claim for it is rejected</param>
        /// <param name="_sendFloatClassName">The name of the class that contains the user provided function detailing what to do when a user calls <see cref="ASLObject.SendFloatArray(float[])"/></param>
        /// <param name="_sendFloatFunctionName">The name of the user provided function that contains the details of what to do with this object if a user calls <see cref="ASLObject.SendFloatArray(float[])"/></param>
        private static void SendSpawnPrefab(string _prefabName, Vector3 _position, Quaternion _rotation, string _parentID = "", string _componentAssemblyQualifiedName = "", string _instantiatedGameObjectClassName = "", string _instantiatedGameObjectFunctionName = "",
                                            string _claimRecoveryClassName = "", string _claimRecoveryFunctionName = "", string _sendFloatClassName = "", string _sendFloatFunctionName = "")
        {
            string guid = Guid.NewGuid().ToString();

            byte[] id         = Encoding.ASCII.GetBytes(guid);
            byte[] position   = GameLiftManager.GetInstance().ConvertVector3ToByteArray(new Vector3(_position.x, _position.y, _position.z));
            byte[] rotation   = GameLiftManager.GetInstance().ConvertVector4ToByteArray(new Vector4(_rotation.x, _rotation.y, _rotation.z, _rotation.w));
            byte[] prefabName = Encoding.ASCII.GetBytes(_prefabName);
            byte[] parentID   = Encoding.ASCII.GetBytes(_parentID);
            byte[] componentAssemblyQualifiedName     = Encoding.ASCII.GetBytes(_componentAssemblyQualifiedName);
            byte[] instantiatedGameObjectClassName    = Encoding.ASCII.GetBytes(_instantiatedGameObjectClassName);
            byte[] instantiatedGameObjectFunctionName = Encoding.ASCII.GetBytes(_instantiatedGameObjectFunctionName);
            byte[] claimRecoveryClassName             = Encoding.ASCII.GetBytes(_claimRecoveryClassName);
            byte[] claimRecoveryFunctionName          = Encoding.ASCII.GetBytes(_claimRecoveryFunctionName);
            byte[] sendFloatClassName    = Encoding.ASCII.GetBytes(_sendFloatClassName);
            byte[] sendFloatFunctionName = Encoding.ASCII.GetBytes(_sendFloatFunctionName);
            byte[] peerId = Encoding.ASCII.GetBytes(GameLiftManager.GetInstance().m_PeerId.ToString());


            byte[] payload = GameLiftManager.GetInstance().CombineByteArrays(id, position, rotation, prefabName, parentID, componentAssemblyQualifiedName, instantiatedGameObjectClassName,
                                                                             instantiatedGameObjectFunctionName, claimRecoveryClassName, claimRecoveryFunctionName, sendFloatClassName, sendFloatFunctionName, peerId);

            RTMessage message = GameLiftManager.GetInstance().CreateRTMessage(GameLiftManager.OpCode.SpawnPrefab, payload);

            GameLiftManager.GetInstance().m_Client.SendMessage(message);
        }
Example #3
0
    public void SendMessage(bool fast, int opCode, String str, int target)
    {
        RTMessage      msg    = realtimeClient.NewMessage(opCode);
        DeliveryIntent intent = fast ? DeliveryIntent.Fast : DeliveryIntent.Reliable;

        Byte[] payload = StringToBytes(str);
        realtimeClient.SendMessage(msg.WithDeliveryIntent(intent).WithPayload(payload).WithTargetPlayer(target));
    }
        /// <summary>
        /// Waits for the cloud anchor to be created so it can then send it's information to other users
        /// </summary>
        /// <param name="_cloudAnchor">The cloud anchor that is being created</param>
        /// <param name="_hitResult">The location where it was created</param>
        /// <param name="_anchorObjectPrefab">The object that will be attached to the cloud anchor</param>
        /// <param name="_myPostCreateCloudAnchorFunction">The function to call after creating the cloud anchor</param>
        /// <param name="_waitForAllUsersToResolve">Flag indicating to wait or not for all users to resolve before calling the post create function</param>
        /// <param name="_setWorldOrigin">Flag indicating to set this cloud anchor as the world origin or not</param>
        /// <returns>Nothing - just waits until the cloud anchor succeeds or fails</returns>
        public IEnumerator WaitForCloudAnchorToBeCreated(ARCloudAnchor _cloudAnchor, Pose _hitResult, ASLObject _anchorObjectPrefab = null,
                                                         ASLObject.PostCreateCloudAnchorFunction _myPostCreateCloudAnchorFunction   = null,
                                                         bool _waitForAllUsersToResolve = true, bool _setWorldOrigin = true)
        {
            while (_cloudAnchor.cloudAnchorState == CloudAnchorState.TaskInProgress)
            {
                yield return(new WaitForEndOfFrame());
            }

            if (_cloudAnchor.cloudAnchorState == CloudAnchorState.Success)
            {
                //Successful:
                Debug.Log("Successfully Resolved cloud anchor: " + _cloudAnchor.cloudAnchorId + " for object: " + _anchorObjectPrefab?.m_Id);
                if (_anchorObjectPrefab == null)
                {
                    //Uncomment the line below to aid in visual debugging (helps display the cloud anchor)
                    //_anchorObjectPrefab = GameObject.CreatePrimitive(PrimitiveType.Cube).AddComponent<ASLObject>(); //if null, then create empty game object
                    _anchorObjectPrefab = new GameObject().AddComponent <ASLObject>();
                    _anchorObjectPrefab._LocallySetAnchorID(_cloudAnchor.cloudAnchorId); //Add ASLObject component to this anchor and set its anchor id variable

                    _anchorObjectPrefab._LocallySetID(Guid.NewGuid().ToString());        //Locally set the id of this object to a new id

                    //Add this anchor object to our ASL dictionary using the anchor id as its key. All users will do this once they resolve this cloud anchor to ensure they still in sync.
                    ASLHelper.m_ASLObjects.Add(_anchorObjectPrefab.m_Id, _anchorObjectPrefab.GetComponent <ASLObject>());
                    //_anchorObjectPrefab.GetComponent<Material>().color = Color.magenta;
                    _anchorObjectPrefab.transform.localScale = new Vector3(0.05f, 0.05f, 0.05f); //Set scale to be 5 cm
                }
                else
                {
                    _anchorObjectPrefab.GetComponent <ASLObject>()._LocallySetAnchorID(_cloudAnchor.cloudAnchorId); //Set anchor id variable
                    _anchorObjectPrefab.transform.localScale = new Vector3(0.05f, 0.05f, 0.05f);                    //Set scale to be 5 cm
                }
                //Send Resolve packet using _anchorObjectPrefab
                _anchorObjectPrefab.GetComponent <ASLObject>().SendCloudAnchorToResolve(_setWorldOrigin, _waitForAllUsersToResolve);

                Debug.Log("Wait: " + _waitForAllUsersToResolve);

                if (_waitForAllUsersToResolve)
                {
                    byte[]    id      = Encoding.ASCII.GetBytes(_anchorObjectPrefab.m_Id);
                    RTMessage message = GameLiftManager.GetInstance().CreateRTMessage(GameLiftManager.OpCode.ResolvedCloudAnchor, id);
                    GameLiftManager.GetInstance().m_Client.SendMessage(message);

                    _anchorObjectPrefab.StartWaitForAllUsersToResolveCloudAnchor(_cloudAnchor, _setWorldOrigin, _myPostCreateCloudAnchorFunction, _hitResult);
                }
                else //Don't wait for users to know about this cloud anchor
                {
                    _anchorObjectPrefab.GetComponent <ASLObject>()._LocallySetCloudAnchorResolved(true);
                    _anchorObjectPrefab.StartWaitForAllUsersToResolveCloudAnchor(_cloudAnchor, _setWorldOrigin, _myPostCreateCloudAnchorFunction, _hitResult);
                }
            }
            else
            {
                Debug.LogError("Failed to host Cloud Anchor " + _cloudAnchor.name + " " + _cloudAnchor.cloudAnchorState.ToString());
            }
        }
Example #5
0
    public void SendMessagePayload(string msg)
    {
        if (!usingTLS)
        {
            _client.SendEvent(200, Encoding.ASCII.GetBytes(msg));
        }
        else
        {
            RTMessage myRT = _client.NewMessage(200).WithTargetPlayer(-1).WithDeliveryIntent(DeliveryIntent.Reliable).WithPayload(Encoding.ASCII.GetBytes(msg));
            myRT.WithTargetPlayer(-1);

            _client.SendMessage(myRT);
        }
    }
        /// <summary>
        /// Currently counts down this object's claim time and releases the object back to the relay server after the specified amount of time has passed.
        /// Update is called once per frame
        /// </summary>
        private void Update()
        {
            // If we own it and we have no callbacks to perform
            //Then we don't need to own it anymore and thus can start to cancel it
            if (m_Mine && m_OutstandingClaimCallbackCount == 0 && m_ClaimTime > 0 && m_ClaimTime != 0) //if 0, then hold onto until stolen
            {
                m_ClaimReleaseTimer += Time.deltaTime * 1000;                                          //Translate to milliseconds
                if (m_ClaimReleaseTimer > m_ClaimTime)                                                 //If time to release our claim
                {
                    m_ReleaseFunction?.Invoke(gameObject);                                             //If user wants to do something before object is released - let them do so
                    _LocallyRemoveReleaseCallback();

                    byte[]    id      = Encoding.ASCII.GetBytes(m_Id);
                    RTMessage message = GameLiftManager.GetInstance().CreateRTMessage(GameLiftManager.OpCode.ReleaseClaimToServer, id);
                    GameLiftManager.GetInstance().m_Client.SendMessage(message);

                    m_Mine = false; //Release
                }
            }
        }
Example #7
0
            /// <summary>
            /// Asynchronously loads a scene and once loaded, informs everyone else that this user is ready to go into the newly loaded scene
            /// </summary>
            /// <param name="_sceneName">The name of the scene to be loaded</param>
            /// <returns>Null while loading</returns>
            private IEnumerator AsyncSceneLoader(string _sceneName)
            {
                while (SceneManager.GetActiveScene().name != "ASL_SceneLoader")
                {
                    yield return(null);
                }
                m_LoadingProgressTest = GameObject.Find("LoadingProgress").GetComponent <Text>();
                //Begin to load scene specified
                AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(_sceneName);

                //Don't let the scene activate until all users have it loaded (done via RT server)
                asyncOperation.allowSceneActivation = false;

                //While the scene is loading - output the progress
                while (!asyncOperation.isDone)
                {
                    m_LoadingProgressTest.text = "\n\nLoading Progress: " + (asyncOperation.progress * 100) + "%";
                    //Check if scene is finished loading:
                    if (asyncOperation.progress >= 0.9f && !asyncOperation.allowSceneActivation)
                    {
                        if (!m_Loaded)
                        {
                            m_Loaded = true; //Prevent multiple packets from sending unnecessarily
                            RTMessage message = GetInstance().CreateRTMessage(OpCode.LaunchScene, Encoding.ASCII.GetBytes(""));
                            GetInstance().m_Client.SendMessage(message);
                        }
                        //Change to text to inform user that they are now waiting on others
                        m_LoadingProgressTest.text = "\n\nFinished Loading. Waiting for other players...";
                        if (m_AllPlayersLoaded)
                        {
                            m_AllPlayersLoaded = false;
                            asyncOperation.allowSceneActivation = true;
                            m_Loaded = false;
                        }
                    }
                    yield return(null);
                }
            }