Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
24
Question by U_Ku_Shu · Apr 26, 2017 at 01:43 PM · uicanvasrecttransformanchorpresets

[SOLUTION] set UI RectTransform Anchor Presets from code c#

Example of usage solution:

 _ImgTransform.SetAnchor(AnchorPresets.TopRight);
 _ImgTransform.SetAnchor(AnchorPresets.TopRight,-10,-10);
 
 ImgTransform.SetPivot(PivotPresets.TopRight);

Didn't test, but must to work ok:

 using UnityEngine;
 
 public enum AnchorPresets
     {
     TopLeft,
     TopCenter,
     TopRight,
 
     MiddleLeft,
     MiddleCenter,
     MiddleRight,
 
     BottomLeft,
     BottonCenter,
     BottomRight,
     BottomStretch,
 
     VertStretchLeft,
     VertStretchRight,
     VertStretchCenter,
 
     HorStretchTop,
     HorStretchMiddle,
     HorStretchBottom,
 
     StretchAll
 }
 
 public enum PivotPresets
 {
     TopLeft,
     TopCenter,
     TopRight,
 
     MiddleLeft,
     MiddleCenter,
     MiddleRight,
 
     BottomLeft,
     BottomCenter,
     BottomRight,
 }
 
 public static class RectTransformExtensions
 {
     public static void SetAnchor(this RectTransform source, AnchorPresets allign, int offsetX=0, int offsetY=0)
     {
         source.anchoredPosition = new Vector3(offsetX, offsetY, 0);
 
         switch (allign)
         {
             case(AnchorPresets.TopLeft):
             {
                 source.anchorMin = new Vector2(0, 1);
                 source.anchorMax = new Vector2(0, 1);
                 break;
             }
             case (AnchorPresets.TopCenter):
             {
                 source.anchorMin = new Vector2(0.5f, 1);
                 source.anchorMax = new Vector2(0.5f, 1);
                 break;
             }
             case (AnchorPresets.TopRight):
             {
                 source.anchorMin = new Vector2(1, 1);
                 source.anchorMax = new Vector2(1, 1);
                 break;
             }
 
             case (AnchorPresets.MiddleLeft):
             {
                 source.anchorMin = new Vector2(0, 0.5f);
                 source.anchorMax = new Vector2(0, 0.5f);
                 break;
             }
             case (AnchorPresets.MiddleCenter):
             {
                 source.anchorMin = new Vector2(0.5f, 0.5f);
                 source.anchorMax = new Vector2(0.5f, 0.5f);
                 break;
             }
             case (AnchorPresets.MiddleRight):
             {
                 source.anchorMin = new Vector2(1, 0.5f);
                 source.anchorMax = new Vector2(1, 0.5f);
                 break;
             }
 
             case (AnchorPresets.BottomLeft):
             {
                 source.anchorMin = new Vector2(0, 0);
                 source.anchorMax = new Vector2(0, 0);
                 break;
             }
             case (AnchorPresets.BottonCenter):
             {
                 source.anchorMin = new Vector2(0.5f, 0);
                 source.anchorMax = new Vector2(0.5f,0);
                 break;
             }
             case (AnchorPresets.BottomRight):
             {
                 source.anchorMin = new Vector2(1, 0);
                 source.anchorMax = new Vector2(1, 0);
                 break;
             }
 
             case (AnchorPresets.HorStretchTop):
             {
                 source.anchorMin = new Vector2(0, 1);
                 source.anchorMax = new Vector2(1, 1);
                 break;
             }
             case (AnchorPresets.HorStretchMiddle):
             {
                 source.anchorMin = new Vector2(0, 0.5f);
                 source.anchorMax = new Vector2(1, 0.5f);
                 break;
             }
             case (AnchorPresets.HorStretchBottom):
             {
                 source.anchorMin = new Vector2(0, 0);
                 source.anchorMax = new Vector2(1, 0);
                 break;
             }
 
             case (AnchorPresets.VertStretchLeft):
             {
                 source.anchorMin = new Vector2(0, 0);
                 source.anchorMax = new Vector2(0, 1);
                 break;
             }
             case (AnchorPresets.VertStretchCenter):
             {
                 source.anchorMin = new Vector2(0.5f, 0);
                 source.anchorMax = new Vector2(0.5f, 1);
                 break;
             }
             case (AnchorPresets.VertStretchRight):
             {
                 source.anchorMin = new Vector2(1, 0);
                 source.anchorMax = new Vector2(1, 1);
                 break;
             }
 
             case (AnchorPresets.StretchAll):
             {
                 source.anchorMin = new Vector2(0, 0);
                 source.anchorMax = new Vector2(1, 1);
                 break;
             }
         }
     }
 
     public static void SetPivot(this RectTransform source, PivotPresets preset)
     {
 
         switch (preset)
         {
             case (PivotPresets.TopLeft):
             {
                 source.pivot = new Vector2(0, 1);
                 break;
             }
             case (PivotPresets.TopCenter):
             {
                 source.pivot = new Vector2(0.5f, 1);
                 break;
             }
             case (PivotPresets.TopRight):
             {
                 source.pivot = new Vector2(1, 1);
                 break;
             }
 
             case (PivotPresets.MiddleLeft):
             {
                 source.pivot = new Vector2(0, 0.5f);
                 break;
             }
             case (PivotPresets.MiddleCenter):
             {
                 source.pivot = new Vector2(0.5f, 0.5f);
                 break;
             }
             case (PivotPresets.MiddleRight):
             {
                 source.pivot = new Vector2(1, 0.5f);
                 break;
             }
 
             case (PivotPresets.BottomLeft):
             {
                 source.pivot = new Vector2(0, 0);
                 break;
             }
             case (PivotPresets.BottomCenter):
             {
                 source.pivot = new Vector2(0.5f, 0);
                 break;
             }
             case (PivotPresets.BottomRight):
             {
                 source.pivot = new Vector2(1, 0);
                 break;
             }
         }
     }
 }


