Confusion over addPersistRootNode on a ui node

I have a ui node that is a child of the canvas and I want it to go to the next scene without being changed. however I don’t want the entire canvas to go to the next scene. cocos creator won’t let me make a node persist if it isn’t a child of the scene.
is there a way around it?

You can only make root nodes persistent not child nodes.
Here is a video tutorial I made for this subject:

Hope it helps :slight_smile:

1 Like

thanks for the reply, but i have another problem now…my root node is not resizing on the web build along with the canvas node. the canvas node is configured to fit width and height both, so it will have some black strips either length-wise or breadth-wise in some cases. so logically the canvas shouldn’t stretch even on the web and its size should be the same as that of the design resolution. however, when i show a root ui element that has a widget that has 0 px distance from all sides, it doesn’t cover the entire screen. could you tell me how to get around this problem?

Normally widget uses parent as target to calculate position and size of the node attached to but anyway you can set Widget’s target node to Canvas just after scene loaded, so it should use canvas rectangle to fit element. You may call Widget’s updateAlignment method if it doesn’t update automatically.

By the way if canvas itself is not fitting to web screen you may add a widget component to canvas not to let fit itself to screen.

Hope it helps :wink:

1 Like

hi @FocusCreative,
thanks for the reply but the api documentation of cc.widgets clearly states that the target node of a widget should only be one of the parent nodes of the widget node.
so that won’t work.
thanks again, and i would greatly appreciate it if you could provide another solution.

Hi @rsamrry, you can just place your widget as the scene root. Canvas is not that necessory for ui child.
When a node is scene root, it’s widget component will align the node by using current screen size. I think that will match your need.
(BTW, since v2.3, all Canvas will has a widget attached automatically, using the same behavior as normal scene root).

that is fine but why doesn’t the scene-root-node size or scale parameters reflect the screen resolution? in fact despite widgets, the size parameters of none of the nodes change. so how are we supposed to know when to change the node size to maintain the aspect ratio of the ui/graphics elements without the updated information? btw, for maintaining aspect ratio of a spriteframe i use getoriginalsize().

another thing i noticed is that without the canvas component at the top, the buttons inside don’t work. so getting rid of the canvas doesn’t seem an option.
please suggest something else.

Because we use the design resolution to develop. All size/position are relative to design resolution. See

You don’t need to maintain the aspect ratio. Aspect ration keeps unchanged by default, unless you use a Widget to expand width or height.

You should use a widget to align your root elements if they don’t inside the canvas.