【问题标题】:Unity Android UI Buttons to Replace WASD Controls For SnakeUnity Android UI 按钮替换 Snake 的 WASD 控件
【发布时间】:2020-05-27 05:40:12
【问题描述】:

我对编程和所有东西都是新手。我在 youtube 上观看了如何创建蛇游戏,我想为我的按钮添加一个脚本来移动播放器。我创建了ui buttons like this,但是当我点击它们时,当然没有任何反应。我为跨平台添加了简单的资产,所有这些都完成了,但我不知道如何编写脚本。我可以用我的W A S D 键移动蛇,但我想用 UI 按钮移动。这是我的脚本:

 private void Update()
    {

        if (isGameOver)
        {
            if (Input.GetKeyDown(KeyCode.R) || Input.GetMouseButtonDown(0))
            {
                onStart.Invoke();
            }
            return;
        }
        GetInput();

        if (isFirstInput)
        {
            SetPlayerDirection();

            timer += Time.deltaTime;
            if (timer > moveRate)
            {
                timer = 0;
                curDirection = targetDirection;
                MovePlayer();
            }
        }
        else
        {
            if (up || down || left || right)
            {

                isFirstInput = true;
                firstInput.Invoke();
            }
        }
    }

    void GetInput()
    {
        up = Input.GetButtonDown("Up");
        down = Input.GetButtonDown("Down");
        left = Input.GetButtonDown("Left");
        right = Input.GetButtonDown("Right");
    }

    void SetPlayerDirection()
    {
        if (up)
        {
            SetDirection(Direction.up);
        }
        else if (down)
        {
            SetDirection(Direction.down);
        }
        else if (left)
        {
            SetDirection(Direction.left);
        }
        else if (right)
        {
            SetDirection(Direction.right);
        }
    }

    void SetDirection(Direction d)
    {
        if (!isOpposite(d))
        {
            targetDirection = d;
        }
    }

    void MovePlayer()
    {

        int x = 0;
        int y = 0;

        switch (curDirection)
        {

            case Direction.up:
                y = 1;
                break;
            case Direction.down:
                y = -1;
                break;
            case Direction.left:
                x = -1;
                break;
            case Direction.right:
                x = 1;
                break;

        }

        Node targetNode = GetNode(playerNode.x + x, playerNode.y + y);
        if (targetNode == null)
        {
            //Game Over
            onGameOver.Invoke();
        }
        else
        {
            if (isTailNode(targetNode))
            {
                //GameOver
                onGameOver.Invoke();
            }
            else
            {



                bool isScore = false;

                if (targetNode == appleNode)
                {
                    isScore = true;
                }

                Node previousNode = playerNode;
                availbableNodes.Add(previousNode);


                if (isScore)
                {
                    tail.Add(CreateTailNode(previousNode.x, previousNode.y ,tailParent));
                    availbableNodes.Remove(previousNode);
                }

                MoveTail();
                PlacePlayerObject(playerObj, targetNode.worldPosition);
                playerNode = targetNode;
                availbableNodes.Remove(playerNode);

                if (isScore)
                {

                    currentScore++;
                    if(currentScore >= highScore)
                    {
                        highScore = currentScore;
                    }

                    onScore.Invoke();

                    if (availbableNodes.Count > 0)
                    {
                        RandomlyPlaceApple();
                    }
                    else
                    {
                        //you won
                    }
                }
            }
        }
    }

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    这应该很简单。您可以创建方法来处理事件。例如,假设您想将SetDirection(Direction.up); 绑定到“向上按钮”,您可以为此创建一个方法。

    public void UpDirection(){
    SetDirection(Direction.up);
    }
    

    返回编辑器,单击向上按钮,通过单击 + 号添加 OnClick 事件。将脚本拖到空槽中,然后您应该从下拉菜单中选择相关函数。我希望你得到这个?

    【讨论】:

      【解决方案2】:

      对于初学者,Unity has a great ~13 minute video on using UI Buttons。观看此视频后,您将了解如何将 UI 按钮连接到事件(如果您想跳过按钮工作方式的视觉细节,请在 8:25 观看)。接下来,您需要在脚本中为四个方向按钮添加四个函数,如下所示:

      public void UpButtonPressed()
      {
          SetDirection(Direction.up);
      }
      public void DownButtonPressed()
      {
          SetDirection(Direction.down);
      }
      public void LeftButtonPressed()
      {
          SetDirection(Direction.left);
      }
      public void RightButtonPressed()
      {
          SetDirection(Direction.right);
      }
      

      然后,只需将它们附加到检查器中正确按钮的 OnClick 事件(在 8:25 的视频中描述)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多