原地址:http://game.ceeger.com/forum/read.php?tid=16571
1,给背景添加一个UIstretch, .将style选择最后一个FitInternalKeepingRatio, 将Intial Size设置为你当前屏幕的大小
2,再在背景下创建四个边的背景, 然后各自选择他们的Pivot , 上边图选择居下,下边图选择居上,左图边选择居右,右边图选择居左。
3,给这四个添加一个UIstretch, .将style选择为both,
4. 然后把下面这个脚本绑定到背景上,传上参数,
using UnityEngine;
using System.Collections;
public class FillBackGround : MonoBehaviour {
public UISprite backGround;
public UIStretch upBG;
public UIStretch downBG;
public UIStretch leftBG;
public UIStretch rightBG;
private float _bgWidth;
private float _bgHeight;
// Use this for initialization
void Start () {
//获取背景的宽和高
_bgWidth = backGround.width;
_bgHeight = backGround.height;
//填充背景上边
upBG.transform.localPosition = new Vector3(0, _bgHeight/2,0);
upBG.relativeSize.y=0.5f;
upBG.borderPadding.y = _bgHeight / 2;
//填充背景下边
downBG.transform.localPosition = new Vector3(0, -_bgHeight / 2, 0);
downBG.relativeSize.y = 0.5f;
downBG.borderPadding.y = _bgHeight / 2;
//填充背景左边
leftBG.transform.localPosition = new Vector3(-_bgWidth / 2, 0, 0);
leftBG.relativeSize.x = 0.5F;
leftBG.borderPadding.x = _bgWidth / 2;
//填充背景右边
rightBG.transform.localPosition = new Vector3(_bgWidth / 2, 0, 0);
rightBG.relativeSize.x = 0.5F;
rightBG.borderPadding.x = _bgWidth / 2;
}
// Update is called once per frame
}
效果运行效果如下
![点击查看原图](http://game.ceeger.com/forum/attachment/thumb/1404/thread/2_23674_09e3ee036aec971.png)
2,再在背景下创建四个边的背景, 然后各自选择他们的Pivot , 上边图选择居下,下边图选择居上,左图边选择居右,右边图选择居左。
![点击查看原图](http://game.ceeger.com/forum/attachment/1404/thread/2_23674_97cda673820e526.png)
![点击查看原图](http://game.ceeger.com/forum/attachment/1404/thread/2_23674_e8e55bd9cd024ce.png)
3,给这四个添加一个UIstretch, .将style选择为both,
![点击查看原图](http://game.ceeger.com/forum/attachment/1404/thread/2_23674_97a6ad685aa4421.png)
4. 然后把下面这个脚本绑定到背景上,传上参数,
![点击查看原图](http://game.ceeger.com/forum/attachment/1404/thread/2_23674_6cf5bd397544b8f.png)
using UnityEngine;
using System.Collections;
public class FillBackGround : MonoBehaviour {
public UISprite backGround;
public UIStretch upBG;
public UIStretch downBG;
public UIStretch leftBG;
public UIStretch rightBG;
private float _bgWidth;
private float _bgHeight;
// Use this for initialization
void Start () {
//获取背景的宽和高
_bgWidth = backGround.width;
_bgHeight = backGround.height;
//填充背景上边
upBG.transform.localPosition = new Vector3(0, _bgHeight/2,0);
upBG.relativeSize.y=0.5f;
upBG.borderPadding.y = _bgHeight / 2;
//填充背景下边
downBG.transform.localPosition = new Vector3(0, -_bgHeight / 2, 0);
downBG.relativeSize.y = 0.5f;
downBG.borderPadding.y = _bgHeight / 2;
//填充背景左边
leftBG.transform.localPosition = new Vector3(-_bgWidth / 2, 0, 0);
leftBG.relativeSize.x = 0.5F;
leftBG.borderPadding.x = _bgWidth / 2;
//填充背景右边
rightBG.transform.localPosition = new Vector3(_bgWidth / 2, 0, 0);
rightBG.relativeSize.x = 0.5F;
rightBG.borderPadding.x = _bgWidth / 2;
}
// Update is called once per frame
}
效果运行效果如下
![点击查看原图](http://game.ceeger.com/forum/attachment/1404/thread/2_23674_c57b1194652901f.png)
![点击查看原图](http://game.ceeger.com/forum/attachment/thumb/1404/thread/2_23674_09e3ee036aec971.png)
[松松果于2014-05-15 11:44编辑了帖子]