Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEditor;
- using UnityEditor.SceneManagement;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- public class ObjectsReplace : MonoBehaviour
- {
- public GameObject prefabToInit;
- // Note your method should probably return something
- public void UpdateOrAddShaderPrefabToDoors()
- {
- GameObject[] doorsLeft = GameObject.FindGameObjectsWithTag("Door_Left");
- GameObject[] doorsRight = GameObject.FindGameObjectsWithTag("Door_Right");
- List<GameObject> allDoors = doorsLeft.Union(doorsRight).ToList();
- allDoors.ForEach(gameObject =>
- {
- Transform childTransform = gameObject.transform.Find("DoorShieldFXLocked Variant");
- GameObject child = childTransform?.gameObject;
- Debug.Log($"Child exist: {child != null}, Name of child: {child?.name}");
- if (child != null)
- {
- ModifyPrefab(child);
- }
- });
- }
- private void ModifyPrefab(GameObject child)
- {
- var mostPrefabInstanceRoot = PrefabUtility.GetOutermostPrefabInstanceRoot(child);
- // Get the Prefab Asset root GameObject and its asset path.
- string assetPath = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(mostPrefabInstanceRoot);
- // Load the contents of the Prefab Asset.
- GameObject contentsRoot = PrefabUtility.LoadPrefabContents(assetPath);
- //PrefabUtility.UnpackPrefabInstance(mostPrefabInstanceRoot, PrefabUnpackMode.Completely, UnityEditor.InteractionMode.AutomatedAction);
- // Modify Prefab contents.
- DestroyImmediate(child);
- PrefabUtility.SavePrefabAsset(mostPrefabInstanceRoot);
- if (PrefabUtility.IsPartOfAnyPrefab(mostPrefabInstanceRoot) == true)
- {
- // Save contents back to Prefab Asset and unload contents.
- PrefabUtility.ApplyPrefabInstance(mostPrefabInstanceRoot, InteractionMode.AutomatedAction);
- }
- PrefabUtility.UnloadPrefabContents(contentsRoot);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement