Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Layout Unity3D滚动视图滚动范围_Layout_Position_Scrollview_Unity3d - Fatal编程技术网

Layout Unity3D滚动视图滚动范围

Layout Unity3D滚动视图滚动范围,layout,position,scrollview,unity3d,Layout,Position,Scrollview,Unity3d,我试图创建一个scrollView,但我不知道如何确定滚动的最大范围(或者更好,最大滚动位置) 这就是我在没有任何积极结果的情况下尝试做的事情: void Update() { //get the amount of space that my text is taking textSize = gs.CalcHeight(new GUIContent(text), (screenWidth/2)); if(Input.touchCount > 0 && In

我试图创建一个scrollView,但我不知道如何确定滚动的最大范围(或者更好,最大滚动位置)

这就是我在没有任何积极结果的情况下尝试做的事情:

void Update()
{
  //get the amount of space that my text is taking
  textSize = gs.CalcHeight(new GUIContent(text), (screenWidth/2));

  if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved && Input.GetTouch(0).tapCount==1) 
  {
    var touch = Input.touches[0];

   //check if touch is within the scrollView area
   if(touch.position.x >= (screenWidth/2) && touch.position.x <= (screenWidth) && touch.position.y >= 0 && touch.position.y <= (screenHeight))
   {
     if(touch.phase == TouchPhase.Moved)
     {
       scrollPosition[1] += touch.deltaPosition.y;
       if(scrollPosition[1] < 0)
       {
           scrollPosition[1] = 0;
       }

       //I subtracted this cause i read that the scrollbars take 16px
       if(scrollPosition[1] > (textSize-scrollViewRect.yMax-16)) //scrollViewRect is a Rect with the size of my scrollView
       {
          scrollPosition[1] = textSize-scrollViewRect.yMax-16;
       }
     }
   }
  }
}

void OnGUI()
{
 screenWidth = camera.pixelWidth;
 screenHeight = camera.pixelHeight;
 GUILayout.BeginArea(new Rect(screenWidth/2, 0, screenWidth/2, screenHeight));

 GUILayout.BeginScrollView(scrollPosition/*, GUILayout.Width (screenWidth/2), GUILayout.Height (screenHeight/2)*/, "box");

 GUILayout.Label (new GUIContent(text), gs);

 // End the scrollview
 GUILayout.EndScrollView ();
 scrollViewRect = GUILayoutUtility.GetLastRect();
 GUILayout.EndArea();
}
void Update()
{
//获取文本占用的空间量
textSize=gs.CalcHeight(新GUIContent(text),(屏幕宽度/2));
if(Input.touchCount>0&&Input.GetTouch(0.phase==TouchPhase.Moved&&Input.GetTouch(0.tapCount==1)
{
var touch=Input.touch[0];
//检查触摸是否在scrollView区域内
如果(touch.position.x>=(屏幕宽度/2)和&touch.position.x=0和&touch.position.y(textSize-scrollViewRect.yMax-16))//scrollViewRect是具有我的scrollView大小的矩形
{
scrollPosition[1]=textSize-scrollViewRect.yMax-16;
}
}
}
}
}
void OnGUI()
{
screenWidth=camera.pixelWidth;
屏幕高度=相机像素高度;
BeginArea(新的Rect(屏幕宽度/2,0,屏幕宽度/2,屏幕高度));
GUILayout.BeginScrollView(滚动位置/*,GUILayout.Width(屏幕宽度/2),GUILayout.Height(屏幕高度/2)*/,“框”);
GUILayout.Label(新GUIContent(text),gs);
//结束滚动视图
GUILayout.EndScrollView();
scrollViewRect=GUILayoutUtility.GetLastRect();
GUILayout.EndArea();
}
这是在假定scrollView位于屏幕的右半部分时完成的

你们能帮我一下吗


提前感谢。

如的API文档所示,您可以为BeginScrollView声明分配一个向量2,以跟踪和更新框滚动到的位置

我举个例子:

using UnityEngine;
using System.Collections;

public class ScrollSize : MonoBehaviour {

private int screenBoxX  = Screen.width;
private int screenBoxY  = Screen.height;

private int scrollBoxX  = Screen.width  * 2;
private int scrollBoxY  = Screen.height * 3;

public Vector2 scrollPosition = Vector2.zero;

void Awake()
{
    Debug.Log ("box size is " + scrollBoxX + " " + scrollBoxY); 
}

void OnGUI()     
{
    scrollPosition = GUI.BeginScrollView(new Rect(0, 0, screenBoxX, screenBoxY),
                         scrollPosition, new Rect(0, 0, scrollBoxX, scrollBoxY));

    GUI.EndScrollView();

    // showing 4 decimal places
    Debug.Log ("Box scrolled @ " + scrollPosition.ToString("F4"));
}
}
为了解释这个例子,我们假设屏幕分辨率是800×600。 因此,屏幕上的可视滚动框宽800,高600,滚动框的内部面积为1600×1800

我们通过将Gui.BeginScrollView()指定为Vector2,scrollPosition的值来创建scrollbox。当滚动框水平和垂直滚动时,矢量2将使用滚动值进行更新

如果滚动框滚动到最左上方的位置,scrollPosition的值将为0,0


如果滚动框滚动到最右下角的位置,scrollPosition的值将是816616,屏幕上框的大小加上滚动条的厚度。

我知道这是3年后的事,但也许它也会帮助其他人

我发现将
scrollPosition
设置为一个非常高的数字将强制滚动条底部:

scrollPosition.y = 999.9f;
然后你可以这样读位置:

scrollPosition = GUILayout.BeginScrollView(scrollPosition, 
                                           GUILayout.Width(370),  
                                           GUILayout.Height(135)); 
它将被设置为最大值


希望这有帮助

我遇到的问题不是卷轴在工作。问题是,我不知道文本可以改变时占用了多少空间,所以我尝试使用CalcHeight函数获取它。我不直接给BeginScrollView分配Vector2变量,因为我想让整个文本可以滚动…我的意思是,我不想让文本只能通过滚动条滚动。那么,恐怕问题就不清楚了。双重发布不完整的代码(缺少变量)这一事实使得理解您想要的内容变得更加困难。我假设你想要一个ScrollView,它可以随着更多文本的添加而扩展ScrollView的内部区域?正如你所看到的,我没有双重发布,这篇文章是由版主编辑的。变量文本从数据库中检索,在用户进入该场景后不会更改。问题是我不知道滚动位置可以取的最大值。因此,正确地知道用户可以滚动的方式比它应该的多,或者,换句话说,如果用户向下滚动很多次,然后尝试再次向上滚动,则需要一些时间才能看到文本真正向上滚动。如果您有固定的项,则可以计算,例如,绘制框内边界标签列表,然后计算项目数量*框高度。我以前用过这个把戏。