private void UpdateLayout() { if (_content2 == null && _content.texture == null && _content.frames == null) { if (_autoSize) { _updatingLayout = true; this.SetSize(50, 30); _updatingLayout = false; } return; } _contentWidth = _contentSourceWidth; _contentHeight = _contentSourceHeight; if (_autoSize) { _updatingLayout = true; if (_contentWidth == 0) { _contentWidth = 50; } if (_contentHeight == 0) { _contentHeight = 30; } this.SetSize(_contentWidth, _contentHeight); _updatingLayout = false; if (_width == _contentWidth && _height == _contentHeight) { if (_content2 != null) { _content2.SetPosition(0, 0); _content2.SetScale(1, 1); } else { _content.SetPosition(0, 0); _content.SetSize(_contentWidth, _contentHeight); } return; } //如果不相等,可能是由于大小限制造成的,要后续处理 } float sx = 1, sy = 1; if (_fill != FillType.None) { sx = this.width / _contentSourceWidth; sy = this.height / _contentSourceHeight; if (sx != 1 || sy != 1) { if (_fill == FillType.ScaleMatchHeight) { sx = sy; } else if (_fill == FillType.ScaleMatchWidth) { sy = sx; } else if (_fill == FillType.Scale) { if (sx > sy) { sx = sy; } else { sy = sx; } } else if (_fill == FillType.ScaleNoBorder) { if (sx > sy) { sy = sx; } else { sx = sy; } } if (_shrinkOnly) { if (sx > 1) { sx = 1; } if (sy > 1) { sy = 1; } } _contentWidth = (int)Math.Floor(_contentSourceWidth * sx); _contentHeight = (int)Math.Floor(_contentSourceHeight * sy); } } if (_content2 != null) { _content2.SetScale(sx, sy); } else { _content.size = new Vector2(_contentWidth, _contentHeight); } float nx; float ny; if (_align == AlignType.Center) { nx = (int)Math.Floor((this.width - _contentWidth) / 2); } else if (_align == AlignType.Right) { nx = (int)Math.Floor(this.width - _contentWidth); } else { nx = 0; } if (_verticalAlign == VertAlignType.Middle) { ny = (int)Math.Floor((this.height - _contentHeight) / 2); } else if (_verticalAlign == VertAlignType.Bottom) { ny = (int)Math.Floor(this.height - _contentHeight); } else { ny = 0; } if (_content2 != null) { _content2.SetPosition(nx, ny); } else { _content.SetPosition(nx, ny); } }