//对齐方式
public enum E_Aligment_Type
{
Up,
Down,
Left,
Right,
Center,
Left_Up,
Right_Up,
Left_Down,
Right_Down,
}
//存储计算数据,不需要继承Mono
public class basePos
{
//处理控件位置相关内容,完成分辨率自适应相关计算
//返回给外部用于绘制控件
private Rect pos = new Rect(0,0,100,100);
//屏幕九宫格对齐方式
public E_Aligment_Type screen_Aligment_Type;
//控件中心对齐方式
public E_Aligment_Type control_Aligment_Type;
//偏移位置
public Vector2 offsetPos;
//控件宽高
public float cWidth = 100;
public float cHeight = 50;
private Vector2 controlCenterPos;
//计算控件中心点
private void CalcControlCenterPos()
{
switch (control_Aligment_Type)
{
case E_Aligment_Type.Up:
controlCenterPos.x = -cWidth / 2;
controlCenterPos.y = 0;
break;
case E_Aligment_Type.Down:
controlCenterPos.x = -cWidth / 2;
controlCenterPos.y = -cHeight;
break;
case E_Aligment_Type.Left:
controlCenterPos.x = 0;
controlCenterPos.y = -cHeight/2;
break;
case E_Aligment_Type.Right:
controlCenterPos.x = -cWidth;
controlCenterPos.y = -cHeight / 2;
break;
case E_Aligment_Type.Center:
controlCenterPos.x = -cWidth/2;
controlCenterPos.y = -cHeight / 2;
break;
case E_Aligment_Type.Left_Up:
controlCenterPos.x = 0;
controlCenterPos.y = 0;
break;
case E_Aligment_Type.Right_Up:
controlCenterPos.x = -cWidth;
controlCenterPos.y = 0;
break;
case E_Aligment_Type.Left_Down:
controlCenterPos.x = 0;
controlCenterPos.y = -cHeight ;
break;
case E_Aligment_Type.Right_Down:
controlCenterPos.x = -cWidth;
controlCenterPos.y = -cHeight;
break;
}
}
//计算最终坐标
private void CalcPos()
{
switch (screen_Aligment_Type)
{
case E_Aligment_Type.Up:
pos.x = Screen.width/2+controlCenterPos.x+offsetPos.x;
pos.y = 0+controlCenterPos.y+offsetPos.y;
break;
case E_Aligment_Type.Down:
pos.x = Screen.width / 2 + controlCenterPos.x + offsetPos.x;
pos.y = Screen.height + controlCenterPos.y - offsetPos.y;
break;
case E_Aligment_Type.Left:
pos.x = 0 + controlCenterPos.x + offsetPos.x;
pos.y = Screen.height/2 + controlCenterPos.y + offsetPos.y;
break;
case E_Aligment_Type.Right:
pos.x = Screen.width + controlCenterPos.x - offsetPos.x;
pos.y = Screen.height / 2 + controlCenterPos.y + offsetPos.y;
break;
case E_Aligment_Type.Center:
pos.x = Screen.width/2 + controlCenterPos.x + offsetPos.x;
pos.y = Screen.height / 2 + controlCenterPos.y + offsetPos.y;
break;
case E_Aligment_Type.Left_Up:
pos.x = 0 + controlCenterPos.x + offsetPos.x;
pos.y = 0+ controlCenterPos.y + offsetPos.y;
break;
case E_Aligment_Type.Right_Up:
pos.x = Screen.width + controlCenterPos.x - offsetPos.x;
pos.y = 0 + controlCenterPos.y + offsetPos.y;
break;
case E_Aligment_Type.Left_Down:
pos.x = 0 + controlCenterPos.x + offsetPos.x;
pos.y = Screen.height + controlCenterPos.y - offsetPos.y;
break;
case E_Aligment_Type.Right_Down:
pos.x = Screen.width + controlCenterPos.x - offsetPos.x;
pos.y = Screen.height + controlCenterPos.y - offsetPos.y;
break;
}
}
//得到最后的坐标
public Rect Pos
{
get
{
//计算控件中心点
CalcControlCenterPos();
//计算坐标
CalcPos();
//宽高直接赋值,返回给外部绘制控件
pos.width = cWidth;
pos.height = cHeight;
return pos;
}
}
}