如何在unity3d中创建触摸屏android滚动?

时间:2013-12-07 11:58:56

标签: android unity3d unityscript

我想用Unity3d创建一款Android游戏。这个游戏有一个可触摸滚动的升级列表。我使用这个代码进行创建但是当在触摸屏中移动手指时,滚动移动并且使用跳跃,我想要轻柔地移动并且像安卓效果一样。

scrollPosition1 = GUI.BeginScrollView(Rect (0,400,Screen.width,175), 
                                      scrollPosition1, Rect (0, 0, 650, 0)); 
    // touch screen 
    if(Input.touchCount==1 && 
       Screen.height -Input.GetTouch(0).position.y >  450 - scrollPositionHome.y && 
       Screen.height - Input.GetTouch(0).position.y < 600 - scrollPositionHome.y)
    {
        var touchDelta2 : Vector2 = Input.GetTouch(0).deltaPosition;
        scrollPosition1.x +=touchDelta2.x;
    }

    for (i=0;i < ImgSliderProducts.Length;i++)
    {
        GUI.DrawTexture(Rect(20+(i* 100),10,100,100), 
                        ImgSliderProducts[i],ScaleMode.ScaleToFit,true);
    }

GUI.EndScrollView(); 

1 个答案:

答案 0 :(得分:14)

using UnityEngine;
using System.Collections;

public class scrollView : MonoBehaviour {


    Vector2 scrollPosition;
    Touch touch;
    // The string to display inside the scrollview. 2 buttons below add & clear this string.
    string longString = "This is a long-ish string";

    void OnGUI () { 

        scrollPosition = GUI.BeginScrollView(new Rect(110,50,130,150),scrollPosition, new Rect(110,50,130,560),GUIStyle.none,GUIStyle.none);

        for(int i = 0;i < 20; i++)
        {
            GUI.Box(new Rect(110,50+i*28,100,25),"xxxx_"+i);
        }
        GUI.EndScrollView ();
    }

    void Update()
    {
        if(Input.touchCount > 0)
        {
            touch = Input.touches[0];
            if (touch.phase == TouchPhase.Moved)
            {
                scrollPosition.y += touch.deltaPosition.y;
            }
        }
    }
}