I have created a CustomEditor to work with lightmaps more easily. As part of it, I defined private functions that the custom editor can use. However, when I compile the script, those methods are never called. No console errors at all. Just a black hole. These methods have to be defined in the editor script because they reference Editor classes, and the scene won't build if the functions are in the in-game script. (They do run if the they are there.)
One more wrinkle: these functions also reference the target class. Is that the problem?
(The BakeLightmap function is the one that OnGUI is trying to call, but which never gets called.)
import System;
@CustomEditor(TimeOfDayManager)
class TimeOfDayMgrEditor extends Editor {
var currentTOD : TimeOfDay;
var newDayTime : DayTime;
var newLocation : Location;
function OnEnable () {
if (!target.currentDayTime) target.currentDayTime = DayTime.dawn;
if (!target.levelLocation) target.levelLocation = Location.SloughCreek;
if (!target.sunLite) target.sunLite = GameObject.FindObjectOfType(Light);
if (!target.sun) target.sun = target.sunLite.transform as Transform;
currentTOD = target.GetTimeOfDayFromDayTime(target.currentDayTime);
newDayTime = target.currentDayTime;
newLocation = target.levelLocation;
}
function OnInspectorGUI() {
//DrawDefaultInspector ();
EditorGUILayout.BeginHorizontal ();
newDayTime = EditorGUILayout.EnumPopup("Time Of Day:", newDayTime);
newLocation = EditorGUILayout.EnumPopup("Level Location:", newLocation);
EditorGUILayout.EndHorizontal ();
EditorGUILayout.BeginHorizontal ();
EditorGUILayout.LabelField("SUN","");
EditorGUILayout.EndHorizontal ();
EditorGUILayout.BeginHorizontal ();
currentTOD.sunHeading = EditorGUILayout.FloatField("Heading:",currentTOD.sunHeading);
currentTOD.sunElevation = EditorGUILayout.FloatField("Elevation:",currentTOD.sunElevation);
GUILayout.FlexibleSpace();
EditorGUILayout.EndHorizontal ();
EditorGUILayout.BeginHorizontal ();
currentTOD.sunColor = EditorGUILayout.ColorField("Color:",currentTOD.sunColor);
EditorGUILayout.EndHorizontal ();
EditorGUILayout.BeginHorizontal ();
currentTOD.sunIntensity = EditorGUILayout.Slider("Intensity:",currentTOD.sunIntensity, 0.0, 3.0);
EditorGUILayout.EndHorizontal ();
EditorGUILayout.BeginHorizontal ();
currentTOD.ambientColor = EditorGUILayout.ColorField("Ambient Light:",currentTOD.ambientColor);
EditorGUILayout.EndHorizontal ();
EditorGUILayout.BeginHorizontal ();
currentTOD.globalFogColor = EditorGUILayout.ColorField("Fog Color:",currentTOD.globalFogColor);
EditorGUILayout.EndHorizontal ();
EditorGUILayout.BeginHorizontal ();
RenderSettings.fog = EditorGUILayout.Toggle("Preview Fog:",RenderSettings.fog);
EditorGUILayout.EndHorizontal ();
EditorGUILayout.BeginHorizontal ();
currentTOD.sunShadowStrength = EditorGUILayout.Slider("Shadow Strength:",currentTOD.sunShadowStrength, 0.0, 1.0);
EditorGUILayout.EndHorizontal ();
EditorGUILayout.BeginHorizontal ();
currentTOD.waterColor = EditorGUILayout.ColorField("Water:",currentTOD.waterColor);
EditorGUILayout.EndHorizontal ();
EditorGUILayout.BeginHorizontal ();
EditorGUILayout.LabelField("LIGHTMAPPING","");
EditorGUILayout.EndHorizontal ();
EditorGUILayout.BeginHorizontal ();
currentTOD.bounceBoost = EditorGUILayout.FloatField("Bounce Boost:",currentTOD.bounceBoost);
currentTOD.bounceIntensity = EditorGUILayout.FloatField("Bounce Intensity:",currentTOD.bounceIntensity);
GUILayout.FlexibleSpace();
EditorGUILayout.EndHorizontal ();
EditorGUILayout.BeginHorizontal();
target.basePath = EditorGUILayout.TextField("Asset Folder:", target.basePath);
EditorGUILayout.EndHorizontal();
EditorGUILayout.BeginHorizontal ();
if (GUILayout.Button ("Bake Current Preview")) BakeLightmaps (true);
GUILayout.Space(10);
if (GUILayout.Button ("Bake and Save All")) BakeLightmaps (false);
GUILayout.FlexibleSpace();
EditorGUILayout.EndHorizontal ();
if (GUI.changed) {
EditorUtility.SetDirty (target);
var oldDayTime : DayTime = target.currentDayTime;
var oldLocation : Location = target.levelLocation;
if ((newDayTime != oldDayTime) || (newLocation != oldLocation)) {
if (newDayTime != oldDayTime) {
target.currentDayTime = newDayTime;
currentTOD = target.GetTimeOfDayFromDayTime(target.currentDayTime);
}
if (newLocation != oldLocation)
target.levelLocation = newLocation;
target.SetTimeOfDayToCurrent();
} else {
target.SetSun (target.currentDayTime);
target.SetWater (target.currentDayTime);
}
}
}
private function BakeLightmaps (isPreview : boolean) {
var currentTOD : TimeOfDay = target.GetTimeOfDayFromDayTime (target.currentDayTime);
if (isPreview) {
Debug.Log ("Baking low quality preview lightmaps for this time of day...");
// set the changes from the sun from time of day
// the rest of the settings in terms of number of bounces, etc., are set in the main lightmapping panel
LightmapEditorSettings.skyLightColor = currentTOD.sunColor;
LightmapEditorSettings.skyLightIntensity = currentTOD.sunIntensity;
LightmapEditorSettings.bounceBoost = currentTOD.bounceBoost;
LightmapEditorSettings.bounceIntensity = currentTOD.bounceIntensity;
LightmapEditorSettings.quality = LightmapBakeQuality.Low;
Lightmapping.BakeAsync ();
} else {
var confirm : boolean = EditorUtility.DisplayDialog("Full Bake Warnings:","Is the terrain lightmap resolution set to the final value in the Lightmapping->Object tab? This will replace all existing lightmaps for this scene.", "Bake All", "Cancel");
if (!confirm) return;
LightmapEditorSettings.quality = LightmapBakeQuality.High;
// go through all the times of day and bake and save the lightmaps
for (var i=0; i<4; i++) {
var day : DayTime = target.GetDayTimeByIndex(i);
var tod : TimeOfDay = target.GetTimeOfDayFromDayTime(day);
target.SetSun(day);
LightmapEditorSettings.skyLightColor = tod.sunColor;
LightmapEditorSettings.skyLightIntensity = tod.sunIntensity;
LightmapEditorSettings.bounceBoost = tod.bounceBoost;
LightmapEditorSettings.bounceIntensity = tod.bounceIntensity;
Debug.Log ("Baking lightmaps for " + Enum.GetName(DayTime, day));
Lightmapping.BakeAsync ();
while (Lightmapping.isRunning) yield;
for (var j=0; j < LightmapSettings.lightmaps.length; j++) {
confirm = SaveLightmap("LightmapNear-" + j.ToString() + ".exr", day);
if (!confirm) return;
confirm = SaveLightmap("LightmapFar-" + j.ToString() + ".exr", day);
if (!confirm) return;
}
}
Debug.Log ("Bake of full set of times of day lightmaps complete for this scene!");
}
}
private function SaveLightmap (mapFile : String, theDayTime : DayTime) : boolean {
var mapSourcePath : String = EditorApplication.currentScene.Substring(0, EditorApplication.currentScene.LastIndexOf(".")) + "/";
var mapDestPath : String = "Assets/0 Game Assets/Resources/" + target.GetPath(target.levelLocation, theDayTime, AssetType._lightmaps);
Debug.Log ("Saving "+mapFile+" from " + mapSourcePath + " to " + mapDestPath);
// The destination folders in Resources/Atmospheres must exist already
var err2 : String;
AssetDatabase.DeleteAsset (mapDestPath+mapFile);
var err : String = AssetDatabase.ValidateMoveAsset(mapSourcePath+mapFile, mapDestPath+mapFile);
if (err == "")
err2 = AssetDatabase.MoveAsset(mapSourcePath+mapFile, mapDestPath+mapFile);
if (err || err2) {
EditorUtility.DisplayDialog("Lightmap Save Failed", "Bake aborted. The lightmap set is not complete. "+err + err2, "OK", "");
return false;
} else return true;
}
}
The problem appears to be that the private function calls can't call the target runtime functions, even though the main editorgui loop can. Moving the code inline doesn't work, since baking the lighmaps and savign them relies on starting a new thread. Ack!
– anon34405538