【问题标题】:Unity3d - Moving camera literally cuts FPS in half?Unity3d - 移动相机实际上将 FPS 减半?
【发布时间】:2015-07-19 23:33:24
【问题描述】:

我在使用 Unity 中的摄像头时遇到问题。当相机通过任何方式移动时,它似乎将我的 FPS 减半,甚至更多。这在 PC 上并不是很明显,除非我正在查看从 800 fps 到大约 150 fps 的速度,但是在移动设备上它会降低我在 Nexus 4 上达到 20 fps 的平滑 60 fps。这绝对是毁灭性的。

这是我正在使用的相机的属性和脚本但是,如果没有任何这些组件和完全重置的相机组件,这个问题仍然会发生:

public class ViewDrag : MonoBehaviour
{
    public Vector3 hit_position = Vector3.zero;
    public Vector3 current_position = Vector3.zero;
    public Vector3 camera_position = Vector3.zero;
    public Vector2 min_position;
    public Vector2 max_position;
    float z = 0.0f;
    MouseHolder holder;
    hider sidebarHide;

    GameObject gameStructure;

    // Use this for initialization
    void Start()
    {
        gameStructure = GameObject.FindGameObjectWithTag("GameStructure");
        holder = gameStructure.GetComponent<MouseHolder>();
        sidebarHide = GameObject.FindGameObjectWithTag("SidebarBG").GetComponent<hider>();
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            hit_position = Input.mousePosition;
            camera_position = transform.position;

        }
        if (Input.GetMouseButton(0))
        {
            current_position = Input.mousePosition;
            LeftMouseDrag();
        }

        if (!sidebarHide.isHidden)
        {
            //GetComponent<Camera2DFollow>().enabled = true;
        }
        if(gameStructure.GetComponent<ExecuteMovement2>().isExecuted)
        {
            //GetComponent<Camera2DFollow>().enabled = true;
        }
    }

    void LeftMouseDrag()
    {
        // From the Unity3D docs: "The z position is in world units from the camera."  In my case I'm using the y-axis as height
        // with my camera facing back down the y-axis.  You can ignore this when the camera is orthograhic.
        //current_position.z = hit_position.z = camera_position.y;

        // Get direction of movement.  (Note: Don't normalize, the magnitude of change is going to be Vector3.Distance(current_position-hit_position)
        // anyways.  
        Vector3 direction = Camera.main.ScreenToWorldPoint(current_position) - Camera.main.ScreenToWorldPoint(hit_position);

        // Invert direction to that terrain appears to move with the mouse.
        direction = direction * -1;

        Vector3 position = camera_position + direction;

        if (position.x < max_position.x && position.x > min_position.x && position.y < max_position.y && position.y > min_position.y)
        {
            if (!EventSystem.current.IsPointerOverGameObject() && holder.fromSlot == null )
            {
                if (sidebarHide.isHidden)
                {
                    if (!gameStructure.GetComponent<ExecuteMovement2>().isExecuted)
                    {
                        //GetComponent<Camera2DFollow>().enabled = false;
                        transform.position = position;
                    }
                }
            }
        }


    }
}

有没有人知道为什么会发生这种情况,如果不解决,我该如何解决?

通过仔细检查,我认为这与 Canvas 作为屏幕空间有关。但它有点需要这种方式。再次,任何解决方法?

检查 cmets 以获取分析器屏幕截图。

【问题讨论】:

  • 你试过分析吗?
  • 遗憾的是没有 Unity Pro。编辑:哎呀。只是意识到它现在可供所有统一用户使用。截取了这张截图:i.imgur.com/NaWSLXt.png 尖峰是我移动相机的地方。

标签: unity3d camera lag frame-rate


【解决方案1】:

问题已解决:

在分析器中,我发现 Canvas.SendWillRenderCanvases() 导致了巨大的峰值。我通过关闭画布中的 Pixel Perfect 完全解决了这个问题。现在像黄油一样光滑。

【讨论】:

  • @PauloHenrique 不用担心! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
  • 1970-01-01
  • 1970-01-01
  • 2012-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多