当前位置:   article > 正文

Unity——当ui挂载Content Size Fitter组件后通过Rect获取的UI物体的 width 和 height_ui上挂载content size fitter组件后用rect获取的ui的width和height

ui上挂载content size fitter组件后用rect获取的ui的width和height
using FitMode = UnityEngine.UI.ContentSizeFitter.FitMode;
  1. public static Vector2 GetContentSizeFitterPreferredSize(this RectTransform rect,ContentSizeFitter contentSizeFitter)
  2. {
  3. LayoutRebuilder.ForceRebuildLayoutImmediate(rect);
  4. return new Vector2(HandleSelfFittingAlongAxis(0,rect,contentSizeFitter), HandleSelfFittingAlongAxis(1,rect,contentSizeFitter));
  5. }
  6. private static float HandleSelfFittingAlongAxis(int axis,RectTransform rect,ContentSizeFitter contentSizeFitter)
  7. {
  8. FitMode fitting = (axis == 0 ? contentSizeFitter.horizontalFit : contentSizeFitter.verticalFit);
  9. if (fitting == FitMode.MinSize)
  10. {
  11. return LayoutUtility.GetMinSize(rect, axis);
  12. }
  13. else
  14. {
  15. return LayoutUtility.GetPreferredSize(rect, axis);
  16. }
  17. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/110389
推荐阅读
相关标签
  

闽ICP备14008679号