看这个的可以转到这个网址看看,http://www.manew.com/forum.php?mod=viewthread&tid=103675,他的做法稍微说下:1:将你的ui设置成居中类型 (也就是anchors的max min都是0.5f,)2:然后在点击你的rectTransform的下拉菜单点击setAanchors ,我的就不要看的,zz
“配置需求”:1:首先这个是ugui的啦,2:适用于RectTransform的poivt是(0.5f,0.5f)
在做ui的时候利用锚点适配,我们通常需要将锚点对应到图片(或者别的)的四个顶点(不知道你们有米有这种情况)也就是下图的情况
然后我们经常手动拖动,第一麻烦,第二,还有时候拖的不太精准,这个时候我的插件就"应运而生”了
下面我就开始了
首先我们写个编辑器类,做好开头工作:
好,并没有什么乱用,接下来就是重点
这个时候当我们回到编辑器,使用这个方法的时候
就这样了
好剩下的我们把锚点设置到四个顶点就可以了
ok 就完工了
看看效果
最后按照国际惯例附上代码
public class AnchorsAdapt {
[MenuItem("Tools/AnchorsAdapt")]
static void SelectionM()
{
GameObject[] gos = Selection.gameObjects;
for (int i = 0; i < gos.Length; i++)
{
if (gos[i].GetComponent<RectTransform>()==null)
continue;
Adapt(gos[i]);
}
}
static void Adapt(GameObject go)
{
//位置信息
Vector3 partentPos = go.transform.parent.position;
Vector3 localPos = go.transform.position;
//------获取rectTransform----
RectTransform partentRect = go.transform.parent.GetComponent<RectTransform>();
RectTransform localRect = go.GetComponent<RectTransform>();
float partentWidth = partentRect.rect.width;
float partentHeight = partentRect.rect.height;
float localWidth = localRect.rect.width * 0.5f;
float localHeight = localRect.rect.height * 0.5f;
//---------位移差------
float offX = localPos.x - partentPos.x;
float offY = localPos.y - partentPos.y;
float rateW = offX / partentWidth;
float rateH = offY / partentHeight;
localRect.anchorMax = localRect.anchorMin = new Vector2(0.5f + rateW, 0.5f + rateH);
localRect.anchoredPosition = Vector2.zero;
partentHeight = partentHeight * 0.5f;
partentWidth = partentWidth * 0.5f;
float rateX = (localWidth / partentWidth) * 0.5f;
float rateY = (localHeight / partentHeight) * 0.5f;
localRect.anchorMax = new Vector2(localRect.anchorMax.x + rateX, localRect.anchorMax.y + rateY);
localRect.anchorMin = new Vector2(localRect.anchorMin.x - rateX, localRect.anchorMin.y - rateY);
localRect.offsetMax = localRect.offsetMin = Vector2.zero;
}
}
欧拉,记得把脚本放在Editor文件夹下
在