【问题标题】:Unity 2d space between sprites精灵之间的统一二维空间
【发布时间】:2015-07-17 07:15:45
【问题描述】:

我正在开发的游戏有横向滚动背景。我有很多背景精灵 当第一个背景图像从屏幕边界退出时,我添加了另一个背景。 当我添加新背景时,我只需检查背景精灵的宽度,并在当前背景之后实例化(移动时),但它在背景图像之间显示细线。

renderer = mapObject.GetComponent<SpriteRenderer>();
mapWidth = renderer.bounds.size.x;

//instantiate new map to new position
addMap(newMap,new Vector3(mapWidth,0f,0f));

出现这个问题是因为我在移动 当前的一个?或者有什么想法可以解决这个问题?

【问题讨论】:

  • 如果您暂停滚动,该行会消失吗?可能是 VSYNC-Off 撕裂问题?
  • 感谢回复。即使暂停滚动也不会消失。所以我认为这是对象实例化的位置问题
  • 我尝试调整向 mapWidth 添加额外值(我不想手动执行)的空间,然后有时在滚动时出现线条。可能与定位不同,看起来像 VSync 问题这种情况。
  • 好的。现在尝试寻找禁用滚动的解决方案。如果您仍然可以看到它,那么我认为这与 Vsync 撕裂无关

标签: unity3d


【解决方案1】:

也许你的精灵的比例不是 (1, 1, 1)

你可以试试这个

GetComponent<Renderer>().bounds.size.x

我之前使用编辑器对精灵进行了统一处理。

这是我的代码

public void SortMaps() {
    float sceenHeight = Camera.main.orthographicSize * 2;
    float aspectRatio = 760f / 660f;
    float sceenWidth = sceenHeight * aspectRatio;
    float proportion = sceenWidth / mapBgs.transform.GetChild (0).GetComponent<SpriteRenderer>().sprite.bounds.size.x;
    //In my method the sprite's width should equal to the sceen's width, so the first sprite's position should be -(sceenHeight / 2 - sceenWidth / 2)
    float initY = -(sceenHeight / 2 - sceenWidth / 2);

    //First sprite
    mapBgs.transform.GetChild (0).name = "MapBg01";
    mapBgs.transform.GetChild (0).transform.position = new Vector3 (0, initY, 0);
    mapBgs.transform.GetChild (0).transform.localScale = new Vector3(proportion, proportion, 0);
    for (int i = 1; i < mapBgs.transform.childCount; i++)
    {
        mapBgs.transform.GetChild(i).name = "MapBg" + (i + 1).ToString ().PadLeft (2, '0');
        //change the scale to make the sprite adapt to the sceen
        mapBgs.transform.GetChild(i).transform.localScale = new Vector3(proportion, proportion, 0);
        //Next sprite's position 
        float y = mapBgs.transform.GetChild(i - 1).transform.position.y +
            mapBgs.transform.GetChild(i - 1).gameObject.GetComponent<Renderer>().bounds.size.y / 2 +
                mapBgs.transform.GetChild(i).gameObject.GetComponent<Renderer>().bounds.size.y / 2;
        mapBgs.transform.GetChild(i).transform.position = new Vector3 (0, y, 0);
    }
} 

【讨论】:

  • 感谢您的回复。正如您所说,游戏对象的比例不是 (1, 1, 1)。它是 (1.1, 1.1, 1.1) 但我尝试使用默认比例仍然出现线。我也使用了 GetComponent().bounds.size.x 但不起作用。你是在 Update 或 fixedUpdate 中添加地图吗?
  • 在游戏运行之前,我在编辑器中添加了地图。您是否使用 SetParent 或其他函数来更改游戏对象的父级?如果你这样做了,在 setParent 之后查看游戏对象的 localScale,它可能会改变。我建议您打印出 GetComponent().bounds.size.x 和位置。您可以在 IEnumerator 而不是 Update 中进行调试。
  • 问题是我在 FixedUpdate 函数中添加地图。我应该避免 Fixedupdate 吗?我在一个地图对象中有 3-4 个纹理。因为它有碰撞器。如果我在运行游戏之前添加所有地图,场景需要时间加载并且会出现内存问题。我没有使用 setParent。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多