Advertisement
Chocolade

Untitled

Feb 16th, 2020
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEditor;
  5. using UnityEditor.SceneManagement;
  6. using UnityEngine;
  7. using UnityEngine.SceneManagement;
  8.  
  9. public class ObjectsReplace : MonoBehaviour
  10. {
  11. public GameObject prefabToInit;
  12.  
  13. // Note your method should probably return something
  14. public void UpdateOrAddShaderPrefabToDoors()
  15. {
  16. GameObject[] doorsLeft = GameObject.FindGameObjectsWithTag("Door_Left");
  17. GameObject[] doorsRight = GameObject.FindGameObjectsWithTag("Door_Right");
  18.  
  19. List<GameObject> allDoors = doorsLeft.Union(doorsRight).ToList();
  20.  
  21. allDoors.ForEach(gameObject =>
  22. {
  23. Transform childTransform = gameObject.transform.Find("DoorShieldFXLocked Variant");
  24. GameObject child = childTransform?.gameObject;
  25. Debug.Log($"Child exist: {child != null}, Name of child: {child?.name}");
  26. if (child != null)
  27. {
  28. ModifyPrefab(child);
  29. }
  30. });
  31. }
  32.  
  33. private void ModifyPrefab(GameObject child)
  34. {
  35.  
  36. var mostPrefabInstanceRoot = PrefabUtility.GetOutermostPrefabInstanceRoot(child);
  37.  
  38. // Get the Prefab Asset root GameObject and its asset path.
  39. string assetPath = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(mostPrefabInstanceRoot);
  40.  
  41. // Load the contents of the Prefab Asset.
  42. GameObject contentsRoot = PrefabUtility.LoadPrefabContents(assetPath);
  43.  
  44. //PrefabUtility.UnpackPrefabInstance(mostPrefabInstanceRoot, PrefabUnpackMode.Completely, UnityEditor.InteractionMode.AutomatedAction);
  45.  
  46. // Modify Prefab contents.
  47. DestroyImmediate(child);
  48. PrefabUtility.SavePrefabAsset(mostPrefabInstanceRoot);
  49.  
  50. if (PrefabUtility.IsPartOfAnyPrefab(mostPrefabInstanceRoot) == true)
  51. {
  52. // Save contents back to Prefab Asset and unload contents.
  53. PrefabUtility.ApplyPrefabInstance(mostPrefabInstanceRoot, InteractionMode.AutomatedAction);
  54. }
  55. PrefabUtility.UnloadPrefabContents(contentsRoot);
  56. }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement