c# - 如何在 unity3d 中创建触摸屏 android 滚动条?

标签 c# android unity3d

我想用 Unity3d 创建一个 Android 游戏。这个游戏有一个带有可触摸卷轴的升级列表。我使用这段代码来创建它,但是当我在触摸屏上移动手指时,用力滚动并跳跃,我希望它能轻柔地移动,就像 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(); 

最佳答案

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;
            }
        }
    }
}

关于c# - 如何在 unity3d 中创建触摸屏 android 滚动条?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20441090/

相关文章:

android - 无法在 eclipse 中导入 NineOldAndroids

c# - 将值从 Controller 发送到 Jquery

c# - WCF 调用者未通过 WsDualHttpBinding 中的服务进行身份验证

c# - 如何通过使用反射设置双泛型来执行泛型方法

安卓 ListView : getTag() returns null

android - 房间查询返回null

c# - Unity插件:编码(marshal)C++ double *将C#中的数组翻倍

c++ - 将使用太多纹理插值器 - 带旋转的着色器

C#:可空结构的默认文字和类型推断

c# - Unity Universal Render Pipeline 性能问题? (二维)