void FilpBookPage(FilpDir filpDir) { //首先生成将要使用的翻转页 GenerateFilpPage(filpDir); if (filpDir == FilpDir.LeftToRight) { StartCoroutine("FilpFromLeftToRight"); } else { StartCoroutine("FilpFromRightToLeft"); } }
void GenerateFilpPage(FilpDir filpDir) { if (filpDir == FilpDir.LeftToRight) { //根据右边页,生成左边页的另一面 m_filpPageAnotherFace = Instantiate(m_rightPageRectTrans, m_filpPageRoot); //因为锚点布局引起的拉伸,通过Reset其宽度来恢复大小 m_filpPageAnotherFace.GetComponent <RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, m_rightPageRectTrans.GetComponent <RectTransform>().rect.height); //因为恢复大小之后,由于锚点造成的自动居中可以通过再次修改其位置来进行修正 m_filpPageAnotherFace.position = m_rightPageRectTrans.position; //旋转到指定位置,准备翻页 m_filpPageAnotherFace.RotateAround(new Vector2(transform.position.x, transform.position.y), Vector3.forward, 180); //根据左边页生成下一页的部分 //首先生成遮罩,遮罩的长度应该不小于书页的对角线长度(由于锚点设置会自动高度增加) m_willShowPageMask = Instantiate(m_leftPageRectTrans, m_filpPageRoot); //旋转到适当的位置 m_willShowPageMask.RotateAround(new Vector2(transform.position.x, transform.position.y), Vector3.forward, 90); //设置其透明度,alpha值为1(只为了mask的作用) m_willShowPageMask.GetComponent <Image>().color *= new Vector4(1, 1, 1, 1.0f / 180); //添加Mask m_willShowPageMask.gameObject.AddComponent <Mask>(); //创建下一页,也就是将要展示的右边那一页 //首先是克隆,但是由于其父节点的原因,会发生一些形变,以及位移,接下来的操作就是一步步纠正 m_willShowPage = Instantiate(m_leftPageRectTrans, m_willShowPageMask); //重新设置其宽度等于页的宽度,高度等于页的高度,最后将其与其父节点反向旋转相同的角度,最后纠正其位置 m_willShowPage.GetComponent <RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, m_leftPageRectTrans.GetComponent <RectTransform>().rect.width); m_willShowPage.GetComponent <RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, m_leftPageRectTrans.GetComponent <RectTransform>().rect.height); m_willShowPage.RotateAround(new Vector2(transform.position.x, transform.position.y), -Vector3.forward, 90); m_willShowPage.position = m_leftPageRectTrans.position; } else if (filpDir == FilpDir.RightToLeft) { //根据左边页,生成右边页的另一面 m_filpPageAnotherFace = Instantiate(m_leftPageRectTrans, m_filpPageRoot); //因为锚点布局引起的拉伸,通过Reset其宽度来恢复大小 m_filpPageAnotherFace.GetComponent <RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, m_leftPageRectTrans.GetComponent <RectTransform>().rect.height); //因为恢复大小之后,由于锚点造成的自动居中可以通过再次修改其位置来进行修正 m_filpPageAnotherFace.position = m_leftPageRectTrans.position; //旋转到指定位置,准备翻页 m_filpPageAnotherFace.RotateAround(new Vector2(transform.position.x, transform.position.y), -Vector3.forward, 180); //根据右边页生成下一页的部分 //首先生成遮罩,遮罩的长度应该不小于书页的对角线长度(由于锚点设置会自动高度增加) m_willShowPageMask = Instantiate(m_rightPageRectTrans, m_filpPageRoot); //旋转到适当的位置 m_willShowPageMask.RotateAround(new Vector2(transform.position.x, transform.position.y), -Vector3.forward, 90); //设置其透明度,alpha值为1(只为了mask的作用) m_willShowPageMask.GetComponent <Image>().color *= new Vector4(1, 1, 1, 1.0f / 180); //添加Mask m_willShowPageMask.gameObject.AddComponent <Mask>(); //创建下一页,也就是将要展示的右边那一页 //首先是克隆,但是由于其父节点的原因,会发生一些形变,以及位移,接下来的操作就是一步步纠正 m_willShowPage = Instantiate(m_rightPageRectTrans, m_willShowPageMask); //重新设置其宽度等于页的宽度,高度等于页的高度,最后将其与其父节点反向旋转相同的角度,最后纠正其位置 m_willShowPage.GetComponent <RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, m_rightPageRectTrans.GetComponent <RectTransform>().rect.width); m_willShowPage.GetComponent <RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, m_rightPageRectTrans.GetComponent <RectTransform>().rect.height); m_willShowPage.RotateAround(new Vector2(transform.position.x, transform.position.y), Vector3.forward, 90); m_willShowPage.position = m_rightPageRectTrans.position; } //随机颜色 m_filpPageAnotherFace.GetComponent <Image>().color = new Color(Random.Range(0f, 1), Random.Range(0f, 1), Random.Range(0f, 1), 1); m_willShowPage.GetComponent <Image>().color = new Color(Random.Range(0f, 1), Random.Range(0f, 1), Random.Range(0f, 1), 1); }