Changes made in prefab mode via script aren't marking the object as Dirty
Asked Answered
G

1

0

Hi there,

We have a set of tools which we use to allow artists to safely edit prefabs without breaking any references.
The tools simply destroy some GameObjects in a prefab, replace them with new ones, and then update the appropriate references.
However, since the change to prefabs, making these changes in Prefab Mode don’t register and the Save button never activates (although the prefab updates correctly). It also doesn’t trigger the autosave. Making the same changes by hand in the same mode does.
I’ve tried marking the prefab as dirty, but that doesn’t activate the save button either. The PrefabUtility options seems to save the object as an override, but we don’t want that, we want them to behave as they do when you edit it manually.
We are simply doing this from an inspector window:

public void LoadDummyCharacter( string dummyCharacterId )
	{
		CharacterBase newDummyChar = LoadCharacterFromResourceFolder( dummyCharacterId );

		if( myTarget.DummyCharacter != null )
		{
			DestroyImmediate( myTarget.DummyCharacter.gameObject );
		}

		if( newDummyChar != null )
		{
			myTarget.AssignDummyCharacterReference( newDummyChar );
			myTarget.DummyCharacter.transform.parent = myTarget.transform;
			myTarget.DummyCharacter.transform.localPosition = Vector3.zero;
			myTarget.DummyCharacter.transform.localScale = Vector3.one;
			myTarget.DummyCharacter.transform.localRotation = Quaternion.identity;
			myTarget.DummyCharacter.gameObject.SetActive( true );
		}
	}

Any thoughts on what I can do to make the prefab detect that it has been changed, so we can save it?

Grice answered 21/1, 2019 at 16:49 Comment(0)
P
0

The Prefab Editor is in a sense its own “scene”, so you have to actually not just mark the object dirty, but also call:

UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(gameObject.scene);

Then the “save” button will show as clickable after your edit.

Plosion answered 20/6, 2024 at 5:35 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.