移动方位,需要自己调整到合适的位置。我设置的是Obj的正中心在屏幕左下角位置
public class test : MonoBehaviour {
public RectTransform Obj;
public float standard_width; //初始宽度
public float standard_height; //初始高度
private int _width;
private int _hight;
// Use this for initialization
void Start () {
_width = Screen.width;
_hight = Screen.height;
SetSize();
}
// Update is called once per frame
void Update () {
}
void SetSize()
{
float device_width = 0f; //当前设备宽度
float device_height = 0f; //当前设备高度
device_width = Screen.width;
device_height = Screen.height;
float S_x = device_width / standard_width; //宽度缩放比例
float S_y = device_height / standard_height; //高度缩放比例
//矫正比例
Obj.transform.localScale = new Vector3(Obj.transform.localScale.x * S_x, Obj.transform.localScale.y * S_y, Obj.transform.localScale.z);
//移动方位,用的时候自己调整这里的位置
Obj.transform.localPosition = new Vector3(-_width / 2, -_hight / 2 - Obj.rect.height / 2 * Obj.transform.localScale.y, 0);
}
}