【发布时间】: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#。我想我知道可能是什么问题。也许这就是问题所在……不确定,但请等待我的回答。