Can i revert only static tags to prefab ?

Hi, i noticed one thing while working in the editor. I have created brand new prefab, then i made adjustments to the static tags… When i place the prefab in the scene and do back to the prefab and change the tags, the copy of the prefab in the scene gets updated… I can do this as many times i want and is very fast way to setup static tags if you have already big level… (Very useful to set the navigation lightmapping and occlusion systems instead of setting each object separately in the scene) But there is one problem. If i change only once the static settings of the object in the scene and after that i go and change the settings of the prefab the object in the scene is not updated… I DO NOT WANT TO REVERT TO PREFAB because i already made another adjustments. It seems that the connection between the objects is still there but not for the static settings. Is it possible to revert only the static tags setting to the prefab by script somehow.

I wrote myself a script that does the job… :slight_smile: it is under Window Menu - Revert Static Flags;
You must have a selection in the scene. This script gets all the children game objects too. It applies the flags from the prefabs to the scene game obejcts

/*
This script reverts the Statc Flags of a game object to its prefab state;
Is it usefull for fast setups of most of the game objects in the scene
when is time to setup a Lightmapping, Occlusion Culling or Navigation System.
*/
using UnityEngine;
using UnityEditor;

public class RevertFlags : EditorWindow
{
	StaticEditorFlags prefFlags;

	[MenuItem ("Window/Revert Static Flags")]
	static void Init()
	{
		RevertFlags window = (RevertFlags)EditorWindow.GetWindow (typeof (RevertFlags));
	}

	void OnGUI()
	{
		EditorGUILayout.LabelField("RevertToStaticSettings", EditorStyles.boldLabel);

		if(GUI.Button(new Rect(1, 20, 150, 50), "Revert Static Flags"))
		{
			GameObject[] selectedGOs = Selection.gameObjects;
			foreach(GameObject obj in selectedGOs)
			{
				if(PrefabUtility.GetPrefabParent(obj) != null)
				{
					Debug.Log(PrefabUtility.GetPrefabParent(obj));
					prefFlags = GameObjectUtility.GetStaticEditorFlags((GameObject)PrefabUtility.GetPrefabParent(obj));
					GameObjectUtility.SetStaticEditorFlags((GameObject)obj, prefFlags);
				}
				
				Transform[] children = obj.transform.GetComponentsInChildren<Transform>();
				
				foreach(Transform ch in children)
				{
					if(PrefabUtility.GetPrefabParent(ch.gameObject) != null)
					{
						prefFlags = GameObjectUtility.GetStaticEditorFlags((GameObject)PrefabUtility.GetPrefabParent(ch.gameObject));
						GameObjectUtility.SetStaticEditorFlags((GameObject)ch.gameObject, prefFlags);
					}
				}
			}
		}
	}
}
2 Likes

I needed to do exactly what you did, so your script was very useful, thanks! However it’s using deprecated code by now so I took the liberty to rewrite it:

using UnityEngine;
using UnityEditor;

public static class RevertFlags
{
    [MenuItem("Tools/Match static flags to prefab.")]
    static void Match()
    {
        GameObject[] selectedGOs = Selection.gameObjects;
        foreach (GameObject obj in selectedGOs)
        {
            var prefab = PrefabUtility.GetCorrespondingObjectFromSource(obj);
            if (prefab != null)
            {
                var prefFlags = GameObjectUtility.GetStaticEditorFlags(prefab);
                GameObjectUtility.SetStaticEditorFlags(obj, prefFlags);
            }

            Transform[] children = obj.transform.GetComponentsInChildren<Transform>();

            foreach (Transform ch in children)
            {
                prefab = PrefabUtility.GetCorrespondingObjectFromSource(ch.gameObject);
                if (prefab == null) continue;
                var prefFlags = GameObjectUtility.GetStaticEditorFlags(prefab);
                GameObjectUtility.SetStaticEditorFlags(ch.gameObject, prefFlags);
            }
        }
    }
}
1 Like