快盘下载:好资源、好软件、快快下载吧!

快盘排行|快盘最新

当前位置:首页软件教程电脑软件教程 → Unity代码设置锚点

Unity代码设置锚点

时间:2022-11-09 21:36:46人气:作者:快盘下载我要评论
 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);


原文链接;https://www.jianshu.com/p/6192e14f88bb

网友评论

快盘下载暂未开通留言功能。

关于我们| 广告联络| 联系我们| 网站帮助| 免责声明| 软件发布

Copyright 2019-2029 【快快下载吧】 版权所有 快快下载吧 | 豫ICP备10006759号公安备案:41010502004165

声明: 快快下载吧上的所有软件和资料来源于互联网,仅供学习和研究使用,请测试后自行销毁,如有侵犯你版权的,请来信指出,本站将立即改正。