• 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 wmadwand · Sep 01, 2017 at 08:23 AM · 2dguiuiinterfacepresets

Bad result of changing RectTransform values (Anchor Preset) programmatically

Howdy, guys! I change RectTransform values of the GO with Image component programmatically. Kinda choose the Anchor Preset but from the code. And as a result i get GO with Image in the right place (according to the anchor's values) but sprite of the Image in the wrong (previous) place.

Screenshot:

alt text

Piece of code:

 switch (_tutorialPage.tooltipPosition)
             {
                 case TooltipPosition.RightBottom:
                     {
                         _tooltipPanelRect.anchorMin = new Vector2(1, 0);
                         _tooltipPanelRect.anchorMax = new Vector2(1, 0);
                         _tooltipPanelRect.pivot = new Vector2(1f, 0f);
                         _tooltipPanelRect.anchoredPosition = new Vector2(0, 0);
 
                         _tooltipGuideRect.anchorMin = new Vector2(1f, 0);
                         _tooltipGuideRect.anchorMax = new Vector2(1f, 0);
                         _tooltipGuideRect.anchoredPosition = new Vector2(-188, 125);
                         break;
                     }
                 case TooltipPosition.LeftBottom:
                     {
                         _tooltipPanelRect.anchorMin = new Vector2(0, 0);
                         _tooltipPanelRect.anchorMax = new Vector2(0, 0);
                         _tooltipPanelRect.pivot = new Vector2(1f, 0f);
                         _tooltipPanelRect.anchoredPosition = new Vector2(_tooltipPanelRect.rect.width, 0);
 
                         _tooltipGuideRect.anchorMin = new Vector2(1f, 0);
                         _tooltipGuideRect.anchorMax = new Vector2(1f, 0);
                         _tooltipGuideRect.anchoredPosition = new Vector2(244, 125);
                         break;
                     }
                 case TooltipPosition.CenterBottom:
                     {
                         _tooltipPanelRect.anchorMin = new Vector2(0.5f, 0);
                         _tooltipPanelRect.anchorMax = new Vector2(0.5f, 0);
                         _tooltipPanelRect.pivot = new Vector2(1f, 0f);
                         _tooltipPanelRect.anchoredPosition = new Vector2(_tooltipPanelRect.rect.width / 2, 0);
 
                         _tooltipGuideRect.anchorMin = new Vector2(0.5f, 0);
                         _tooltipGuideRect.anchorMax = new Vector2(0.5f, 0);
                         _tooltipGuideRect.anchoredPosition = new Vector2(_tooltipGuideRect.rect.width / 2, 125);
                         break;
                     }
             }

Next, after choosing whichever different Anchor Preset manually things get right and the sprite changes the position to the right place similar with the Image.

Does anybody know how to deal with it?

i80cimgpsh-orig222222.png (198.0 kB)
Comment
Add comment · Show 1
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 wmadwand · Sep 02, 2017 at 05:52 AM 0
Share

Guys @Homeship, @alejandro-unity , @crazy$$anonymous$$night may be you have any ideas how to sort it out?

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by wmadwand · Sep 02, 2017 at 12:24 PM

Finally got the solution:

 *Piece of code from the above*
 
 CanvasGO.SetActive(false);
 CanvasGO.SetActive(true);

And that's all the trick. Simple but slightly inefficient, thought.

And here's another one option which i haven't tried yet but pathetically the better one due to its better performance:

 IEnumerator UpdateUIPanel()
 {
    *Piece of code from the above*
    yield return null;
    Canvas.ForceUpdateCanvases();
 }

And yep, it definitely looks like Unity bug.

Comment
Add comment · Show 2 · 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 slik · Sep 04, 2017 at 09:50 AM 0
Share

Did you try https://docs.unity3d.com/ScriptReference/UI.LayoutRebuilder.ForceRebuildLayoutImmediate.html ?

avatar image wmadwand slik · Sep 22, 2017 at 10:58 AM 0
Share

Will defenitely try this one. Thanks for the answer!

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

192 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 avatar image

Related Questions

Problem with GUI element following enemy 0 Answers

GameObject with SpriteRenderer either on top of everything or not showing 0 Answers

Can I do a "rotatory" UI like this? 1 Answer

Unity 5 GUI Buttons Won't Show When Built 0 Answers

What is the right choice for UI Performance? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges