Skip to content

Instantly share code, notes, and snippets.

@iendsl
Created October 7, 2020 03:51
Show Gist options
  • Save iendsl/ae155ba2567160e5927f844e6dc83908 to your computer and use it in GitHub Desktop.
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.
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