Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Layout does not refresh it self in Play Mode #1

Open
IvanMurzak opened this issue Dec 3, 2022 · 9 comments
Open

Layout does not refresh it self in Play Mode #1

IvanMurzak opened this issue Dec 3, 2022 · 9 comments

Comments

@IvanMurzak
Copy link

IvanMurzak commented Dec 3, 2022

Need to move mouse over the specific area in a layout. That is so annoying thing. Because in the middle of development and debugging need to find a component with the SafeAreaLayout component and move mouse over. Is it possible to force it to work automatically OnAwake or OnStart events (at least as an option)?

Unity_RjdR9pUOmB.mp4

P.S. the package is very cool! Thanks for the work <3

@IvanMurzak
Copy link
Author

UPDATE: possibly OnEnable event would be better to listen for making the "refresh" of SafeArea

@gilzoide
Copy link
Owner

gilzoide commented Dec 3, 2022

Hi @IvanMurzak, thanks for the report.
I've never had this problem, so weird =S
Does the problem occur only in the Unity Editor or the Safe Area gets ignored in builds as well?

In my experience, whenever object gets reactivated and OnEnable called, the Unity GUI will rebuild the layouts and the Safe Area gets applied. Maybe it's not doing so in your setup only...

Could you try adding a call to LayoutRebuilder.MarkLayoutForRebuild(SelfRectTransform) in the end of OnEnable and tell me if it fixes the behavior?

protected virtual void OnEnable()
{
    _canvas = GetComponentInParent<Canvas>();
    RefreshChildrenAnchors();
    LayoutRebuilder.MarkLayoutForRebuild(SelfRectTransform);  // <-- this line
}

Say, do you reparent the object after creating it outside of the Canvas?
I've just noticed that the script is not refreshing its Canvas and rebuilding the layout if it gets reparented.
Maybe you could try adding a OnTransformParentChanged method to the SafeAreaLayoutGroup script, if this is the case:

void OnTransformParentChanged()
{
    _canvas = GetComponentInParent<Canvas>();
    // RefreshChildrenAnchors();  // no need to refresh children here
    LayoutRebuilder.MarkLayoutForRebuild(SelfRectTransform);
}

Even if it does not work for you, I'll add it to the script later to handle these reparenting cases.

@IvanMurzak
Copy link
Author

It doesn't refresh in builds also. That all what I can say for now. Will test your suggestions. Do you want me to add the code into your class (SafeArea...)?

@IvanMurzak
Copy link
Author

IvanMurzak commented Dec 3, 2022

My UI never changes parents. It just disabled/enabled.

Unity_JfNurYzeI6.mp4

@IvanMurzak
Copy link
Author

@gilzoide I reproduced the error with manual actions. Just need to disable gameObject, scale it to zero, enable it and scale back to 1.

Unity_1rH3ac0PEC.mp4

@gilzoide
Copy link
Owner

gilzoide commented Dec 4, 2022

Thanks for the insights!
Maybe the problem is with the usage of nested Canvases.
Try something for me, please: instead of using the parent Canvas directly, get the root Canvas in OnEnable:

protected virtual void OnEnable()
{
    _canvas = GetComponentInParent<Canvas>().rootCanvas;
    RefreshChildrenAnchors();             // ^ here, get the rootCanvas
}

Do you want me to add the code into your class (SafeArea...)?

Yes. If you installed the plugin via UPM, you can find the script at the folder Library/PackageCache/com.gilzoide.safe-area-layout@<some number here>/Runtime/SafeAreaLayoutGroup.cs. You can edit it there directly and Unity will use the updated code.

Just need to disable gameObject, scale it to zero, enable it and scale back to 1.

Hmm, it is possible that scaling won't trigger layout rebuilds. I'll test and fix this case later.

Thank you again for the additional information on this issue!

@gilzoide
Copy link
Owner

Hey @IvanMurzak.
So, I was testing here with your "manual actions" and wasn't able to reproduce the issue.
I used a Screen Space - Camera Canvas with a subcanvas, then tested scaling and nothing went wrong.
This video shows some of my tests, but I tried quite some more than that. I also tested on both Unity 2021.3.13 and 2019.4.40, no luck reproducing.

safe-area.mp4

Your case is certainly very strange =/
I'm using only uGUI Text and Images in my sample scene, you seem to use "UIView" and "UICanvas" scripts. Are they part of some third-party asset?

@gilzoide
Copy link
Owner

So, something else I just realized is that hovering the Editor button calls LayoutRebuilder.ForceRebuildLayoutImmediate while the runtime uses LayoutRebuilder.MarkLayoutForRebuild. I have to say, I see no reason why marking layout for rebuild instead of forcing the rebuild would not work, appart from weird editor-only cases, but maybe it's the key to your problem.

@gilzoide
Copy link
Owner

gilzoide commented Dec 27, 2022

Hey @IvanMurzak , I've made some changes to the package that might fix your problem.
Please try out the version at tag 1.0.2 and tell me if it works. You can use the url https://github.com/gilzoide/unity-safe-area-layout.git#1.0.2 at your manifest.json file if you use UPM.

Now every internal call to RefreshChildrenAnchors marks the layout for rebuild, which I hope will work for you.

If it does not, I'll add an option for forcing the layout instead of only marking for rebuild.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants