http://blog.****.net/cube454517408/article/details/7907247 首先是参考此文;
main.cs作用:1.大图的拆分
2.判断是否成功
3.对碎片随机分配位置
4.按键的响应
pain.cs:针对每一个小plane,1.响应鼠标的拖拽
2.判断是否到达目的地
本文为第一步:大图拆分成多个碎片,让background的宽x占屏幕1/4
正交投影下:
怎么拆分成多个plane?
1.纹理的裁剪(以左下角为坐标原点)--->2.裁剪的纹理scale缩放;以此局部texture匹配整个plane大小----->plane缩小
怎么让plane的x长占屏幕固定比例?
1、首先,在正交投影下,渲染屏幕的宽是由camera的size决定的;
2、那么我们只要获取plane的size;然后通过plane的scale来缩放即可;也即一个比例关系求出scale;(本次只考虑plane的长宽相等情况)
怎么让plane移动到右侧,靠着右侧边缘?
1、渲染屏幕的长度L为:camera的size乘以2再乘以W/H宽高比;camera的size为width的一半
2、那么BackPlane的中心点向右移动的坐标为:L/2-plane.size.x*scale.x/2
结果如下:
如上图:验证拆分成功。
疑问:一直不知道初始设置的1280X720怎么获取,文档里screen.width获取的是current resolution运行时分辨率。
以下为代码:
using UnityEngine; using System.Collections; using UnityEditor; /****************************** * main * 1.切图,分割, * 2.移位到最右侧 * * / */ public class main : MonoBehaviour { public GameObject mPlane; public GameObject mPlaneParent; public GameObject mBackGround; private GameObject[] mPlaneAll; public Vector3[] mtestPos; //测试拆分用; public int mXNums;//水平线的个数;最少为两个;共有(mXNums-1)*(mYNums-1)张碎片 public int mYNums;//竖直线的个数; public int mPlaneRatio = 4;//水平比例为1/4 private Vector2 mBlock; private Vector2 mTextureBlock; private float mPicLength; private Vector2 mLeftTopCood; public void Awake() { Screen.fullScreen = !Screen.fullScreen; } //本次只考虑plane长宽相等的情况 // Use this for initialization void Start() { float planeX = mBackGround.GetComponent<MeshFilter>().mesh.bounds.size.x; float planeY = mBackGround.GetComponent<MeshFilter>().mesh.bounds.size.y; float planeZ = mBackGround.GetComponent<MeshFilter>().mesh.bounds.size.z; Debug.Log("x:" + planeX + "y:" + planeY + "z:" + planeZ); float W = Screen.width; float H = Screen.height; float C = Camera.main.orthographicSize; Debug.Log("orthographicSize:" + C); float M = mPlaneRatio; float P = planeX; float mScale = (W / H) * 2 * (C / M) / P; Debug.Log("scale:" + mScale); mBackGround.transform.localScale = new Vector3(mScale, 1.0f, mScale); mPicLength = mBackGround.transform.localScale.x; mBlock.x = mPicLength / (mXNums - 1); mBlock.y = mPicLength / (mYNums - 1); mTextureBlock.x = 1.0f / (mXNums - 1); mTextureBlock.y = 1.0f / (mYNums - 1); float W_long = C*W/H; float new_x = W_long - planeX*mScale/(2.0f); mBackGround.transform.localPosition = new Vector3(new_x, mBackGround.transform.localPosition.y, mBackGround.transform.localPosition.z); } void OnGUI() { if (GUI.Button(new Rect(10, 10, 100, 30), "拆分")) { Split(); } } void Split()//1.生成(mXNums-1)*(mYNums-1)张碎片; 2.随机分配位置 3.每张碎片不重叠 { mPlaneAll = new GameObject[(mXNums - 1) * (mYNums - 1)]; int k = 0; for (int i = 0; i < mXNums - 1; i++)//X轴的分割 { for (int j = 0; j < mYNums - 1; j++) { mPlaneAll[k] = (GameObject)Instantiate(mPlane); mPlaneAll[k].name = "plane" + k; mPlaneAll[k].transform.parent = mPlaneParent.transform; mPlaneAll[k].transform.localScale = new Vector3(mBlock.x, 1, mBlock.y);//使每个plane缩小到一个碎片的大小;注意Plane经过旋转后,scale坐标系发生了变化; //计算纹理的偏移offset(把中心点移位到左下角,依照LeftTop进行每块纹理坐标的获取) float offset_x = mTextureBlock.x * i;//按照网格的“每一列”算 float offset_y = 1.0f - mTextureBlock.y * (j + 1); //纹理的偏移和外界的变化无关,只和:拆分的程度有关。 //scaleTexture纹理缩小的比例 float scale_x = mTextureBlock.x; float scale_y = mTextureBlock.y; mPlaneAll[k].renderer.material.mainTextureOffset = new Vector2(offset_x, offset_y); mPlaneAll[k].renderer.material.mainTextureScale = new Vector2(scale_x, scale_y); mPlaneAll[k].SetActive(true); k++;//planeAll数组中:第一列从上到下的碎片,第二列......最后一列 } } for (int i = 0; i < (mXNums - 1) * (mYNums - 1); i++) { mPlaneAll[i].transform.localPosition = new Vector3(mtestPos[i].x, mtestPos[i].y, 0.0f); } } // Update is called once per frame void Update() { } }
接下来做一下碎片的随机分配。