I found we can't assign UICamera.currentCamera
to a field variable in NGUI
as the Camera.main
, we have to assign it every time in Update()
which I think may cause a performance issue:
this works
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);
}
}
}
But when change to UICamera.currentCamera
, it won't work
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);
}
}
}
And the console gives an error:
NullReferenceException: Object reference not set to an instance of an object
TestUICamera.Update () (at Assets/TestUICamera.cs:20)
And this works, but I think there maybe some performance issue since we ask for the currentCamera and assign the variable every 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);
}
}
}