3.xx 以上 绑定到UIRoot
usingUnityEngine; namespaceCom.Xyz.UI { [ExecuteInEditMode] [RequireComponent(typeof(UIRoot))] public classUIScreenAdaptive : MonoBehaviour { public int aspectRatioWidth = 1280; public int aspectRatioHeight = 720; public bool runOnlyOnce = false; privateUIRoot mRoot; private bool mStarted = false; private voidAwake() { UICamera.onScreenResize +=OnScreenResize; } private voidOnDestroy() { UICamera.onScreenResize -=OnScreenResize; } private voidStart() { mRoot = NGUITools.FindInParents<UIRoot>(this.gameObject); mRoot.scalingStyle =UIRoot.Scaling.FixedSize; this.Update(); mStarted = true; } private voidOnScreenResize() { if (mStarted &&runOnlyOnce) { this.Update(); } } private voidUpdate() { float defaultAspectRatio = aspectRatioWidth * 1f /aspectRatioHeight; float currentAspectRatio = Screen.width * 1f /Screen.height; if (defaultAspectRatio >currentAspectRatio) { int horizontalManualHeight = Mathf.FloorToInt(aspectRatioWidth /currentAspectRatio); mRoot.manualHeight =horizontalManualHeight; } else { mRoot.manualHeight =aspectRatioHeight; } if (runOnlyOnce &&Application.isPlaying) { this.enabled = false; } } } }