Created
October 7, 2020 03:51
-
-
Save iendsl/ae155ba2567160e5927f844e6dc83908 to your computer and use it in GitHub Desktop.
A Unity component that prevents a user from editing a prefab in the Scene view.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
#if UNITY_EDITOR | |
using UnityEditor; | |
using UnityEditor.SceneManagement; | |
#endif | |
[ExecuteInEditMode] | |
public class PrefabLock : MonoBehaviour | |
{ | |
#if UNITY_EDITOR | |
// Update is called once per frame | |
void Update() | |
{ | |
if (Application.isPlaying) | |
{ | |
return; | |
} | |
if (!PrefabUtility.IsPartOfAnyPrefab(gameObject)) | |
{ | |
Debug.LogWarning("Must Be On Prefab!"); | |
this.enabled = false; | |
return; | |
} | |
List<ObjectOverride> overrides = PrefabUtility.GetObjectOverrides(gameObject); | |
foreach (ObjectOverride objectOverride in overrides) | |
{ | |
if (objectOverride.instanceObject == this) | |
{ | |
continue; | |
} | |
objectOverride.Revert(InteractionMode.AutomatedAction); | |
} | |
List<RemovedComponent> removedComponents = PrefabUtility.GetRemovedComponents(gameObject); | |
foreach (RemovedComponent component in removedComponents) | |
{ | |
component.Revert(InteractionMode.AutomatedAction); | |
} | |
} | |
#endif | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment