Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 astrocoder · Feb 11, 2016 at 02:01 PM · c#for-loopforeachc# tutorialimplementation

ScrollableList best way to implement

Hello, This morning I was watching a tutorial on "UI Objects Created at Runtime" but the code that the tutorial has a feature where for-loop in which I do not see no way to adjust to my foreach code.

ScrollableList Class:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine.UI;
 
 public class ScrollableList : MonoBehaviour
 {
     public GameObject itemPrefab;
     public int itemCount = 10, columnCount = 1;
 
     void Start()
     {
         RectTransform rowRectTransform = itemPrefab.GetComponent<RectTransform>();
         RectTransform containerRectTransform = gameObject.GetComponent<RectTransform>();
 
         //calculate the width and height of each child item.
         float width = containerRectTransform.rect.width / columnCount;
         float ratio = width / rowRectTransform.rect.width;
         float height = rowRectTransform.rect.height * ratio;
         int rowCount = itemCount / columnCount;
         if (itemCount % rowCount > 0)
             rowCount++;
 
         //adjust the height of the container so that it will just barely fit all its children
         float scrollHeight = height * rowCount;
         containerRectTransform.offsetMin = new Vector2(containerRectTransform.offsetMin.x, -scrollHeight / 2);
         containerRectTransform.offsetMax = new Vector2(containerRectTransform.offsetMax.x, scrollHeight / 2);
 
         int j = 0;
         for (int i = 0; i < itemCount; i++)
         {
             //this is used instead of a double for loop because itemCount may not fit perfectly into the rows/columns
             if (i % columnCount == 0)
                 j++;
 
             //create a new item, name it, and set the parent
             GameObject newItem = Instantiate(itemPrefab) as GameObject;
             newItem.name = gameObject.name + " item at (" + i + "," + j + ")";
             newItem.transform.parent = gameObject.transform;
 
             //move and size the new item
             RectTransform rectTransform = newItem.GetComponent<RectTransform>();
 
             float x = -containerRectTransform.rect.width / 2 + width * (i % columnCount);
             float y = containerRectTransform.rect.height / 2 - height * j;
             rectTransform.offsetMin = new Vector2(x, y);
 
             x = rectTransform.offsetMin.x + width;
             y = rectTransform.offsetMin.y + height;
             rectTransform.offsetMax = new Vector2(x, y);
         }
 
 
     }
 
 }

My class:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine.UI;
 using UnityEngine.Events;
 
 public class ShopHandler : MonoBehaviour {
 
     int counter;
     public bool isShopSelected = false;
     public PlayerHandler player;
    
     public GameObject[] itemButtons;
 
     public Item[] shopItems;
 
     public GameObject button;
 
     // Use this for initialization
     void Start () {
         itemButtons = new GameObject[shopItems.Length];
 
         counter = 0;
         foreach(Item i in shopItems) {
             GameObject btn = (GameObject) Instantiate(button);
             ItemScript scp = btn.GetComponent<ItemScript>();
 
             itemButtons[counter] = btn;
 
             scp.name.text = i.Name;
             scp.cost.text = "Cost: " + i.Cost.ToString("F3");
             scp.gain.text = "CPS: " + i.Gain.ToString("F1");
             scp.type.text = i.type.ToString();
             scp.level.text = i.Number.ToString();
             scp.icon.sprite = i.Icon;
 
             Item itemBought = i;
 
             scp.thisButton.onClick.AddListener(() => Purchase(itemBought));
             //scp.thisButton.onClick = i.btnClicked;
             btn.transform.SetParent(this.transform);
 
             counter++;
         }
 
         
     }
     
     void Purchase(Item bought) {
         
         if(bought.type == Item.Type.Console) {
            
         } else if(bought.type == Item.Type.Upgrade) {
             if(bought.Number == 10) {
                 return;
             } else {
                 bought.Number++;
             }
 
             bought.Cost *= 1.3f;
         }
        
     }
 
     void UpdateItems() {
         counter = 0;
         foreach(Item i in shopItems) {
             
             ItemScript scp = itemButtons[counter].GetComponent<ItemScript>();
 
             
 
             scp.name.text = i.Name;
             scp.cost.text = "Cost: " + i.Cost.ToString("F1");
             scp.gain.text = "CPS: " + i.Gain.ToString("F1");
             scp.type.text = i.type.ToString();
             if(i.Number == 10) {
                 scp.level.text = "✔";
             } else {
                 scp.level.text = i.Number.ToString();
             }
             scp.icon.sprite = i.Icon;
 
             
 
             counter++;
         }
     }
 
     // Update is called once per frame
     void Update () {
         UpdateItems();
 
         counter = 0;
         foreach(Item i in shopItems) {
             if(i.Cost > player.totalConsoles) {
                 itemButtons[counter].GetComponent<Button>().interactable = false;
             } else {
                 itemButtons[counter].GetComponent<Button>().interactable = true;
             }
 
             counter++;
         }
     }
 }

*Im a little noob on c# language

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

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Foreach Statement Random Sequence/Order? 3 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

foreach (or for-next loop) not updating local values 1 Answer

Converting foreach touch into for. 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