1

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);
        }

    }
}
Programmer
  • 121,791
  • 22
  • 236
  • 328
armnotstrong
  • 8,605
  • 16
  • 65
  • 130
  • You keep forgetting C# tag. What do you mean by "won't work"? – Programmer Jun 17 '17 at 07:38
  • sorry for that but I think it's not a c# issue more like a ngui issue,will add the tag though. By not work I mean the console throw a no reference exception, will edit to add that too :D – armnotstrong Jun 17 '17 at 07:42
  • "console throw a no reference exception" then it is C# and has a-lot to do with programming. C# is not required if this is just an Editor problem. I think I know what might be the problem. Maybe that's the problem...not sure but wait for my answer. – Programmer Jun 17 '17 at 07:43

1 Answers1

1

The UICamera.currentCamera variable is the last active camera that sent out the event. So if there is no event in the Start function, it will return null. When you try to use it later on, you will get the null exception message.

You should be using UICamera.current since it is the first camera that processes all the events.

And this works, but I think there maybe some performance issue since we ask for the currentCamera and assign the variable every Update()

This is correct.

You have to use UICamera.cachedCamera if you care about performance. It is meant just for that purpose. The UICamera.cachedCamera variable is not static so you need instance of UICamera to get that. You first have to get UICamera.current then the cached camera from it.

UICamera.current.cachedCamera

Finally, don'trust NGUI since it is a third-party plugin. You should always check for null before using the returned camera.

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

    if(cam != null)
    {
        //Use Camera
    }
}
Programmer
  • 121,791
  • 22
  • 236
  • 328