2

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

これは、scrollViewが画面の右半分にあると想定して行われました。

皆さん、これについて私を助けてくれませんか?

前もって感謝します。

4

2 に答える 2

3

GUI.BeginScrollViewのAPIドキュメントに示されているように、 BeginScrollViewの宣言にVector2を割り当てて、ボックスがスクロールされた場所を追跡および更新できます。

例を挙げましょう:

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 x 600であると仮定します。したがって、画面に表示されるスクロールボックスは、幅800、高さ600で、スクロールボックスの内部領域は1600x1800になります。

Vector2、scrollPositionの値としてGui.BeginScrollView()を割り当てることにより、スクロールボックスを作成します。スクロールボックスが水平方向および垂直方向にスクロールされると、Vector2はスクロールされた値で更新されます。

スクロールボックスを左上の位置にスクロールすると、scrollPositionの値は0,0になります。

スクロールボックスを右下の位置にスクロールすると、scrollPositionの値は816、616、画面上のボックスのサイズにスクロールバーの太さを加えたものになります。

于 2012-07-22T14:15:22.537 に答える