Switching cameras makes my event system hang my game
I am trying to do something that I deem to be n00b day one stuff and this issue defies all logic. I have a Canvas that covers my screen in overlay mode and when I click on an element in the scene I create a canvas in world space. I then activate a hidden camera after making it match the main camera's position and rotation and then animate the camera to a predefined position in front of the the world space camera using Lerp.
This works perfectly fine (n00b day one, after all). Unfortunately the canvas can't receive input because the canvas has no event camera set. Simple enough, simply get the Canvas component and set worldCamera to the temp camera. Job done. Now comes the weird part...
When I click on any of the buttons in the canvas it is supposed to call the exact same function that I used to animate the camera to the world position to now animate the temp camera back to where it came from with the function simply swapping the target and origin transforms used by the Lerp. Again, n00b day 1 stuff... only now I have a constant 1.5 FPS and I can't figure out why.
Looking at my EventSystem I can see the button I clicked on the World Space canvas is still the active event object and by manually disabling the EventSystem in the inspector and then re-enabling it returns my game to a perfectly fine FPS.
Why?
I tried disabling the event system before I start the loop and then re-enabling it afterwards but that causes hundreds or errors while the event system is disabled so that is not gonna fly when submitting the game. My question is basically this: Why is my EventSystem causing my system to go nuts and leave me with only 1.5 FPS?
My system has 16Gb of RAM and the game is only using 300Mb thereof. The scene has 35 draw calls and 400k triangles. According to the Profiler everything is well within range except it is taking forever to draw the scene. 980ms for something and then it seems something else is taking that long also. What am I not seeing here? What am I doing wrong?
IEnumerator ZoomIn(bool _zoom_in)
{
EventSystem.current.enabled = false;
Transform target, origin;
if (_zoom_in)
{
target = camera_position.transform;
origin = main_cam.transform;
main_cam.gameObject.SetActive(false);
} else
{
origin = camera_position.transform;
target = main_cam.transform;
}
temp_cam.transform.position = origin.position;
temp_cam.transform.rotation = origin.rotation;
temp_cam.gameObject.SetActive(true);
float progress = 0f;
while (progress < 1f)
{
progress = Mathf.Min(1f, progress + Time.deltaTime);
temp_cam.transform.position = Vector3.Lerp(origin.position, target.position, damp_curve.Evaluate(progress));
temp_cam.transform.rotation = Quaternion.Lerp(origin.rotation, target.rotation, damp_curve.Evaluate(progress));
yield return 1;
}
if (!_zoom_in)
{
CAData.CameraState = ECACameraState.Normal;
main_cam.gameObject.SetActive(true);
temp_cam.gameObject.SetActive(false);
}
EventSystem.current.enabled = true;
gameObject.SetActive(_zoom_in);
}
//used by the button on the canvas:
public void OnCloseClicked() => StartCoroutine(ZoomIn(false));
NOTE: By commenting out those two lines where I enable/disable the event system I get pathetic FPS until I simply click somewhere in my scene (after the main GUI is back in view) so that EventSystem.current now has something else to say was last selected and active and bla bla bla.
It would appear that I need a way to reset EventSystem.current to nothing or something other than what I clicked on and that should solve my issue. Anyone have any idea why or more importantly HOW to do that? I don't see any Reset or Clear methods to call...
Note 2:
Also, that code above for enabling/disabling the event system, that successfully disables it but doesn't enable it again for some reason. I need to do that manually via the inspector. I am assu$$anonymous$$g that when disabled EventSytem.current evaluates to null?
Note3: By changing the code for the eventsysetm to this:
GameObject eventsys = EventSystem.current.gameObject;
eventsys.SetActive(false);
and then after the loop I use SetActive(true), that gives me about a half second delay between clicking the buttonand anything happening but after that the animation works as expected and my FPS is up from 1.5 to 106 and everything is well with the world... except for the 999+ errors that got generated during that half a second due to this:
void Update(){
if (EventSystem.current.currentSelectedGameObject)
Debug.LogWarning(EventSystem.current.currentSelectedGameObject.name
+ " is selected");
}
So I simply commented that out and now my entire system works perfectly... the only question now being: "Why do I have to disable the EventSystem.gameObject temporarily?"
Answer by MrDude · Dec 12, 2018 at 03:46 AM
FFS.... How non-obvious is this!?!?!
In script A I have that loop and in script B I have the Update function that prints whatever is currently selected by EventSystem.current if any. THAT is causing it. Simply by removing that Update function all my issues are sorted. Now I no longer need to disable the EventSystem either... What the ...?!? [Insert red emote with a profanity filter over his mouth here]
Your answer
Follow this Question
Related Questions
Really High CPU usage On empty scenes/new projects (ApplicationTickTimer problem) profiler pics 0 Answers
Extreme low performance on mobile phone (UWP windows 10 mobile) with simple scene 1 Answer
Massive Rendering spike in profiler when unloading the scene 0 Answers
DrawMeshInstanced has bad perfomance on some Android devices 0 Answers