• 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 MoCubed · Jun 21, 2015 at 05:53 PM · 4.6

Unity 4.6 UI - Have to resize canvas for Layout to affect child elements?

I have been working on this all day, and I think that it's time I asked someone about the problem with the new UI system that I'm currently having. It seems as though when I start my project, any new UI elements I create through script are do not adhere to the layout component rules until I resize my debug window in the Unity Editor, or change the RectTransform in some way (like change the position or size of the created element).

I have made a panel (List), with a second panel (ListItems) and a label (ListTitle) as its children. I put a layout element on the ListItems GameObject to mange the size of its parent, and I use script to add a number of buttons with layout managers. The problem is, the first button created does not have the same anchor as the prefab being created with Instantiate, and the subsequent buttons are appearing off the side of the screen. I have a screenshot of what I happening on the client as soon as I start the project.

alt text

Then if I adjust the size of the game window even slightly - everything snaps into place as expected. I know that it's a minor issue, but to expect that to be the functionality means that I'm doing something wrong, and I am completely lost as to what.

    private void createSimpleCharacter (CharacterListManager targetList)
     {
         GameObject details = (GameObject)GameObject.Instantiate (characterTemplate.gameObject);
         var script = details.GetComponent<CharacterDetailScript> ();
         script.SetInitialValues (10, 10, 10, 10, 10, 10, 10);
         //create the assocated button
         GameObject button = (GameObject)GameObject.Instantiate (buttonTemplate.gameObject);
         var buttonScript = button.GetComponent<TargetableCharacterButtonScript> ();  
         //have the canvas figure out how to place buttons for everyone      
         targetList.GetComponent<CharacterListManager> ().Add (buttonScript);
     }
   public void SetupSimple ()
     {
         //create an enemy list
         GameObject eList = (GameObject)GameObject.Instantiate (listTemplate.gameObject);
         eList.transform.parent = Gui.transform;
 
         eList.name = "Enemy List";
         teams.Add (eList);
         var manager = eList.GetComponent<CharacterListManager> ();
         //position the list on the screen
         eList.GetComponent<RectTransform> ().anchoredPosition = new Vector3 (Screen.width - 140, 0, 0);
         manager.labelText = "Enemies";
         //create 1-3 enemies
         int rand = Random.Range (1, 3);
         for (int i = 0; i < rand; i++) {
             createSimpleCharacter (manager);
         }
 }
 
     public void Add (TargetableCharacterButtonScript button)
     {
         characterButtons.Add (button.gameObject);
         //button.transform.parent = this.transform; 
         button.transform.parent = buttonList.transform;
         
         LayoutRebuilder.MarkLayoutForRebuild (GetComponent<RectTransform> ());
     }

I am new the StackOverflow question format, so please let me know if this question can be improved.

ui-issue.jpg (256.6 kB)
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

New UI system. How to focus on input fields. 6 Answers

4.6UI Rect Transfrom fix 1 Answer

Unity 4.6 beta button with Javascript rather than C# 2 Answers

Is it possible to start a coroutine using a button from the new UI system (4.6) 1 Answer

Unity 4.6 adding a Canvas Text onto a gameobject prefab?? -1 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