- Home /
How can I make an array fill the contents of my GUI List?
Hello Unity Community,
I am very new to Unity and coding, and would really, really appreciate any bit of help.
I have a scrollable GUI List (from http://www.mindthecube.com/blog/2010/09/adding-iphone-touches-to-unitygui-scrollview). I put the code at the bottom of this post. I am trying to make the list be a descending list of the activities from which the player has received XP, and how many points the player received from that activity. For example:
Activity 5: 876
Activity 4: 554
Activity 1: 332
Activity 7: 154
and so forth.
I was thinking that I should make an array (not sure if it should be JavaScript Array or Generic List) that would collect all this information somehow (i.e. if I click a certain button, I get XP and this somehow gets entered into the array).
My first question is how I could make the contents of the GUI list be filled with an array??
My next question is how could I make the array get the information that I want it to?
Is there a better way to achieve this? Any pointers in the right direction would be much appreciated!!!
Thank you very, very much for any and all help; it really is much appreciated!
This is the script I am using for my GUI window:using UnityEngine; using System.Collections;
[ExecuteInEditMode]
public class GUITouchScroll : MonoBehaviour {
 public GUISkin optionsSkin;
 public GUIStyle rowSelectedStyle;
 
 // Internal variables for managing touches and drags
 private int selected = -1;
 private float scrollVelocity = 0f;
 private float timeTouchPhaseEnded = 0f;
 private float previousDelta = 0f;
 private const float inertiaDuration = 0.5f;
 
 public Vector2 scrollPosition;
 // size of the window and scrollable list
 public int numRows;
 public Vector2 rowSize;
 public Vector2 windowMargin;
 public Vector2 listMargin;
 public Texture rowTexture;
 
 private Rect windowRect;
 void Update()
 {
     if (iPhoneInput.touchCount != 1)
     {
         selected = -1;
         if ( scrollVelocity != 0.0f )
         {
             // slow down over time
             float t = (Time.time - timeTouchPhaseEnded) / inertiaDuration;
             float frameVelocity = Mathf.Lerp(scrollVelocity, 0, t);
             scrollPosition.y += frameVelocity * Time.deltaTime;
             
             // after N seconds, we've stopped
             if (t >= inertiaDuration) scrollVelocity = 0.0f;
         }
         return;
     }
     
     iPhoneTouch touch = iPhoneInput.touches[0];
     if (touch.phase == iPhoneTouchPhase.Began)
     {
         selected = TouchToRowIndex(touch.position);
         previousDelta = 0.0f;
         scrollVelocity = 0.0f;
     }
     else if (touch.phase == iPhoneTouchPhase.Canceled)
     {
         selected = -1;
         previousDelta = 0f;
     }
     else if (touch.phase == iPhoneTouchPhase.Moved)
     {
         // dragging
         selected = -1;
         previousDelta = touch.deltaPosition.y;
         scrollPosition.y += touch.deltaPosition.y;
     }
     else if (touch.phase == iPhoneTouchPhase.Ended)
     {
         // Was it a tap, or a drag-release?
         if ( selected > -1 )
         {
             Debug.Log("Player selected row " + selected);
         }
         else
         {
             // impart momentum, using last delta as the starting velocity
             // ignore delta < 10; precision issues can cause ultra-high velocity
             if (Mathf.Abs(touch.deltaPosition.y) >= 10) 
                 scrollVelocity = (int)(touch.deltaPosition.y / touch.deltaTime);
             timeTouchPhaseEnded = Time.time;
         }
     }
     
 }
 void OnGUI ()
 {
     GUI.skin = optionsSkin;
     
     windowRect = new Rect(40,140,200,185);
     GUI.Window(0, windowRect, (GUI.WindowFunction)DoWindow, "How I'm Livin' Time Tracker");
 }
 void DoWindow (int windowID) 
 {
     Vector2 listSize = new Vector2(windowRect.width - 2*listMargin.x,
                                    windowRect.height - 2*listMargin.y);
     Rect rScrollFrame = new Rect(listMargin.x, listMargin.y, listSize.x, listSize.y);
     Rect rList = new Rect(0, 0, rowSize.x, numRows*rowSize.y);
     
     scrollPosition = GUI.BeginScrollView (rScrollFrame, scrollPosition, rList, false, false);
         
     Rect rBtn = new Rect(0, 0, rowSize.x, rowSize.y);
     for (int iRow = 0; iRow < numRows; iRow++)
     {
            // draw call optimization: don't actually draw the row if it is not visible
         if ( rBtn.yMax >= scrollPosition.y && 
              rBtn.yMin <= (scrollPosition.y + rScrollFrame.height) )
            {
             bool fClicked = false;
                string rowLabel = BuiltInArray;
                 //"Row Number " + iRow;//
             
             GUIContent contents = new GUIContent(rowLabel, rowTexture);
                
                if ( iRow == selected )
                {
                 fClicked = GUI.Button(rBtn, contents, rowSelectedStyle);
                }
                else
             {
                    fClicked = GUI.Button(rBtn, contents);
             }
             
             // Allow mouse selection, if not running on iPhone.
             // Note: this code will be triggered 
             if ( fClicked && Application.platform != RuntimePlatform.IPhonePlayer )
             {
                Debug.Log("Player mouse-clicked on row " + iRow);
             }
            }
                        
         rBtn.y += rowSize.y;
     }
     GUI.EndScrollView();
 }
 private int TouchToRowIndex(Vector2 touchPos)
 {
     float y = Screen.height - touchPos.y;  // invert coordinates
     y += scrollPosition.y;  // adjust for scroll position
     y -= windowMargin.y;    // adjust for window y offset
     y -= listMargin.y;      // adjust for scrolling list offset within the window
     int irow = (int)(y / rowSize.y);
     
     irow = Mathf.Min(irow, numRows);  // they might have touched beyond last row
     return irow;
 }
}
Answer by Berenger · Feb 04, 2012 at 12:29 AM
I didn't read the code, but I can still put you on the right track : http://unity3d.com/support/documentation/ScriptReference/GUILayout.BeginScrollView.html
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                