Unity代码设置锚点

 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;
             }
         }
     }
 }


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


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值