// 屏幕适配问题
const int PANEL_HEIGHT_DEFAULT = 640;
const float ADPATE_ASPECTRATIO_MIN = 1.5f; // (窗口比值)窗口支持的最小长宽比
// UIPanel 使用此值作为 Manual Height
public static int GetPanelHeight()
{
int panelHeight = PANEL_HEIGHT_DEFAULT;
float aspacetRation = Screen.width * 1.0f / Screen.height;
if(aspacetRation < ADPATE_ASPECTRATIO_MIN)
{
int minWidth = (int)(Screen.height * ADPATE_ASPECTRATIO_MIN);
panelHeight = PANEL_HEIGHT_DEFAULT * minWidth / Screen.width;
}
return panelHeight;
}
public static int GetPanelWidth()
{
return GetPanelHeight () * Screen.width / Screen.height;
}
02.png
02—1.png
// 情况2 根据屏幕长宽比动态调节 UIPanels 和 UI2DSprite 的宽度和高度
const float ADAPTE_ASPECTRATIO_4T03 = 1.334f;
const float ADAPTE_ASPECTRATIO_3T02 = 1.5f;
const float ADAPTE_ASPECTRATIO_16T09 = 1.77f;
public Sprite splashSprite;
int width = Screen.width;
int height = Screen.height;
float aspectRantion = Screen.width *1.0f /Screen.height;
string splashTextureName = null;
//生命周期
void Start () {
if (aspectRantion <= ADAPTE_ASPECTRATIO_4T03) {
splashTextureName = @"Textures/splash_4to3@1dpi";
} else if (aspectRantion <= ADAPTE_ASPECTRATIO_3T02) {
splashTextureName = @"Textures/splash_3to2@xhdpi";
} else if (aspectRantion <= ADAPTE_ASPECTRATIO_16T09) {
splashTextureName = @"Textures/splash_16to9@xhdpi";
} else
{
splashTextureName = @"Textures/splash_16to2@xhdpi";
}
Sprite splashTexture = (Sprite)Resources.Load (splashTextureName, typeof(Sprite));
if (splashTexture != null) {
int textureWidth = (int)splashTexture.textureRect.width;
int textureHeight = (int)splashTexture.textureRect.height;
splashSprite.sprite2D = splashTexture;
if (Screen.height < textureHeight) {
textureWidth = textureWidth * Screen.height / textureHeight;
textureHeight = Screen.height;
}
if (aspectRantion > ADAPTE_ASPECTRATIO_16T09) {
textureWidth = Screen.width;
textureHeight = textureWidth * 640 / 1136;
}
root.manualHeight = textureWidth;
splashSprite.width = textureHeight;
splashSprite.height = textureHeight;
}
else
{
Debug.LogWarning (string.Format("NO Sprite At Resource Path {0} Found!",splashTextureName));
}
}
网友评论