• 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 Hi-day · Jun 08, 2018 at 11:34 AM · uinested

How can i create nested-menu with c# script?

i want creat nested menu with script but i have some problem.

 List mButton = new List();
 public RectTransform buttonRect;
 public bool open;
 public int buttonDistance ;
 Vector2[] buttonPos;
 float speed = 10.0f;
 float buttonHeight;

 void Start () {

     buttonHeight = buttonRect.rect.height*2.0f;
     mButton = this.GetComponentsInChildren<Button>(true).
                 Where(x => x.gameObject.transform.parent != transform.parent).ToList();

     buttonPos = new Vector2[mButton.Count];

     this.GetComponent<Button>().onClick.AddListener( () => { OpenMenu();} );

     this.GetComponent<RectTransform>().pivot = new Vector2(1f, 1f);

     foreach (Button b in mButton)
     {
         b.gameObject.SetActive(false);
     }
 }

 public void OpenMenu()
 {
     open = !open;

     for(int i =0;i<mButton.Count;i++)
     {
         if(open)
         {
  
             float xpos = buttonRect.rect.width*1.5f;
            
             float ypos = (i+1)*buttonHeight;

             buttonPos[i] = new Vector2(this.transform.position.x + xpos, this.transform.position.y + ypos);
         }
         else
         {
             buttonPos[i] = this.transform.position;

         }

     }

     StartCoroutine(MoveElements());

 }

 private IEnumerator MoveElements()
 {
     foreach(Button b in mButton)
     {

         b.gameObject.SetActive(true);
     }
     int loops = 0;
     while(loops<= buttonDistance / speed)
     {
         yield return new WaitForSeconds(0.01f);
         for(int i = 0; i < mButton.Count; i++)
         {
             Color c = mButton[i].gameObject.GetComponent<Image>().color;
             if (open)
                 c.a = Mathf.Lerp(c.a, 1, speed * Time.fixedTime);
             else
                 c.a = Mathf.Lerp(c.a, 0, speed * Time.fixedTime);
             mButton[i].gameObject.GetComponent<Image>().color = c;
             mButton[i].gameObject.transform.position = Vector2.Lerp(mButton[i].gameObject.transform.position, buttonPos[i], speed * Time.fixedTime);
         }
         loops++;

     }
     if (!open)
     {
         foreach(Button b in mButton)
         {
             b.gameObject.SetActive(false);
         }
     }
 }
Comment
Add comment · Show 4
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 Hi-day · Jun 08, 2018 at 11:36 AM 0
Share

alt text

nestedmenu.png (12.5 kB)
avatar image Harinezumi · Jun 08, 2018 at 11:48 AM 0
Share

What do you want achieve exactly, how do you want to position the buttons? If I understand correctly, you want them to be children of this game object, arranged vertically. Have you considered using a VerticalLayoutGroup?

avatar image Hi-day · Jun 08, 2018 at 12:00 PM 0
Share

I want create nested for multi-resolution and buttons don't separate.

avatar image agarcia115 Hi-day · Jun 08, 2018 at 12:14 PM 0
Share

Yeah use that, vertical layout, even you can create an Scroll view so you can see your buttons in a scroll list. To make a layout group, your buttons shall have layout element.

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

141 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Good idea to use nested canvases for children of scrollable lists? 2 Answers

Alternatives to Canvas Draw Order when using elements that rotate around each other 0 Answers

Panel "Raycast Target" unblock specific portion? 0 Answers

Toggles/Toggle Groups Question 0 Answers

UI does not scale and is eaten by a screen 3 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