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 MCoburn · Nov 19, 2014 at 01:47 AM · uibuttonsscrollview4.6

4.6 Beta uGUI: Putting UI elements in a ScrollRect via code

For reference:

 ScrollView (Parent Object)
 - Content (Child Object of ScrollView)
 -- Server Button 1 (Child Object of Content Object)
 -- Server Button 2 (Child Object of Content Object)
 -- Server Button 3 (Child Object of Content Object)
 ...and so forth.

I'm trying to make a server list window, done code-wise by spawning a set number of buttons inside a ScrollRect. Here's my snippet of code, that gets triggered when the server list is updated from the master server.

     // NOTE: uiSLButtonPrefab is a button that has been saved as a prefab with the correct settings.
     // uiServerListGO is the actual reference to the uiServerList scroll window, which is a child of the ScrollRect.
     // uiButtonSize is the size of the button (in this case, 30 pixels).
     
     void DrawServerList()
         {
             // Step 1: Purge all the things.
             if(!uiSLButtonPrefab) return;
 
             GameObject[] toPurge = GameObject.FindGameObjectsWithTag("SrvListEntry");
             for (int i = 0; i < toPurge.Length - 1; i++) Destroy(toPurge[i].gameObject);
     
             // Step 2: For as many items we have, do the prefabs for them.
             // Not actually the real deal, done for testing.
             for (int i = 0; i < 100; i++)
             {
                 GameObject nButton = Instantiate(uiSLButtonPrefab) as GameObject;
                 nButton.transform.parent = uiServerListGO.transform;
                 nButton.transform.position = new Vector2(0, i * (uiButtonSize / 2)); // <- likely the issue but I could be wrong!
             }
     
             // Step 3, done here.
         }

The code partially works. As a result, the buttons are spawned correctly and parented under the ScrollRect. However, they do not appear. Looking at the inspector reveals that they are being instantiated at something like -764 on the left and -414 on the vertical axis. When I drag these back into view, the editor shows the button with red lines through it, like it's corrupt or something. I'm not sure if I'm meant to be playing with the actual button's transform position, since buttons actually use a RectTransform. Has anyone done code-based buttons using the new UI system, and if so, could they please assist?

Cheers.

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 Kiwasi · Nov 19, 2014 at 04:36 AM

Bunch of things to note

  • The red x simply means the rectTransform limits are not valid. This is typically when the top is lower then the bottom, or the right coordinate falls to the left of the left coordinate

  • The easiest way to position a UI element is to use the layout elements

  • You can also modify its position manually, check out anchorMax, anchorMin, offsetMax and offsetMin. Docs here. Let me know if you are interested in seeing example code.

I also have a couple of video tutorials that might help.

  • Menu with layout elements

  • Menu buttons from script

Comment
Add comment · Show 1 · 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
avatar image MCoburn · Nov 23, 2014 at 11:36 PM 0
Share

If you can provide sample code, that would be great. I've set the anchors in the prefab to be at the top and set the width and height of the button, but all I would need to do is just change the Y of the button itself, so it appears after each other. I'll go take a look at the video tutorials and see what insight they provide.

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

unity 4.6 new UI Android soft buttons 1 Answer

[4.6] Add scroll wheel functionality to scroll rect 2 Answers

Dynamically adding (prefab) Buttons to a ScrollView 0 Answers

[4.6][UI] Render component only over parent panel. 1 Answer

HELP! Buttons stop working when made a child of a "Canvas Group" empty in 4.6.3f1? 4 Answers


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