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 Bokaii · Jul 17, 2016 at 01:07 PM · c#uicanvas

Switch between canvas

How would I go about switching between multiple canvas? Up until now I have just enabled and disabled the "canvas" component, but this means I can still interact with the buttons. I ran into this problem when I closed a canvas after opening it, and then jumped. This disabled the players movement scripts (like it should do when I open a canvas) and tried to do what the buttons were supposed to do. - So my question is, how would I go about switching between multiple canvas, without having to define every single canvas?

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
4
Best Answer

Answer by gorsefan · Jul 17, 2016 at 02:24 PM

I have a bunch of canvases I toggle on and off.

Short answer, the main thing is to get the UnityEngine.CanvasGroup's and set the .alpha .interactable and .blocksRaycasts properties, as desired.

Long answer; Here's an Abstract class the individual controller scripts for each canvas implement, it may be of use. It also fades them in & out. Client code must implement PostMenuClose() which can be custom code called when the manu/panel is closed. Take from it what you like :)

 using UnityEngine;
 using System.Collections;
 
 public abstract class AbstractFadePanel : MonoBehaviour
 {
 
     protected UnityEngine.UI.Text[] textEls;
     protected CanvasGroup RootCanvasGroup;
     public abstract bool MenuOpen { get; protected set; }
     public abstract void PostMenuClose ();
 
     UnityEngine.UI.Image[] imageEls;
 
     bool firstTime = true;
 
     const float FADE_TIME = 0.2f;
 
     void Awake ()
     {
         RootCanvasGroup = gameObject.GetComponentsInChildren<UnityEngine.CanvasGroup> () [0];
         if (RootCanvasGroup == null)
             RootCanvasGroup = gameObject.GetComponent<UnityEngine.CanvasGroup> ();
 
         textEls = gameObject.GetComponentsInChildren<UnityEngine.UI.Text> (includeInactive: true);
         imageEls = gameObject.GetComponentsInChildren<UnityEngine.UI.Image> (includeInactive: true);
     }
 
 
     /// <summary>
     /// Toggles the visibility of the entire panel
     /// </summary>
     /// <param name="turnOn">If set to <c>true</c> turn on.</param>
     public void ToggleVisibility (bool turnOn)
     {
         // Until used, the menus may exist in the editor in a crippled state
         if (firstTime) {
             RootCanvasGroup.alpha = 1;
             RootCanvasGroup.interactable = true;
         }
 
         RootCanvasGroup.interactable = turnOn;
 
         // Remove focus from the menu so arrow keys don't also move through widgets
         //  after it's been hidden.
         UnityEngine.EventSystems.EventSystem.current.SetSelectedGameObject (null);
 
         // Fade out constituent els
         float targetAlpha = turnOn ? 1f : 0f;
 
         foreach (var t in textEls)
             t.CrossFadeAlpha (targetAlpha, FADE_TIME, false);
 
         foreach (var i in imageEls)
             i.CrossFadeAlpha (targetAlpha, FADE_TIME, false);
 
         if (turnOn)
             StartCoroutine (ForceInteractableRedraw ());
     }
         
 
     /// <summary>
     /// Closes the menu
     /// </summary>
     protected void closeMenu ()
     {
         if (!UI.UIRegistry.GetClickLock ())
             return;
 
         RootCanvasGroup.interactable = false;
         RootCanvasGroup.blocksRaycasts = false;
         MenuOpen = false;
         ToggleVisibility (turnOn: false);
         PostMenuClose ();
     }
 
 
     /// <summary>
     /// Opens the menu
     /// </summary>
     protected void openMenu ()
     {
         RootCanvasGroup.interactable = true;
         RootCanvasGroup.blocksRaycasts = true;
         MenuOpen = true;
         ToggleVisibility (turnOn: true);
     }
 
 
     /// <summary>
     /// For some reason toggling Interactable does not ensure the objects will
     /// redraw correctly to reflect the new state.  This forces it.
     /// Note Canvas.ForceUpdateCanvases() sounds like it should address this, but couldn't
     /// get it working in a quick test.
     /// </summary>
     /// <returns>The interactable redraw.</returns>
     IEnumerator ForceInteractableRedraw ()
     {
         yield return new WaitForSeconds (0.04f);
         RootCanvasGroup.interactable = false;
         RootCanvasGroup.interactable = true;
     }
 }

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 Bokaii · Jul 17, 2016 at 02:41 PM 1
Share

Thank you very much, this was VERY helpful!

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

191 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 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

Unity 2D Combat Text Issue With Multiple Enemies 2 Answers

UI Minimap 0 Answers

Canvas Button OnClick() function arguments 1 Answer

When I deleted an UI obj. in a cell, I want an empty cell. How can I do this? 0 Answers

Hidden Canvas, doesn't disable interaction with buttons. 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