Editor Script to save UI prefab during Playmode in Unity

January 17, 2019

So last week I saw someone that wanted a option in the unity GUI to “save” it during runtime. While a normal save would be a rather dangerous feature in development. (Missclicks happen sooo often trust me) I understand the advantages to see your GUI in different situations and then able to adjust it accordingly would be a neat thing.

Long story short here is a script just paste it in an “Editor” folder and you will have a button on your canvas to save a clone into “Assets/”.


PS: Sorry somehow my pretty print plugin for the code is broken gotta fix that later 😉

* Copyright (c) 2016, Rudolf Chrispens.  All rights reserved.
* found on http://www.rudolf-chrispens.de/posts/
* Copyrights licensed under the GNU License.
* https://www.gnu.org/licenses/gpl-3.0
using UnityEngine;
using System.Collections;
using UnityEditor;
using UnityEngine.UI;

public class UISafeCopyInPlaymode : Editor 
    //Note this script will override the prefab if it exists already.
    string uiPath = "Assets/";
    string cloneName = "will be set automaticaly";

    public override void OnInspectorGUI()
        DrawDefaultInspector ();

        if(GUILayout.Button("Save UI during Playmode!")){

    void cloneTheUI(){
        Canvas myTarget = (Canvas)target;
        cloneName = myTarget.transform.name+".prefab";
        PrefabUtility.CreatePrefab(uiPath+cloneName, myTarget.gameObject);
        Debug.Log("Saved here: " + uiPath+cloneName);