Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by kayzee · Feb 03, 2012 at 01:21 AM · guiarraylistarrays

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

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Reading all data in the list with duplicate data inside 1 Answer

How to return index of List element? 1 Answer

Is there a way to remove array entries in the editor? 4 Answers

Which is better for an inventory system, Array or List? 0 Answers

How to create an array of Transforms 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges