- 最后登录
- 2016-8-29
- 注册时间
- 2012-8-25
- 阅读权限
- 90
- 积分
- 23585
- 纳金币
- 20645
- 精华
- 62
|
这是我觉得比较简单容易理解的自适应,记录一下
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class UIRootEx : MonoBehaviour
{
public bool m_IsLandscape = true;//是否是横屏
const int WindowScreenWidth = 960;//标准宽度
const int WindowScreenHeight = 640;//标准高度
void Awake()
{
float scale = (float)UnityEngine.Screen.width / (float)UnityEngine.Screen.height;//现有手机的宽高比
if (m_IsLandscape)
{
if (scale < 1.5f)
GetComponent<UIRoot>().manualHeight = (int)(WindowScreenWidth / scale);
else
GetComponent<UIRoot>().manualHeight = WindowScreenHeight;
}
else
{
scale = 1.0f / scale;
if (scale < 1.5f)
GetComponent<UIRoot>().manualHeight = WindowScreenWidth;
else
GetComponent<UIRoot>().manualHeight = (int)(WindowScreenHeight * scale);
}
}
// Use this for initialization
void Start()
{
//设置手机屏幕为横屏
Screen.autorotateToLandscapeLeft = true;
Screen.autorotateToLandscapeRight = true;
Screen.autorotateToPortrait = false;
Screen.autorotateToPortraitUpsideDown = false;
}
// Update is called once per frame
void Update()
{
}
}
这个脚本是挂在UIRoot上的
|
|