private void InitData(Canvas canvas) { _model = new BookModel(); _bookSprites = Resources.LoadAll <Sprite>("Book"); if (_bookSprites.Length > 0) { _rect.sizeDelta = new Vector2(_bookSprites[0].rect.width * 2, _bookSprites[0].rect.height); } CurrentLeftId = -1; _isDragging = false; float scaleFactor = 1; if (canvas != null) { scaleFactor = canvas.scaleFactor; } //计算屏幕上书页的显示尺寸 float pageWidth = _rect.rect.width * scaleFactor / 2; float pageHeight = _rect.rect.height * scaleFactor; Vector3 pos = _rect.position + Vector3.down * pageHeight / 2; _model.BottomCenter = World2LoaclPos(pos); pos = _rect.position + Vector3.down * pageHeight / 2 + Vector3.right * pageWidth; _model.RightCorner = World2LoaclPos(pos); pos = _rect.position + Vector3.up * pageHeight / 2; _model.TopCenter = World2LoaclPos(pos); pos = _rect.position + Vector3.down * pageHeight / 2 + Vector3.left * pageWidth; _model.LeftCorner = World2LoaclPos(pos); float width = _rect.rect.width / 2; float height = _rect.rect.height; _model.PageWidth = width; _model.PageDiagonal = Mathf.Sqrt(Mathf.Pow(width, 2) + Mathf.Pow(height, 2)); _clippingMask.sizeDelta = new Vector2(_model.PageDiagonal, _model.PageDiagonal + _model.PageWidth); _model.ClippingPivotY = _model.PageWidth / _clippingMask.sizeDelta.y; _leftSideOfPage.InitShadow(new Vector2(_model.PageDiagonal, _model.PageDiagonal)); _rightSideOfPage.InitShadow(new Vector2(_model.PageDiagonal, _model.PageDiagonal)); }
/// <summary> /// 初始化数据 /// </summary> /// <param name="canvas"></param> private void InitData(Canvas canvas) { _model = new BookModel(); _sprites = Resources.LoadAll <Sprite>(_bookName); if (_sprites.Length > 0) { _rect.sizeDelta = new Vector2(_sprites[0].rect.width * 2, _sprites[0].rect.height); } CurrentLeftId = -1; //左边第一页是空白页 _isDragging = false; //默认不是拖动状态 float scaleFactor = 1; if (canvas != null) { scaleFactor = canvas.scaleFactor; } //计算屏幕上书页的显示尺寸,受画布缩放比例的影响 float pageWidth = _rect.rect.width * scaleFactor; float pageHeight = _rect.rect.height * scaleFactor; //获取底边中点、顶边中点、左页左下角顶点、右页右下角顶点 Vector3 pos = _rect.position + Vector3.down * pageHeight / 2; _model.BottomCenter = World2LocalPos(pos); pos = _rect.position + Vector3.up * pageHeight / 2; _model.TopCenter = World2LocalPos(pos); pos = _rect.position + Vector3.down * pageHeight / 2 + Vector3.left * pageWidth / 2; _model.LeftCorner = World2LocalPos(pos); pos = _rect.position + Vector3.down * pageHeight / 2 + Vector3.right * pageWidth / 2; _model.RightCorner = World2LocalPos(pos); //获取一页的宽度和对角线长度 float width = _rect.rect.width / 2; float height = _rect.rect.height; _model.PageWidth = width; _model.PageDiagonal = Mathf.Sqrt(Mathf.Pow(width, 2) + Mathf.Pow(height, 2)); //获取剪切遮罩宽高及轴心点Y值 _clippingMask.sizeDelta = new Vector2(_model.PageDiagonal, _model.PageDiagonal + _model.PageWidth); _model.ClippingPivotY = _model.PageWidth / _clippingMask.sizeDelta.y; //设置拖拽页正面和背面的阴影大小 _leftSide.InitShadow(new Vector2(_model.PageDiagonal, _model.PageDiagonal)); _rightSide.InitShadow(new Vector2(_model.PageDiagonal, _model.PageDiagonal)); }