推薦閱讀
- CSDN主頁
- GitHub開源地址
- Unity3D插件分享
- 簡書地址
- 我的個人博客
- QQ群:1040082875
大家好,我是佛系工程師☆恬靜的小魔龍☆,不定時更新Unity開發技巧,覺得有用記得一鍵三連哦,
一、前言
前段時間分享了UGUI的每個組件的屬性和使用方法,比如Text、Button、Image、Toggle、InputField、ScrollView等等,
接著分享了UGUI的一些原理,比如說UGUI的渲染模式、UGUI的縮放計算、UGUI的描點定位、UGUI的自動布局等等,
相信大家看完后會對UGUI有一個比較全面的認識了,
下面,就繼續分享UGUI的UI組件進行應用的實體,
這是本系列文章的第六篇:
【Unity3D-UGUI應用篇】(一)使用Text實作進度等待影片
【Unity3D-UGUI應用篇】(二)使用Image實作進度條影片
【Unity3D-UGUI應用篇】(三)使用UGUI實作層級選單
【Unity3D-UGUI應用篇】(四)使用UGUI彈窗顯示模型及彈窗模型互動
【Unity3D-UGUI應用篇】(五)使用Button完成滑鼠移動到UI上面顯示文字功能
【Unity3D-UGUI應用篇】(六)螢屏自適應(多分配率適配)
【Unity3D-UGUI應用篇】(七)UGUI實作視窗的自由拖拽
【Unity3D-UGUI應用篇】(八)Image實作畫線、畫三角形、畫正方形、畫圓
二、Canvas及CanvasScaler的屬性配置


三、代碼實作
void Start ()
{
float standard_width = 960f; //初始寬度
float standard_height = 640f; //初始高度
float device_width = 0f; //當前設備寬度
float device_height = 0f; //當前設備高度
float adjustor = 0f; //螢屏矯正比例
//獲取設備寬高
device_width = Screen.width;
device_height = Screen.height;
//計算寬高比例
float standard_aspect = standard_width / standard_height;
float device_aspect = device_width / device_height;
//計算矯正比例
if (device_aspect < standard_aspect)
{
adjustor = standard_aspect / device_aspect;
}
CanvasScaler canvasScalerTemp = transform.GetComponent<CanvasScaler>();
if (adjustor == 0)
{
canvasScalerTemp.matchWidthOrHeight = 1;
}
else
{
canvasScalerTemp.matchWidthOrHeight = 0;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/298183.html
標籤:其他
上一篇:《30天自制作業系統》第9天
