分享

unity5x+UGUI屏幕自适应(多分配率适配)

 office0228 2018-01-23

1、Canvas的属性配置


2、Canvas Scaler的属性配置


3、根据不同的屏幕的比例动态修改缩放基准

[csharp] view plain copy
  1. void Start ()   
  2.     {  
  3.         float standard_width = 960f;        //初始宽度  
  4.         float standard_height = 640f;       //初始高度  
  5.         float device_width = 0f;                //当前设备宽度  
  6.         float device_height = 0f;               //当前设备高度  
  7.         float adjustor = 0f;         //屏幕矫正比例  
  8.         //获取设备宽高  
  9.         device_width = Screen.width;  
  10.         device_height = Screen.height;  
  11.         //计算宽高比例  
  12.         float standard_aspect = standard_width / standard_height;  
  13.         float device_aspect = device_width / device_height;  
  14.         //计算矫正比例  
  15.         if (device_aspect < standard_aspect)  
  16.         {  
  17.             adjustor = standard_aspect / device_aspect;  
  18.         }  
  19.   
  20.         CanvasScaler canvasScalerTemp = transform.GetComponent<CanvasScaler>();  
  21.         if (adjustor == 0)  
  22.         {  
  23.             canvasScalerTemp.matchWidthOrHeight = 1;  
  24.         }  
  25.         else  
  26.         {  
  27.             canvasScalerTemp.matchWidthOrHeight = 0;  
  28.         }  
  29.     }  

将脚本挂在画布控件上。


效果


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多