I’m trying to automatically mark GameObjects as Static, to replace the manual workflow of selecting the GO in the Hierarchy panel, going to the Inspector, and checking the “Static” checkbox.
Using the following Editor script, I was hoping this would be easy:
static void SetAssetAsStaticGameObject(string assetName)
{
// find the game object using the asset name
GameObject gameObjectFromAsset = GameObject.Find(assetName);
// set it as static for lighting to affect it
gameObjectFromAsset.isStatic = true;
Debug.Log("Setting '" + gameObjectFromAsset + "' as static...");
}
I can tell the code is getting hit and the GO is valid thanks to the Debug.Log statement, but when I inspect the object in the Inspector, the “Static” checkbox is stubbornly not checked.
I’ve also tried doing a for loop to enable “Static” on all of the children transforms, and that works only inconsistently for the children but not at all for the top-level Game Object.
Any thoughts on what I’m doing wrong?
Try SetStaticEditorFlags?
Also, when are you calling this method of yours? You’re using the ‘Find’ method, and I’m wondering if the object is instantiated and ready to be found when you call it. Not sure the timing on that in regards to editor scripting, I seldom if ever use the ‘find’ method.
1 Like
Thanks. I’ve tried the StaticEditorFlags, but they don’t work either:
// find the game object using the asset name
GameObject gameObjectFromAsset = GameObject.Find(assetName);
// set it as static for lighting to affect it
var staticFlags = StaticEditorFlags.LightmapStatic | StaticEditorFlags.OccluderStatic | StaticEditorFlags.OccludeeStatic | StaticEditorFlags.BatchingStatic | StaticEditorFlags.BatchingStatic | StaticEditorFlags.ReflectionProbeStatic;
GameObjectUtility.SetStaticEditorFlags(gameObjectFromAsset, staticFlags );
So far in my testing, I’ve already manually placed the GO from the Project pane to the Hierarchy window, so it should definitely be there, but I have code executed above this that instantiates it if it hasn’t been already. If it’s in the hierarchy window, “Find” should find it by name, right?
Here’s where I call this function. I’m looking for certain FBX files by name and configuring them as necessary.
void OnPreprocessModel() {
String assetFilePath = importer.assetPath.ToLower();
if (assetFilePath.Contains("some file name.fbx");
{
Debug.Log("Found file to update: " + assetFilePath);
// delete and reimport materials and textures
DeleteReimportMaterialsTextures(assetFilePath);
// set importer settings
SetStandardScaleColliderUVImportSettings(importer);
// set the gameObject as static
SetAssetAsStaticGameObject(globalAssetFileName);
}
}
I finally figured this out. My editor script was calling isStatic in the PreProcessor, but I just had to move the code to the PostProcessor for it to work correctly. Sometimes I also have to run the PostProcessor again, or save the project, for the Static setting to appear in the inspector. Quirky, but it works.