/// <summary> /// 更新. /// </summary> void Update() { if (null == this._uiPanel) { return; } // 判断安全范围是否改变了 if (true == this._applyed) { return; } // 取得安全区域 DisplaySafePadding _safeArea = null; #if BUILD_DEBUG _safeArea = DeviceInfo.Instance.GetSafePadding(this.IsDebugTest); #else _safeArea = DeviceInfo.Instance.GetSafePadding(); #endif if ((null != _safeArea) && (false == this._defaultPadding.Equal(_safeArea))) { this._applyed = false; } // 应用安全区域 if (false == this._applyed) { ApplySafePadding(_safeArea); this._applyed = true; } }
void Awake() { if (null == this.Padding) { this.Padding = new RectOffset(0, 0, 0, 0); } if (null == this._defaultPadding) { this._defaultPadding = DisplaySafePadding.Create(0, 0, 0, 0); } _uiPanel = this.GetComponent <RectTransform> (); if (null == _uiPanel) { this.Error("Awake():There is no component which is RectTransform!!!"); return; } }
/// <summary> /// 应用安全区域. /// </summary> /// <param name="iSafePadding">安全边距.</param> private void ApplySafePadding(DisplaySafePadding iSafePadding) { this.Info("ApplySafePadding():{0}", iSafePadding.ToString()); if (true == DisplaySafePadding.Zero.Equal(this._defaultPadding)) { this._defaultPadding.Padding.left = (int)this._uiPanel.offsetMin.x; this._defaultPadding.Padding.right = (int)this._uiPanel.offsetMax.x; this._defaultPadding.Padding.top = (int)this._uiPanel.offsetMax.y; this._defaultPadding.Padding.bottom = (int)this._uiPanel.offsetMin.y; this._defaultPadding.isRetina = iSafePadding.isRetina; this._defaultPadding.RetinaScale = iSafePadding.RetinaScale; } // 设定左右边距 if (null != this._uiPanel) { this._uiPanel.pivot = new Vector2(0.5f, 0.5f); // 上边距 { float _temp = this._defaultPadding.Padding.top; if (true == iSafePadding.IsFullScreen()) { _temp += iSafePadding.Padding.top; _temp += this.Padding.top; } this._uiPanel.offsetMax = new Vector2(this._uiPanel.offsetMax.x, -_temp); } // 右边距 { float _temp = this._defaultPadding.Padding.right; if (true == iSafePadding.IsFullScreen()) { _temp += iSafePadding.Padding.right; _temp += this.Padding.right; } this._uiPanel.offsetMax = new Vector2(-_temp, this._uiPanel.offsetMax.y); } // 下边距 { float _temp = this._defaultPadding.Padding.bottom; if (true == iSafePadding.IsFullScreen()) { _temp += iSafePadding.Padding.bottom; _temp += this.Padding.bottom; _temp += this.PosOffset.y; } this._uiPanel.offsetMin = new Vector2(this._uiPanel.offsetMin.x, iSafePadding.Padding.bottom); } // 左边距 { float _temp = this._defaultPadding.Padding.left; if (true == iSafePadding.IsFullScreen()) { _temp += iSafePadding.Padding.left; _temp += this.Padding.left; _temp += this.PosOffset.x; } this._uiPanel.offsetMin = new Vector2(_temp, this._uiPanel.offsetMin.y); } } }