Unity最近在SpriteRenderer上添加了一个新的功能,以便使用tile系统。 当我用这些设置SpriteRenderer时:
我无法找到一个正确的方法来知道我的精灵中心部分在x和y轴上重复的次数。
带图像:
使用此sprite
我想知道在我的精灵渲染器中有多少次重复区域“E”
like in this exemple
SpriteRenderer.adaptiveModeThreshold是在此过程中需要考虑的重要值。 从Unity的文档:
拉伸值: 使用“拉伸值”滑块将值设置为0 和1.请注意,1表示调整大小为原始图像两倍的图像 尺寸,因此如果拉伸值设置为1,则重复该部分 当图像被拉伸到原始尺寸的两倍时。
文档链接:SpriteRenderer,adaptiveModeThreshold,9slice sprites
感谢您的时间!
到目前为止,我最好的尝试是:
SpriteRenderer sr = bc.GetComponent<SpriteRenderer>();
Sprite s = sr.sprite;
// retrieve data of sprite
float szTopBorder, szBottomBorder, szLeftBorder, szRightBorder; // sz of borders (in meter)
Rect innerRect; // the center rect of the spriterenderer without borders (in meter)
FoundInnerTilledRect(sr, out szTopBorder, out szBottomBorder, out szLeftBorder, out szRightBorder, out innerRect);
// the center size of sprite without border (in meter)
Vector2 sz = s.textureRect.size / s.pixelsPerUnit;
sz.x -= szLeftBorder + szRightBorder;
sz.y -= szBottomBorder + szTopBorder;
// Compute how many time the tile can repeat in "continuous" mode
float countX = (innerRect.size.x / sz.x);
float countY = (innerRect.size.y / sz.y);
if (sr.tileMode == SpriteTileMode.Adaptive)
{
// Compute how many time the tile can repeat in "adaptative" mode
countX = Mathf.Ceil(countX - sr.adaptiveModeThreshold);
countY = Mathf.Ceil(countY - sr.adaptiveModeThreshold);
}
这里是FoundInnerTilledRect()的代码
public static void FoundInnerTilledRect(SpriteRenderer sr, out float szTopBorder, out float szBottomBorder, out float szLeftBorder, out float szRightBorder, out Rect innerRect)
{
var s = sr.sprite;
float szX = sr.size.x;
float szY = sr.size.y;
szTopBorder = s.border.w / s.pixelsPerUnit;
szBottomBorder = s.border.y / s.pixelsPerUnit;
szLeftBorder = s.border.x / s.pixelsPerUnit;
szRightBorder = s.border.z / s.pixelsPerUnit;
innerRect = new Rect();
innerRect.xMin = szLeftBorder;
innerRect.xMax = szX - szRightBorder;
innerRect.yMin = szBottomBorder;
innerRect.yMax = szY - szTopBorder;
// set correct center for the rect
innerRect.center = - new Vector2((szRightBorder - szLeftBorder), (szTopBorder - szBottomBorder)) * 0.5f;
}