How to implement switching between Canvas from a script?

On what is written in the documentation studio yells that outdated methods.

What I found on the Internet is some kind of old, and in my opinion, you can make it shorter and simpler, there must be tools for this ...

I added two Animations to Canvas, on and off I added Canvas.Enable to them, in one animation it is 1 in another 0. Switching between them by trigger in Animator, is it a crutch? Or quite a working scheme?

But there was a new question how to determine which Canvas is currently Enable to disable it? Drive into a variable? As it is not universal ..

How can I get all Canvas objects on stage?

  • one
    Can you describe what exactly you want to achieve? Why do you need to switch between canvases? - M. Green
  • @ M.Green to switch between game interfaces, menus, etc. - Vladimir Alexandrov

1 answer 1

When I was making a game panel system, it looked like this: I have an "UI" object on it, an appropriately tuned Canvas and something like UISystem are hanging on it. Inside this system object are already panel objects. Each panel has its own disabled Canvas and a class inherited from the common ancestor of APanel. This class already controls the "appearance" and contains the panel ID.

Those. the system itself knows about all the panels that are its children and contains a list of them. When I want to open a panel, I say to the system “Open the Menu Panel”, it searches the list by its identifier, finds and calls its method, for example, Open () ;. The panel turns on its Canvas via _canvas.enable = true. When the panel need to close - I say the system "close the panel so-and-so" and the action described above is repeated.

In fact, everything was much more complicated there, including separate scripts "animators" that were responsible for exactly how the panel appeared, separate appearance modifiers (like "Close everything, and then open this one"), kalbeki after opening and events for panels so that you can do something in the panel before / after playing the open / close animation. However, the main point is described in the first two paragraphs.

  • Thank you for the answer! While I was sitting and trying to do something, I almost did what you wrote only without the main UISystem, but I pass it under your model, much more practical ... Is there one problem with enabling Canvas on android ... Does the canvas.enable method work there exactly? .. In Unity, everything works, however, when assembling and installing on Android, just nothing happens .. as long as I sin on the canvas.enable - Vladimir Alexandrov
  • one
    @Vladimir Aleksandrov, yes, it definitely works for me. Build a build by ticking "DebugBuild". Perhaps you just have an error somewhere or a blank link and everything doesn’t work, and not specifically ".able". With debug build, the console is screwed and you will see it in the game if something happens. - M. Green
  • I ticked the console but there is no console, only the inscription on the bottom right is Build developer ... - Vladimir Alexandrov
  • I found a mistake, I have a search for an ancestor by tag in my child, a NullException crashes in it ... I don’t understand how this error goes to the heir ... he cannot appear earlier than the ancestor .. how to be in this situation? - Vladimir Alexandrov
  • @Vladimir Aleksandrov, search not by tag) why is there anyway to search by tag? The UISystem script hangs on the main canvas, so from its child panel and look for it via GetComponentInParent <UISystem> (); If it does not find it, it means that you are looking for the wrong person. In general, it is better to make a general method of access to the system so that the search from another place does not differ from the search from the panel itself. Although the same singleton use (although they are scolded). A search by tag generally bypasses all the objects in the scene — which is bad for performance. - M. Green