/// <summary>
        /// Instantiates a gameobject in a default position and rotation in the world.
        /// </summary>
        /// <param name="path">Project path where the gameobject resides.</param>
        /// <returns>Returns the gameobject set to instantiate.</returns>
        public async Task <GameObject> InstantiateGameObject(string path)
        {
            GameObject GO = null;

            GO = await Addressables.Instantiate(path, Vector3.zero, Quaternion.identity) as GameObject;

            return(GO);
        }
        /// <summary>
        /// Instantiates a gameobject in a given position and rotation in the world.
        /// </summary>
        /// <param name="path">Addressables</param>
        /// <param name="position">Position in the world to instantiate the gameobject.</param>
        /// <param name="rotation">Rotation to instantiate the gameobject.</param>
        /// <returns>Returns the gameobject set to instantiate.</returns>
        public async Task <GameObject> InstantiateGameObject(string path, Vector3 position, Quaternion rotation)
        {
            GameObject GO = null;

            GO = await Addressables.Instantiate(path, position, rotation) as GameObject;

            return(GO);
        }
        private void Start()
        {
            Image  img = GetComponent <Image>();
            Sprite s   = null;

            Addressables.Instantiate <Sprite>(address).Completed += (o) =>
            {
                s          = o.Result;
                img.sprite = s;
            };
        }
Example #4
0
        /// <summary>
        /// 创建go并放在parent下面, 且指定坐标
        /// </summary>
        public void InstantiateGameObject(string address, Transform parent, Vector3 localPosition,
                                          Action <GameObject> onComplete = null)
        {
            Address = address;

            //特别注意, Addressables 需要的地址是Position, 不是localPosition
            var pos    = parent.position + localPosition;
            var result = Addressables.Instantiate <GameObject>(address, pos, Quaternion.identity, parent);

            InternalOnComplete <GameObject>(result, onComplete);
        }
Example #5
0
    private async UniTask DoMainLoop()
    {
        // TODO: Handle an exception being thrown as a result of the connection failing.
        _socket = await WebSocket.ConnectAsync(new Uri("ws://localhost:8088/ws/"));

        // Wait for the initial game state to come in from the server.
        //
        // TODO: Handle an exception being thrown while waiting (i.e. if we disconnect).
        // TODO: Handle serialization errors.
        var state = await _socket.RecvMessageAsync <GameStateData>();

        Debug.LogFormat("Recieved initial state: {0}", state);
        Debug.LogFormat("Received initial state with {0} players", state.Players.Count);

        var players          = new Dictionary <int, GameObject>();
        var movementPreviews = new Dictionary <int, GameObject>();

        // Create objects in the world as necessary based on the initial game state
        // when we first connect to the server.
        foreach (var(id, player) in state.Players)
        {
            // Create an object in the world for the player and set it to the world position
            // that corresponds to their grid position.
            var playerInstance = await Addressables.Instantiate <GameObject>(_playerPrefab);

            playerInstance.transform.localPosition = player.Pos.WorldPos;

            players.Add(id, playerInstance);

            // Visualize the pending move action for the player, if they already have
            // one setup.
            var pendingMovement = player.PendingTurn?.Movement;
            if (pendingMovement.HasValue)
            {
                var movementPreview = await Addressables.Instantiate <GameObject>(_playerMovementPreviewPrefab);

                movementPreview.transform.localPosition = pendingMovement.Value.WorldPos;

                movementPreviews.Add(id, movementPreview);
            }
        }

        // Once the intial state has been received from the server, spawn two tasks to
        // run concurrently:
        //
        // * One to listen for and handle incoming messages from the server.
        // * One to handle player input every frame.
        var handleMessages = HandleMessages(players, movementPreviews);
        var handleInput    = HandleInput();

        await UniTask.WhenAll(handleMessages, handleInput);
    }
