简体   繁体   English

如何更改以下 Unity C# 游戏对象左/右滑动代码以进行鼠标单击?

[英]How can I change the following Unity C# gameobject left/right swipe code for Mouse clicks?

I want to test the game on Unity editor that's why I want to change the following code for mouse click as 'touch' don't work in unity editor.我想在 Unity 编辑器上测试游戏,这就是为什么我想更改鼠标单击的以下代码,因为“触摸”在统一编辑器中不起作用。 I have to make an APK each time to test the game.我每次都必须制作一个APK来测试游戏。 Please help me with this.请帮我解决一下这个。

float swipespeed = 0.002f;
void Update 
{
    if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
    {
        // Get movement of the finger since last frame
        Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
        float pos=touchDeltaPosition.x;
        // Move object across XY plane
        transform.Translate( 0f,0f,-pos * swipespeed);
    }
}

Assuming the rest works as you need you could check Input.mousePresent假设 rest 按您的需要工作,您可以检查Input.mousePresent

Indicates if a mouse device is detected.指示是否检测到鼠标设备。

On Windows, Android and Metro platforms, this function does actual mouse presence detection, so may return true or false.在 Windows、Android 和 Metro 平台上,此 function 执行实际的鼠标存在检测,因此可能返回 true 或 false。 On Linux, Mac, WebGL, this function will always return true.在 Linux、Mac、WebGL 上,此 function 将始终返回 true。 On iOS and console platforms, this function will always return false.在 iOS 和控制台平台上,此 function 将始终返回 false。

or alternatively also Input.touchSupported或者还有Input.touchSupported

Returns whether the device on which application is currently running supports touch input.返回当前运行应用程序的设备是否支持触摸输入。

Rather than checking the platform, use this property to determine whether your game should expect touch input, as some platforms can support multiple input methods.无需检查平台,而是使用此属性来确定您的游戏是否应该期待触摸输入,因为某些平台可以支持多种输入法。

and do something like并做类似的事情

public float swipeSpeed = 0.002f;

// Here every frame the last mousePosition will be stored
// so we can compare the current one against it
private Vector3 lastMousePos;

private void Update 
{
    if(Input.mousePresent)
    {
        if(Input.GetMouseButton(0))
        {
            var currentMousePos = Input.mousePosition;
            var mouseDeltaPosition = currentMousePos - lastMousePos;

            transform.Translate( 0f,0f, -mouseDeltaPosition.x * swipespeed);
        }

        // update the last position with the current
        lastMousePos = currentMousePos;
    }
    else
    {
        if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
        {
            // Get movement of the finger since last frame
            var touchDeltaPosition = Input.GetTouch(0).deltaPosition;

            // Move object across XY plane
            transform.Translate( 0f,0f, -touchDeltaPosition.x * swipespeed);
        }
    }
}

Rather than build a monolithic if/else structure that will compile into your code and be executed by all platforms, consider lookin at Platform Dependent Compilation .与其构建一个整体的 if/else 结构来编译到您的代码中并由所有平台执行,不如考虑查看Platform Dependent Compilation This allows you to write code that compiles differently for specific environments.这允许您编写针对特定环境进行不同编译的代码。

For example:例如:

public float swipeSpeed = 0.002f;
#if UNITY_EDITOR
    private Vector3 lastMousePos;
#endif

private void Update 
{
    bool touch;
    Vector3 deltaPosition;

    #if UNITY_EDITOR
        touch = Input.GetMouseButton(0);
        if(touch)
        {
            deltaPosition = Input.mousePosition - lastMousePos;
            lastMousePos = Input.mousePosition;
        }
    #else
        touch = (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved);
        if(touch)
        {
            deltaPosition = Input.GetTouch(0).deltaPosition
        }
    #endif


    if(touch)
    {
        transform.Translate( 0f,0f, -deltaPosition.x * swipespeed);
    }
}

Update: Since apparently telling someone to drop their ego gets my comment deleted, no this is not a duplicate of his/her answer.更新:由于显然告诉某人放弃他们的自我,我的评论被删除了,不,这不是他/她的答案的重复。 This is best practice for multi-platform development and differs significantly from his/her answer.这是多平台开发的最佳实践,与他/她的回答有很大不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM