public override void OnInspectorGUI() { serializedObject.Update(); base.OnInspectorGUI(); if (GUILayout.Button("Generate LevelCoinMap")) { _collectableIds.ClearArray(); AssetReferenceConfiguration config = (AssetReferenceConfiguration)_assetReferenceConfig.objectReferenceValue; for (int i = 0; i < config.LevelAssetReferences.Length; i++) { _collectableIds.InsertArrayElementAtIndex(i); SerializedProperty element = _collectableIds.GetArrayElementAtIndex(i); using (var editingScope = new PrefabUtility.EditPrefabContentsScope(AssetDatabase.GUIDToAssetPath(config.LevelAssetReferences[i].AssetGUID))) { element.FindPropertyRelative("LevelIndex").intValue = i; CollectableSpawnBehaviour[] collectables = editingScope.prefabContentsRoot.GetComponentsInChildren <CollectableSpawnBehaviour>(); SerializedProperty collectableIds = element.FindPropertyRelative("CollectableIds"); collectableIds.ClearArray(); for (var index = 0; index < collectables.Length; index++) { collectableIds.InsertArrayElementAtIndex(index); SerializedProperty newArrayElement = collectableIds.GetArrayElementAtIndex(index); newArrayElement.intValue = collectables[index].CollectableId; } } } } serializedObject.ApplyModifiedProperties(); }
private static void SetupBenchmarkPrefab(int index = -1) { // setup benchmark prefab using var benchmarkPrefab = new PrefabUtility.EditPrefabContentsScope(benchmarkPrefabPath); if (!benchmarkPrefab.prefabContentsRoot.TryGetComponent(out Benchmark b)) { return; } b.simpleRun = index != -1; b.simpleRunScene = index; b.urpVersion = GetURPPackageVersion(); }
void RenameUrlInPrefab(OpenUrl component, string newFormattedUrl, PrefabMatchInfo match) { // https://forum.unity.com/threads/how-do-i-edit-prefabs-from-scripts.685711/#post-4591885 using (var editingScope = new PrefabUtility.EditPrefabContentsScope(match.PrefabAssetPath)) { var prefabRoot = editingScope.prefabContentsRoot; var prefabbedComponent = prefabRoot.GetComponentsInChildren <OpenUrl>(true)[match.ComponentID]; prefabbedComponent.Url = newFormattedUrl; // https://forum.unity.com/threads/remove-all-missing-components-in-prefabs.897761/ GameObjectUtility.RemoveMonoBehavioursWithMissingScript(prefabRoot); } }