Example #6
0
    private async UniTask HandleMessages(Dictionary <int, GameObject> players, Dictionary <int, GameObject> movementPreviews)
    {
        while (true)
        {
            // TODO: Handle an exception being thrown while waiting (i.e. if we disconnect).
            // TODO: Handle serialization errors.
            var update = await _socket.RecvMessageAsync <Message>();

            switch (update.Type)
            {
            case MessageType.PlayerAdded:
                var playerAdded = update.Data.ToObject <PlayerAdded>();

                // Create an object in the world for the player and set it to the world position
                // that corresponds to their grid position.
                var playerInstance = await Addressables.Instantiate <GameObject>(_playerPrefab);

                playerInstance.transform.localPosition = playerAdded.Data.Pos.WorldPos;

                Debug.AssertFormat(!players.ContainsKey(playerAdded.Id), "Player with ID {0} already exists", playerAdded.Id);
                players.Add(playerAdded.Id, playerInstance);

                break;

            case MessageType.SetMovement:
                var setMovement = update.Data.ToObject <SetMovement>();

                // Get the existing preview object, or create a new one if one doesn't
                // already exist.
                GameObject movementPreview;
                if (!movementPreviews.TryGetValue(setMovement.Id, out movementPreview))
                {
                    movementPreview = await Addressables.Instantiate <GameObject>(_playerMovementPreviewPrefab);

                    movementPreviews.Add(setMovement.Id, movementPreview);
                }

                movementPreview.transform.localPosition = setMovement.Pos.WorldPos;

                break;
            }
        }
    }
        private static IEnumerator InstantiateCore <T>(
            object key, IObserver <T> observer, CancellationToken token, Transform parent = null, Action <IAsyncOperation <T> > onCompleted = null, bool isWorldSpace = false)
            where T : UnityEngine.Object
        {
            var aop = Addressables.Instantiate <T>(key, parent, isWorldSpace);

            if (onCompleted != null)
            {
                aop.Completed += onCompleted;
            }

            while (!aop.IsDone && !token.IsCancellationRequested)
            {
                // Debug.Log(1);
                yield return(null);
            }

            if (token.IsCancellationRequested)
            {
                yield break;
            }
            observer.OnNext(aop.Result);
            observer.OnCompleted();
        }
 public void InstantiateGameObject(string address, Transform parent = null, bool instantiateInWorldSpace = false)
 {
     Addressables.Instantiate <GameObject>(address, parent, instantiateInWorldSpace);
 }
Example #9
0
 private void Start()
 {
     //Loading Player;
     Addressables.Instantiate <GameObject>("player").Completed += LoadPlayer;
 }
Example #10
0
    void OnButtonClick()
    {
        var randSpot = new Vector3(Random.Range(-5, 1), Random.Range(-10, 10), Random.Range(0, 100));

        Addressables.Instantiate("ball", randSpot, Quaternion.identity);
    }
Example #11
0
 // Start is called before the first frame update
 void Start()
 {
     Addressables.Instantiate("Cube");
 }
Example #12
0
 public IAsyncOperation <GameObject> Instantiate(object key, Vector3 position, Quaternion rotation, Transform parent = null)
 {
     return(Addressables.Instantiate(key, position, rotation, parent));
 }
Example #13
0
 public IAsyncOperation <GameObject> Instantiate(object key, Transform parent = null, bool instantiateInWorldSpace = false)
 {
     return(Addressables.Instantiate(key, parent, instantiateInWorldSpace));
 }
Example #14
0
 public IAsyncOperation <GameObject> Instantiate(object key, InstantiationParameters instantiateParameters)
 {
     return(Addressables.Instantiate(key, instantiateParameters));
 }
Example #15
0
 public static IAsyncOperation Instantiate(IResourceLocation assetReference, Vector3 position, Quaternion rotation, Transform parent = null)
 {
     return(Addressables.Instantiate <GameObject>(assetReference, position, rotation, parent));
 }