简体   繁体   中英

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. I have to make an APK each time to test the game. 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

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. On Linux, Mac, WebGL, this function will always return true. On iOS and console platforms, this function will always return false.

or alternatively also 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 . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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