|
| 1 | +using UnityEditor; |
| 2 | +using UnityEngine; |
| 3 | +using System.IO; |
| 4 | +// Editor tool to easily paste scripts from web (automagically creates new file) |
| 5 | +// original source: https://unitycoder.com/blog/2017/07/12/editor-plugin-paste-script-to-file/ |
| 6 | + |
| 7 | +namespace UnityLibrary |
| 8 | +{ |
| 9 | + public class CopyPasteHelper : EditorWindow |
| 10 | + { |
| 11 | + // settings: output folder is set as Assets/Scripts/Paste/ |
| 12 | + static string baseFolder = "Scripts"; |
| 13 | + static string subFolder = "Paste"; |
| 14 | + |
| 15 | + [MenuItem("Window/UnityLibrary/CopyPasteHelper")] |
| 16 | + public static void ShowWindow() |
| 17 | + { |
| 18 | + //Show existing window instance. If one doesn't exist, make one. |
| 19 | + var window = EditorWindow.GetWindow(typeof(CopyPasteHelper)); |
| 20 | + window.titleContent = new GUIContent("CopyPasteHelper"); |
| 21 | + window.minSize = new Vector2(64, 24); |
| 22 | + } |
| 23 | + |
| 24 | + // mainloop |
| 25 | + void OnGUI() |
| 26 | + { |
| 27 | + if (GUILayout.Button("C#", GUILayout.Width(44))) |
| 28 | + { |
| 29 | + CreateScript(); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + static void CreateScript() |
| 34 | + { |
| 35 | + // combine paths, TODO: cleanup |
| 36 | + var fileName = "NewScript.cs"; // default filename if cannot parse from text |
| 37 | + var mainFolder = Application.dataPath + Path.DirectorySeparatorChar + baseFolder + Path.DirectorySeparatorChar; |
| 38 | + var childFolder = subFolder + Path.DirectorySeparatorChar; |
| 39 | + var fullPath = mainFolder + childFolder + fileName; |
| 40 | + |
| 41 | + // check folders and create them if missing |
| 42 | + if (Directory.Exists(mainFolder) == false) |
| 43 | + { |
| 44 | + Debug.Log("Creating missing folder: " + mainFolder); |
| 45 | + AssetDatabase.CreateFolder("Assets", baseFolder); |
| 46 | + } |
| 47 | + if (Directory.Exists(mainFolder + childFolder) == false) |
| 48 | + { |
| 49 | + Debug.Log("Creating missing folder: " + mainFolder + childFolder); |
| 50 | + AssetDatabase.CreateFolder("Assets" + Path.DirectorySeparatorChar + baseFolder, subFolder); |
| 51 | + } |
| 52 | + |
| 53 | + // get clipboard text |
| 54 | + string clipBoardContents = ReadClipBoard(); |
| 55 | + if (string.IsNullOrEmpty(clipBoardContents.Trim())) |
| 56 | + { |
| 57 | + Debug.LogError("Nothing to paste.."); |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + // TODO: validate and detect string type (c# or shader) |
| 62 | + // TODO: get script class name or shader name from string |
| 63 | + |
| 64 | + // confirm overwrite dialog |
| 65 | + if (File.Exists(fullPath) == true) |
| 66 | + { |
| 67 | + // TODO: add option to autorename file (and class name) |
| 68 | + if (EditorUtility.DisplayDialog("Copy Paste Helper", "Replace existing file: " + fullPath, "Replace", "Cancel") == false) |
| 69 | + { |
| 70 | + return; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + // save to file |
| 75 | + File.WriteAllText(fullPath, clipBoardContents, System.Text.Encoding.Default); |
| 76 | + Debug.Log("<color=green>CopyPasteHelper</color> Script saved at: " + fullPath); |
| 77 | + |
| 78 | + AssetDatabase.Refresh(); |
| 79 | + |
| 80 | + // show-select created asset |
| 81 | + Object newScript = AssetDatabase.LoadAssetAtPath("Assets" + Path.DirectorySeparatorChar + baseFolder + Path.DirectorySeparatorChar + subFolder + Path.DirectorySeparatorChar + fileName, typeof(Object)); |
| 82 | + EditorGUIUtility.PingObject(newScript); |
| 83 | + } |
| 84 | + |
| 85 | + |
| 86 | + // paste text to temporary texteditor and then get text |
| 87 | + public static string ReadClipBoard() |
| 88 | + { |
| 89 | + TextEditor textEditor = new TextEditor(); |
| 90 | + textEditor.multiline = true; |
| 91 | + textEditor.Paste(); |
| 92 | + return textEditor.text; |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments