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

1、Canvas的属性配置

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

2、Canvas Scaler的属性配置

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

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




将脚本挂在画布控件上。


效果

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

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






相关文章:

  • 2021-09-17
  • 2022-12-23
  • 2022-12-23
  • 2021-12-31
  • 2021-10-09
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-04-21
  • 2021-08-14
  • 2021-04-10
  • 2022-03-05
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案