Author Topic: can't assign UICamera.currentCamera to field variables  (Read 241 times)

armnotstrong

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
can't assign UICamera.currentCamera to field variables
« on: June 17, 2017, 02:33:00 AM »
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
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class TestUICamera : MonoBehaviour {
  6.  
  7.  
  8.     private Camera cam;
  9.     // Use this for initialization
  10.     void Start () {
  11.         cam = Camera.main;
  12.     }
  13.  
  14.     // Update is called once per frame
  15.     void Update () {
  16.  
  17.         if (Input.GetMouseButtonDown(0))
  18.         {
  19.             Vector3 point = cam.ScreenToWorldPoint(Input.mousePosition);
  20.             Debug.Log(" pos is :" + point);
  21.         }
  22.  
  23.     }
  24. }
  25.  
But when change to this, it won't work
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class TestUICamera : MonoBehaviour {
  6.  
  7.  
  8.     private Camera cam;
  9.     // Use this for initialization
  10.     void Start () {
  11.         cam = UICamera.currentCamera; //changing the camera to UICamera Here.
  12.     }
  13.  
  14.     // Update is called once per frame
  15.     void Update () {
  16.  
  17.         if (Input.GetMouseButtonDown(0))
  18.         {
  19.             Vector3 point = cam.ScreenToWorldPoint(Input.mousePosition);
  20.             Debug.Log(" pos is :" + point);
  21.         }
  22.  
  23.     }
  24. }
  25.  
And this works, but I think there maybe some performance issue since we ask for the currentCamera and assign the variable every Update():
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class TestUICamera : MonoBehaviour {
  6.  
  7.  
  8.     private Camera cam;
  9.     // Use this for initialization
  10.     void Start () {
  11.     }
  12.  
  13.     // Update is called once per frame
  14.     void Update () {
  15.         cam = UICamera.currentCamera;
  16.  
  17.         if (Input.GetMouseButtonDown(0))
  18.         {
  19.             Vector3 point = cam.ScreenToWorldPoint(Input.mousePosition);
  20.             Debug.Log(" pos is :" + point);
  21.         }
  22.  
  23.     }
  24. }
  25.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,137
  • Toronto, Canada
    • View Profile
Re: can't assign UICamera.currentCamera to field variables
« Reply #1 on: June 18, 2017, 07:27:55 PM »
All NGUI's 'current' type statics are valid only during NGUI's events such as OnClick, OnPress, etc. All other usage is undefined. The "Start" is most certainly not an NGUI event. If you are trying to get a specific camera, you should be referencing it in your script directly by having a public field, not relying on Camera.main or UICamera.currentCamera.