Since the “Total bake time” output of the lightbaking window is wrong I have written a script that writes the correct bake time into console using Lightmapping.bakeStarted and Lightmapping.bakeCompleted events.
So far it works correct but unluckily the events get called twice, so all outputs are doubled.
I am new to Editorscripting and to events, may anyone tell me why the events are called twice?:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEngine.UI;
[CustomEditor(typeof(LightbakingMonitor))]
public class LightbakingMonitorEditor : Editor
{
public Text textout;
public float seconds = 0;
private bool baking;
float timer = 0.0f;
LightbakingMonitor lbm;
private float temp = 10;
protected virtual void OnEnable()
{
#if UNITY_EDITOR
Debug.Log("Lightmapping Monitor enabled");
Lightmapping.bakeStarted += BakeStarted;
Lightmapping.bakeCompleted += BakeCompleted;
EditorApplication.update += OnEditorUpdate;
lbm = (LightbakingMonitor)target;
#endif
}
protected virtual void OnDisable()
{
#if UNITY_EDITOR
Debug.Log("Disbled");
EditorApplication.update -= OnEditorUpdate;
Lightmapping.bakeStarted -= BakeStarted;
Lightmapping.bakeCompleted -= BakeCompleted;
#endif
}
void BakeStarted()
{
timer = Time.realtimeSinceStartup;
baking = true;
Debug.Log("Baking Process started at: " + System.DateTime.Now.ToString());
}
void BakeCompleted()
{
seconds = Time.realtimeSinceStartup - timer;
baking = false;
if (seconds < 60)
Debug.Log("Baking completed after " + ((int)seconds).ToString() + " sec");
else
{
int minutes = (int)(seconds / 60.0f);
float sec = seconds - minutes * 60;
Debug.Log("Baking completed after " + ((int)minutes).ToString() + " min " + ((int)sec).ToString() + " sec");
}
lbm.PlaySound();
}
protected virtual void OnEditorUpdate()
{
#if UNITY_EDITOR
if (baking)
{
seconds = Time.realtimeSinceStartup - timer;
if(seconds>temp)
{
temp += 10;
if(seconds<60)
Debug.Log("Baking processed " + ((int)seconds).ToString() + " sec");
else
{
int minutes = (int)(seconds / 60.0f);
float sec = seconds - minutes * 60;
Debug.Log("Baking processed " + ((int)minutes).ToString() + " min " + ((int)sec).ToString() + " sec" );
}
}
}
#endif
}
}