Unity中的跳转按钮用于画布

时间:2015-09-29 16:08:48

标签: c# button unity3d

我正在寻找C#或Java中的代码或脚本,以便在下面的脚本中将我的多维数据集标记为sampleApp.run(function ($http, $templateCache) { $http.get('templates/show_order.html', { cache: $templateCache }); }); 跳转。

我已经编写了一些代码并将其附加到画布上的按钮上,但问题是当我按住按钮时,它会一直跳跃并且无限跳跃。

这是我用C#编写的脚本

Player

2 个答案:

答案 0 :(得分:1)

您可以使用unity coroutine

在例程开始时,你设置(例如)" isJumping" (一个博尔),然后在你开始你的循环位之前,你要通过检查' isJumping'来检查你是否正在跳跃。

如果不是" isJumping&#34 ;,将isJumping设置为true,执行跳转,然后在例程完成时将isJumping设置为false。

//untested (uncompiled) code written on the fly
bool isJumping = false;
IEnumerator doJump()
{
    if (!isJumping) {
        isJumping = true;
        // do jump (probably a loop)
        while (jumpCondition) {
            // jump instructions
            yield return
        }
        //unset isJumping, inside 'if' but after yield return 
        isJumping = false
    }
}

注意:协程中的yield return之后的代码只会(可能)运行一次,并且只在协程存在时运行(因为没有更多的结果意味着协程已经结束)

答案 1 :(得分:1)

勾选PointerDown(按下按钮时调用)和来自UIButton的PointerUp(按钮已经放开)事件,并使用Time.deltaTime加权对位置的翻译,你应该好好去。 (player.transform.Translate(0,1 * Time.deltaTime, 0),可选择将其与另一个速度调制因子相乘。)参考文献:http://unity3d.com/learn/tutorials/modules/beginner/ui/ui-events-and-event-triggers

编辑:是的,一些示例代码。首先,我在按钮上有一个EventTrigger组件。我使用这个,我可以挂钩前面描述的PointerDownPointerUp事件。它在检查员中看起来像这样:

unity

(使用"添加新事件类型"按钮重定向事件调用。) 然后,我在按钮上有这个脚本。代码说明了一切。

using UnityEngine;
using UnityEngine.EventSystems;

public class JumpButton : MonoBehaviour {

    private bool shouldJump = false;

    // Update is called once per frame
    void Update () {
        //Find the player
        var player = GameObject.FindGameObjectWithTag("Player");
        //No player? exit out.
        if (player == null)
            return;
        //Is the jump button currently being pressed?
        if (shouldJump)
        {
            //Translate it upwards with time.
            player.transform.Translate(new Vector3(0, Time.deltaTime * 5, 0));
            //Make sure the Rigidbody is kinematic, or gravity will pull us down again
            if (player.GetComponent<Rigidbody>().isKinematic == false)
                player.GetComponent<Rigidbody>().isKinematic = true;
        }
        //Not jumping anymore? reset the Rigidbody.
        else
            player.GetComponent<Rigidbody>().isKinematic = false;
    }

    //When the button is being pressed down, this function is called.
    public void ButtonPressedDown(BaseEventData e)
    {
        shouldJump = true;
    }

    //When the button is released again, this function is called.
    public void ButtonPressedUp(BaseEventData e)
    {
        shouldJump = false;
    }
}

切换到运动刚体的东西是可选的。此外,可以使用Translate()调用中的乘法常数来调整速度。我使用标准多维数据集测试了此代码,其中包含Player标记和Rigidbody,并且工作正常。

快乐的编码。