// FixedSize时:填理想分辨率的高度
// FixedSizeofWidth时:填理想分辨率的宽度
Manual Height:先按照理想分辨率做。当Game视图(打包后的屏幕分辨率)不是这个理想分辨率的时候就会进行比例缩放。
Minimum Height:Game视图低于这个数值开始按比例缩放。
Maximum Height:Game视图高于这个数值开始按比例缩放。
这三种缩放方式全部都是按照高度计算缩放比例,完全忽略宽度。
public enum Scaling { PixelPerfect, FixedSize, FixedSizeOnMobiles, /// <summary> /// 根据宽度适配 /// </summary> FixedSizeofWidth, }
public float GetPixelSizeAdjustment (int height) { height = Mathf.Max(2, height); //修改1 if (scalingStyle == Scaling.FixedSize || scalingStyle == Scaling.FixedSizeofWidth) return (float)manualHeight / height; #if UNITY_IPHONE || UNITY_ANDROID if (scalingStyle == Scaling.FixedSizeOnMobiles) return (float)manualHeight / height; #endif if (height < minimumHeight) return (float)minimumHeight / height; if (height > maximumHeight) return (float)maximumHeight / height; return 1f; }
public int activeHeight :{ get { int height = Mathf.Max(2, Screen.height); //修改2 if (scalingStyle == Scaling.FixedSize || scalingStyle == Scaling.FixedSizeofWidth) return manualHeight; #if UNITY_IPHONE || UNITY_ANDROID if (scalingStyle == Scaling.FixedSizeOnMobiles) return manualHeight; #endif if (height < minimumHeight) return minimumHeight; if (height > maximumHeight) return maximumHeight; return height; } }
void Update () { #if UNITY_EDITOR if (!Application.isPlaying && gameObject.layer != 0) UnityEditor.EditorPrefs.SetInt("NGUI Layer", gameObject.layer); #endif if (mTrans != null) { float calcActiveHeight = activeHeight; if (calcActiveHeight > 0f ) { float size = 2f / calcActiveHeight; //看这里,看这里,看这里 if (scalingStyle == Scaling.FixedSizeofWidth) { float radio = (float)Screen.width / Screen.height; size = size * radio; } Vector3 ls = mTrans.localScale; if (!(Mathf.Abs(ls.x - size) <= float.Epsilon) || !(Mathf.Abs(ls.y - size) <= float.Epsilon) || !(Mathf.Abs(ls.z - size) <= float.Epsilon)) { mTrans.localScale = new Vector3(size, size, size); } } } }
public enum Style { None, Horizontal, Vertical, Both, BasedOnHeight, BasedOnWidth, FillKeepingRatio, FitInternalKeepingRatio }
if (style == Style.BasedOnHeight) { localScale.x = relativeSize.x * rectHeight; localScale.y = relativeSize.y * rectHeight; }else if (style == Style.BasedOnWidth) { localScale.x = relativeSize.x * rectWidth; localScale.y = relativeSize.y * rectWidth; } else if (style == Style.FillKeepingRatio) {……}