Skip to content

Instantly share code, notes, and snippets.

@paulhayes
Last active May 13, 2016 11:05
Show Gist options
  • Select an option

  • Save paulhayes/027ebfba95e6fe14c9ee70648e75ab10 to your computer and use it in GitHub Desktop.

Select an option

Save paulhayes/027ebfba95e6fe14c9ee70648e75ab10 to your computer and use it in GitHub Desktop.
Allows a user to save a prefab in play mode.
using UnityEngine;
using System.Collections;
public class SavePrefab : MonoBehaviour {
}
using UnityEngine;
using UnityEditor;
using System.Collections;
[CustomEditor(typeof(SavePrefab))]
public class SavePrefabEditor : Editor {
public override void OnInspectorGUI(){
SavePrefab saveTarget = target as SavePrefab;
if( GUILayout.Button("Save") ){
Object parentObject = PrefabUtility.GetPrefabParent(saveTarget.gameObject);
if( parentObject == null ){
Debug.LogWarningFormat("No prefab found for GameObject {0}",saveTarget.gameObject.name);
return;
}
PrefabUtility.ReplacePrefab( saveTarget.gameObject, parentObject );
AssetDatabase.SaveAssets();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment