/// <summary>
/// 对UI做出限制
/// 将UI限制在屏幕内
/// </summary>
private void UIAstrict()
{
// 世界空间
Vector2 minworld = transform.TransformPoint(rect.rect.min);
Vector2 maxworld = transform.TransformPoint(rect.rect.max);
Vector2 sizeworld = maxworld - minworld;
// 保持最小的位置在屏幕边界-大小
maxworld = new Vector2(Screen.width, Screen.height) - sizeworld;
// 保持位置在(0,0)和maxworld之间
float y = Mathf.Clamp(minworld.y, 0, maxworld.y);
float x = Mathf.Clamp(minworld.x, 0, maxworld.x);
// set new position to xy(=local) + offset(=world)设置新位置为xy(=本地)+偏移量(=世界)
Vector2 offset = (Vector2)transform.position - minworld;
transform.position = new Vector2(x, y) + offset;
}