Comment
Add comment · Show 6
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 MattCardinal · Aug 16, 2016 at 05:06 PM 0
Share

Nailed it :)

avatar image osybear · Jan 11, 2017 at 03:08 AM 0
Share

Thank you been forking at this problem for hours! :D

avatar image rfadeev · Apr 26, 2017 at 03:40 AM 0
Share

Thank you! AnchorPresets. BottomStretch seems to be obsolete preset value, I guess it can be removed. Also no need in Vector3 when assigning anchored position, can do source.anchoredPosition = new Vector2(offsetX, offsetY); ins$$anonymous$$d.

avatar image IndieForger · Jul 08, 2017 at 07:44 PM 0
Share

Works like charm. Started writing it from scratch and figured will check if someone has done it already and voila! It saved me an hour or two. Thanks for posting this.

avatar image cyf649669121 · Mar 13, 2019 at 06:37 AM 0
Share

秒啊~~~

什么?居然要10个字?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Lund · Jun 19, 2019 at 11:25 AM

Thanks for it - it was pretty much what I was searching for. I rewrote it a little bit to use DoTween and make a smooth transition instead of instant jump to the new position since that was what I needed.

 using DG.Tweening;
 using UnityEngine;
 
 public enum AnchorPresets
 {
     TopLeft,
     TopCenter,
     TopRight,
 
     MiddleLeft,
     MiddleCenter,
     MiddleRight,
 
     BottomLeft,
     BottonCenter,
     BottomRight,
     BottomStretch,
 
     VertStretchLeft,
     VertStretchRight,
     VertStretchCenter,
 
     HorStretchTop,
     HorStretchMiddle,
     HorStretchBottom,
 
     StretchAll
 }
 
 public enum PivotPresets
 {
     TopLeft,
     TopCenter,
     TopRight,
 
     MiddleLeft,
     MiddleCenter,
     MiddleRight,
 
     BottomLeft,
     BottomCenter,
     BottomRight,
 }
 
 public static class RectTransformExtensions
 {
     public static Tween SetAnchor(this RectTransform source, AnchorPresets allign, float offsetX = 0, float offsetY = 0, float duration = 1, Ease ease = Ease.OutCubic)
     {
         Vector2 anchorMin = Vector2.zero;
         Vector2 anchorMax = Vector2.zero;
         switch (allign)
         {
             case (AnchorPresets.TopLeft):
                 {
                     anchorMin = new Vector2(0, 1);
                     anchorMax = new Vector2(0, 1);
                     break;
                 }
             case (AnchorPresets.TopCenter):
                 {
                     anchorMin = new Vector2(0.5f, 1);
                     anchorMax = new Vector2(0.5f, 1);
                     break;
                 }
             case (AnchorPresets.TopRight):
                 {
                     anchorMin = new Vector2(1, 1);
                     anchorMax = new Vector2(1, 1);
                     break;
                 }
 
             case (AnchorPresets.MiddleLeft):
                 {
                     anchorMin = new Vector2(0, 0.5f);
                     anchorMax = new Vector2(0, 0.5f);
                     break;
                 }
             case (AnchorPresets.MiddleCenter):
                 {
                     anchorMin = new Vector2(0.5f, 0.5f);
                     anchorMax = new Vector2(0.5f, 0.5f);
                     break;
                 }
             case (AnchorPresets.MiddleRight):
                 {
                     anchorMin = new Vector2(1, 0.5f);
                     anchorMax = new Vector2(1, 0.5f);
                     break;
                 }
 
             case (AnchorPresets.BottomLeft):
             {
                 anchorMin = new Vector2(0, 0);
                     anchorMax = new Vector2(0, 0);
                     break;
                 }
             case (AnchorPresets.BottonCenter):
                 {
                     anchorMin = new Vector2(0.5f, 0);
                     anchorMax = new Vector2(0.5f, 0);
                     break;
                 }
             case (AnchorPresets.BottomRight):
                 {
                     anchorMin = new Vector2(1, 0);
                     anchorMax = new Vector2(1, 0);
                     break;
                 }
 
             case (AnchorPresets.HorStretchTop):
                 {
                     anchorMin = new Vector2(0, 1);
                     anchorMax = new Vector2(1, 1);
                     break;
                 }
             case (AnchorPresets.HorStretchMiddle):
                 {
                     anchorMin = new Vector2(0, 0.5f);
                     anchorMax = new Vector2(1, 0.5f);
                     break;
                 }
             case (AnchorPresets.HorStretchBottom):
                 {
                     anchorMin = new Vector2(0, 0);
                     anchorMax = new Vector2(1, 0);
                     break;
                 }
 
             case (AnchorPresets.VertStretchLeft):
                 {
                     anchorMin = new Vector2(0, 0);
                     anchorMax = new Vector2(0, 1);
                     break;
                 }
             case (AnchorPresets.VertStretchCenter):
                 {
                     anchorMin = new Vector2(0.5f, 0);
                     anchorMax = new Vector2(0.5f, 1);
                     break;
                 }
             case (AnchorPresets.VertStretchRight):
                 {
                     anchorMin = new Vector2(1, 0);
                     anchorMax = new Vector2(1, 1);
                     break;
                 }
 
             case (AnchorPresets.StretchAll):
                 {
                     anchorMin = new Vector2(0, 0);
                     anchorMax = new Vector2(1, 1);
                     break;
                 }
         }
 
 
         source.DOAnchorMin(anchorMin, duration).SetEase(ease);
         source.DOAnchorMax(anchorMax, duration).SetEase(ease);
 
         return source.DOAnchorPos(new Vector2(offsetX, offsetY), duration).SetEase(ease);
     }
 
     public static Tween SetPivot(this RectTransform source, PivotPresets preset, float duration = 1, Ease ease = Ease.OutCubic)
     {
         Vector2 pivotVector2 = Vector2.zero;
         switch (preset)
         {
             case (PivotPresets.TopLeft):
                 {
                     pivotVector2 = new Vector2(0, 1);
                     break;
                 }
             case (PivotPresets.TopCenter):
                 {
                     pivotVector2 = new Vector2(0.5f, 1);
                     break;
                 }
             case (PivotPresets.TopRight):
                 {
                     pivotVector2 = new Vector2(1, 1);
                     break;
                 }
 
             case (PivotPresets.MiddleLeft):
                 {
                     pivotVector2 = new Vector2(0, 0.5f);
                     break;
                 }
             case (PivotPresets.MiddleCenter):
                 {
                     pivotVector2 = new Vector2(0.5f, 0.5f);
                     break;
                 }
             case (PivotPresets.MiddleRight):
                 {
                     pivotVector2 = new Vector2(1, 0.5f);
                     break;
                 }
 
             case (PivotPresets.BottomLeft):
                 {
                     pivotVector2 = new Vector2(0, 0);
                     break;
                 }
             case (PivotPresets.BottomCenter):
                 {
                     pivotVector2 = new Vector2(0.5f, 0);
                     break;
                 }
             case (PivotPresets.BottomRight):
             {
                     pivotVector2 = new Vector2(1, 0);
                     break;
                 }
         }
          return source.DOPivot(pivotVector2, duration).SetEase(ease);
     }
 }
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 JonRitman · Oct 22, 2021 at 03:09 PM 0
Share

Wow, what a lot of code to write, do you not use tables? float[] table = new float[]{0,1,0,1,0.5,1,0.5,1,1,1,1,1 ...} int base = allign << 2; source.AnchorMin = new Vector2(table[base++], table[base++]); source.AnchorMax = new Vector2(table[base++], table[base]);

avatar image JonRitman · Oct 22, 2021 at 03:10 PM 0
Share

Oops, bad format, try again:

Wow, what a lot of code to write, do you not use tables?

float[] table = new float[]{0,1,0,1,0.5,1,0.5,1,1,1,1,1 ...}

int base = allign << 2;

source.AnchorMin = new Vector2(table[base++], table[base++]);

source.AnchorMax = new Vector2(table[base++], table[base]);

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

82 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

Related Questions

Keeping relative size of UI elements without moving them 0 Answers

RectTransform outline and position aren't synced 0 Answers

Moving RectTransform over another RectTransform under GridLayout 0 Answers

GetComponent() Problem with getting value. 1 Answer

Canvas shrinks over time 0 Answers


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