Hi there! I am using Unity 2021.3.15f1.
What am i doing: I generate the scriptable through the code. After that I save the link of scriptable in new created prefab (is nested from another one). Lets call the prefab with link is LinkPrefab. My workflow:
- (Code example below) Check the folder, where must be scriptable. If not empty - remove all. Else - creating empty directory.
- Than I generate the scriptable and save them in new directory.
- (Code example below) After that I should create nested prefab. I create that, and check FileSystem to old prefab existing. If not exist - create new. If exist - replace link on scriptable using EditPrefabContentsScope.
Problem reproduce:
- before generation, old prefab and old scriptable must be existed.
- I generate new scriptables, which replace old scriptables and change the link in LinkPrefab.
- If I entry in LinkPrefab - all correct. If I entry in another prefabs (lets call PrefabContainer), that contains LinkPrefab - all correct. Bit when I resave the PrefabContainer, exit from prefab edit mode and entry again in PrefabContainer - all links to scriptable in LinkPrefab will be null and marked modified. If i revert them, save and reenter in PrefabContainer - they all the same will be null. I opened the PrefabContainer through Notepad after reverting modifiers - the modifier didn’t revert (see attached image).
I didn’t check that with non nested prefab, but I think, that problem will be reproduced again.
Has anyone experienced this? Or maybe unity specialists can help me?
Paragraph 1 code.
StringBuilder builderScriptable = new StringBuilder("Assets/ScriptableObjects/Game/");
builderScriptable.Append(transform.name);
builderScriptable.Append("/{0}.asset");
_folderName = builderScriptable.ToString();
string directory = Path.GetDirectoryName(_folderName);
if (Directory.Exists(directory))
{
string[] files = Directory.GetFiles(directory);
for (int i = 0; i < files.Length; i++)
{
File.Delete(files[i]);
}
files = null;
}
else
{
Directory.CreateDirectory(directory);
}
Paragraph 3.
TestPrefab newPrefab = (TestPrefab)PrefabUtility.InstantiatePrefab(_originalPrefab);
newPrefab.transform.position = Vector3.zero;
newPrefab.transform.rotation = Quaternion.identity;
newPrefab.name = transform.name;
string prefabPath = $"Assets/Prefabs/Game/{transform.name}.prefab";
if (!File.Exists(prefabPath))
{
PrefabUtility.SaveAsPrefabAsset(newPrefab.gameObject, prefabPath);
}
else
{
using var editingScope = new PrefabUtility.EditPrefabContentsScope(prefabPath);
TestPrefab prefab = editingScope.prefabContentsRoot.GetComponent<TestPrefab>();
prefab.Info = info;
EditorUtility.SetDirty(prefab);
PrefabUtility.RecordPrefabInstancePropertyModifications(prefab);
}
DestroyImmediate(newPrefab.gameObject);
DestroyImmediate(gameObject);