RectTransform rectTransform= newGameObject.GetComponent<RectTransform>();
newGameObject.transform.parent = parent.transform;
rectTransform.anchoredPosition3D = Vector3.zero;
if (view.width > 0 || view.height > 0)
{
if (view.getType() == IDataType.web)
{
if (view.scale > 0)
{
newGameObject.transform.localScale = new Vector3(view.scale, view.scale, view.scale);
}
rectTransform.sizeDelta = new Vector2(view.width > 0 ? view.width : rectTransform.sizeDelta.x, view.height > 0 ? view.height : rectTransform.sizeDelta.y);
}
else
{
// 如果ratio=-1,则代表适用宽度,直接设置 宽高而非缩放到指定宽度
if (view.scale == -1)
{
rectTransform.sizeDelta = new Vector2(view.width > 0 ? view.width : rectTransform.sizeDelta.x, view.height > 0 ? view.height : rectTransform.sizeDelta.y);
parent.transform.localScale = Vector3.one;
}
else
if (view.width != 0 && view.height == 0) //基于宽度缩放
{
float scaleX = view.width / rectTransform.rect.width;
newGameObject.transform.localScale = new Vector3(scaleX, scaleX, scaleX);
}
else
if (view.width == 0 && view.height != 0)//基于高度缩放
{
float scaleY = view.height / rectTransform.rect.height;
newGameObject.transform.localScale = new Vector3(scaleY, scaleY, scaleY);
}
else
{
float scaleX = view.width / rectTransform.rect.width;
float scaleY = view.height / rectTransform.rect.height;
newGameObject.transform.localScale = new Vector3(scaleX, scaleY, 1);
}
}
rectTransform.pivot = parent.GetComponent<RectTransform>().pivot;
}
else
{
//直接设置比例
if (view.scale > 0)
{
newGameObject.transform.localScale = new Vector3(view.scale, view.scale, view.scale);
}
else //使用默认宽高约束 如果 比我设置的最大比例还要大,就不缩放了, 原比例默认
{
float scaleratio = _baseScaleWidth >= rectTransform.rect.width ? 1 : _baseScaleWidth / rectTransform.rect.width;
newGameObject.transform.localScale = new Vector3(scaleratio, scaleratio, scaleratio);
}
rectTransform.pivot = parent.GetComponent<RectTransform>().pivot;
}