在打包或者发布版本的时候肯定要适应不同型号的手机,而制作的UI之前都是把锚点固定好的,为啦不让UI在不同的手机上露馅,我们只能让UI自动拉伸,只需要在UI的canvas上面挂一个脚本就可以啦,代码如下
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; namespace Game.Utility { public class CavansController : MonoBehaviour { private CanvasScaler m_Scaler; // Use this for initialization void Awake() { m_Scaler = GetComponent<CanvasScaler>(); var sizeDesign = m_Scaler.referenceResolution; var rateDesign = sizeDesign.x / sizeDesign.y; var rateScreen = Screen.width / Screen.height; if (rateDesign > rateScreen) { m_Scaler.matchWidthOrHeight = 0.0f; } else { m_Scaler.matchWidthOrHeight = 1.0f; } } } }