示例#1
0
    /// <summary>
    /// Queue a command to get a GameObject's transform
    /// </summary>
    /// <param name="go">
    /// GameObject to get the transform from. Must not be null.
    /// </param>
    /// <param name="result">
    /// Result to be filled in when the command executes. Must not be null.
    /// </param>
    public void GetTransform(
        GameObject go,
        Result <Transform> result)
    {
        Assert.IsTrue(go != null);
        Assert.IsTrue(result != null);

        result.Reset();
        GetTransformCommand cmd = GetFromPool(getTransformPool);

        cmd.GameObject = go;
        cmd.Result     = result;
        QueueCommand(cmd);
    }
示例#2
0
    /// <summary>
    /// Execute commands until there are none left or a maximum time is used
    /// </summary>
    /// <param name="maxMilliseconds">
    /// Maximum number of milliseconds to execute for. Must be positive.
    /// </param>
    public void Execute(int maxMilliseconds = int.MaxValue)
    {
        Assert.IsTrue(maxMilliseconds > 0);

        // Process commands until we run out of time
        executeLimitStopwatch.Reset();
        executeLimitStopwatch.Start();
        while (executeLimitStopwatch.ElapsedMilliseconds < maxMilliseconds)
        {
            // Get the next queued command, but stop if the queue is empty
            BaseCommand baseCmd;
            lock (commandQueue)
            {
                if (commandQueue.Count == 0)
                {
                    break;
                }
                baseCmd = commandQueue.Dequeue();
            }

            // Process the command. These steps are followed for each command:
            // 1. Extract the command's fields
            // 2. Reset the command's fields
            // 3. Do the work
            // 4. Return the command to its pool
            // 5. Make the result ready
            switch (baseCmd.Type)
            {
            case CommandType.NewGameObject:
            {
                // Extract the command's fields
                NewGameObjectCommand cmd = (NewGameObjectCommand)baseCmd;
                string name = cmd.Name;
                Result <GameObject> result = cmd.Result;

                // Reset the command's fields
                cmd.Name   = null;
                cmd.Result = null;

                // Return the command to its pool
                ReturnToPool(newGameObjectPool, cmd);

                // Do the work
                GameObject go = new GameObject(name);

                // Make the result ready
                result.Ready(go);
                break;
            }

            case CommandType.GetTransform:
            {
                // Extract the command's fields
                GetTransformCommand cmd    = (GetTransformCommand)baseCmd;
                GameObject          go     = cmd.GameObject;
                Result <Transform>  result = cmd.Result;

                // Reset the command's fields
                cmd.GameObject = null;
                cmd.Result     = null;

                // Return the command to its pool
                ReturnToPool(getTransformPool, cmd);

                // Do the work
                Transform transform = go.transform;

                // Make the result ready
                result.Ready(transform);
                break;
            }

            case CommandType.SetPosition:
            {
                // Extract the command's fields
                SetPositionCommand cmd       = (SetPositionCommand)baseCmd;
                Transform          transform = cmd.Transform;
                Vector3            position  = cmd.Position;
                Result             result    = cmd.Result;

                // Reset the command's fields
                cmd.Transform = null;
                cmd.Position  = Vector3.zero;
                cmd.Result    = null;

                // Return the command to its pool
                ReturnToPool(setPositionPool, cmd);

                // Do the work
                transform.position = position;

                // Make the result ready
                result.Ready();
                break;
            }
            }
        }
    }