NGUI 为什么 UICamera.currentCamera 返回 null?
NGUI Why UICamera.currentCamera is returning null?
我发现我们无法在 NGUI 中将 UICamera.currentCamera
分配给字段变量
作为 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);
}
}
}
UICamera.currentCamera
变量是最后一个发出事件的活动摄像机。所以如果Start
函数中没有事件,就会returnnull
。当您稍后尝试使用它时,您将收到 null
异常消息。
您应该使用 UICamera.current
,因为它是第一个处理所有事件的相机。
And this works, but I think there maybe some performance issue since
we ask for the currentCamera and assign the variable every Update()
这是正确的。
如果您关心性能,则必须使用 UICamera.cachedCamera
。它就是为了这个目的。 UICamera.cachedCamera
变量不是静态的,因此您需要 UICamera
的实例来获取它。您首先必须获取 UICamera.current
,然后从中获取缓存的相机。
UICamera.current.cachedCamera
最后,不要相信NGUI,因为它是第三方插件。在使用 returned 相机之前,您应该始终检查是否为 null。
void Update()
{
Camera cam = UICamera.current.cachedCamera;
if(cam != null)
{
//Use Camera
}
}
我发现我们无法在 NGUI 中将 UICamera.currentCamera
分配给字段变量
作为 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);
}
}
}
UICamera.currentCamera
变量是最后一个发出事件的活动摄像机。所以如果Start
函数中没有事件,就会returnnull
。当您稍后尝试使用它时,您将收到 null
异常消息。
您应该使用 UICamera.current
,因为它是第一个处理所有事件的相机。
And this works, but I think there maybe some performance issue since we ask for the currentCamera and assign the variable every Update()
这是正确的。
如果您关心性能,则必须使用 UICamera.cachedCamera
。它就是为了这个目的。 UICamera.cachedCamera
变量不是静态的,因此您需要 UICamera
的实例来获取它。您首先必须获取 UICamera.current
,然后从中获取缓存的相机。
UICamera.current.cachedCamera
最后,不要相信NGUI,因为它是第三方插件。在使用 returned 相机之前,您应该始终检查是否为 null。
void Update()
{
Camera cam = UICamera.current.cachedCamera;
if(cam != null)
{
//Use Camera
}
}