【问题标题】:NGUI Why UICamera.currentCamera is returning null?NGUI 为什么 UICamera.currentCamera 返回 null?
【发布时间】:2017-06-17 07:26:39
【问题描述】:

我发现我们无法将UICamera.currentCamera 分配给 NGUI 中的字段变量 作为Camera.main,我们每次都必须在Update() 中分配它,我认为这可能会导致性能问题:

这行得通

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestUICamera : MonoBehaviour {


    private Camera cam;
    // Use this for initialization
    void Start () {
        cam = Camera.main;
    }

    // Update is called once per frame
    void Update () {

        if (Input.GetMouseButtonDown(0))
        {
            Vector3 point = cam.ScreenToWorldPoint(Input.mousePosition);
            Debug.Log(" pos is :" + point);
        }

    }
}

但是换成UICamera.currentCamera就不行了

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestUICamera : MonoBehaviour {


    private Camera cam;
    // Use this for initialization
    void Start () {
        cam = UICamera.currentCamera; //changing the camera to UICamera Here.
    }

    // Update is called once per frame
    void Update () {

        if (Input.GetMouseButtonDown(0))
        {
            Vector3 point = cam.ScreenToWorldPoint(Input.mousePosition);
            Debug.Log(" pos is :" + point);
        }

    }
}

控制台报错:

 NullReferenceException: Object reference not set to an instance of an object
 TestUICamera.Update () (at Assets/TestUICamera.cs:20)

这可行,但我认为可能存在一些性能问题,因为我们要求 currentCamera 并每隔 Update() 分配变量:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestUICamera : MonoBehaviour {


    private Camera cam;
    // Use this for initialization
    void Start () {
    }

    // Update is called once per frame
    void Update () {
        cam = UICamera.currentCamera;

        if (Input.GetMouseButtonDown(0))
        {
            Vector3 point = cam.ScreenToWorldPoint(Input.mousePosition);
            Debug.Log(" pos is :" + point);
        }

    }
}

【问题讨论】:

  • 你总是忘记 C# 标签。你说的“行不通”是什么意思?
  • 很抱歉,但我认为这不是 c# 问题,更像是 ngui 问题,不过会添加标签。不工作是指控制台抛出无​​引用异常,将编辑添加:D
  • "console throw a no reference exception" 那么它是 C# 并且与编程有很大关系。如果这只是编辑器问题,则不需要 C#。我想我知道可能是什么问题。也许这就是问题所在……不确定,但请等待我的回答。

标签: c# unity3d ngui


【解决方案1】:

UICamera.currentCamera 变量是发出事件的最后一个活动摄像机。所以如果Start函数中没有事件,它将返回null。当您稍后尝试使用它时,您将收到null 异常消息。

您应该使用UICamera.current,因为它是第一个处理所有事件的摄像头。

这可行,但我认为可能存在一些性能问题,因为 我们要求 currentCamera 并为每个 Update() 分配变量

这是正确的。

如果你关心性能,你必须使用UICamera.cachedCamera。它只是为了这个目的。 UICamera.cachedCamera 变量不是静态的,因此您需要 UICamera 的实例来获得它。您首先必须获取UICamera.current,然后从中获取缓存的摄像头。

UICamera.current.cachedCamera

最后,不要相信 NGUI,因为它是一个第三方插件。在使用返回的相机之前,您应该始终检查 null。

void Update()
{
    Camera cam = UICamera.current.cachedCamera;

    if(cam != null)
    {
        //Use Camera
    }
}

【讨论】:

  • 感谢您解释机制的完整视图:D
  • 不客气。更改了标题以指出正在发生的事情,以便其他遇到您问题的人可以找到它。觉得不好可以改回来。
  • 很高兴你改变了这一点,我的英语很差,但我正在努力练习:D
  • 我第一次读到你的问题就明白了,所以我不认为你的英语不好。编码愉快!
猜你喜欢
  • 2022-12-26
  • 2016-12-28
  • 2015-11-25
  • 2015-07-04
  • 2019-09-29
  • 2015-02-25
  • 2015-10-21
  • 2021-08-12
  • 2011-03-18
相关资源
最近更新 更多