Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 48de735

Browse files
Create PasteScript.cs
1 parent 13b1f16 commit 48de735

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// new version of the https://github.com/UnityCommunity/UnityLibrary/blob/master/Assets/Scripts/Editor/Tools/CopyPasteHelper.cs
2+
// creates c# script from clipboard when click button
3+
4+
using UnityEngine;
5+
using UnityEditor;
6+
using System.IO;
7+
using System.Text.RegularExpressions;
8+
9+
namespace UnityLibrary.Tools
10+
{
11+
public class PasteScript : EditorWindow
12+
{
13+
private string statusMessage = "";
14+
private string lastCreatedScriptPath = "";
15+
16+
[MenuItem("Tools/UnityLibrary/Clipboard To C# Script")]
17+
public static void ShowWindow()
18+
{
19+
GetWindow<PasteScript>("Clipboard to Script");
20+
}
21+
22+
void OnGUI()
23+
{
24+
if (GUILayout.Button("Create Script from Clipboard"))
25+
{
26+
TryCreateScriptFromClipboard();
27+
}
28+
29+
GUILayout.Space(10);
30+
31+
// Draw clickable HelpBox
32+
EditorGUILayout.HelpBox(statusMessage, MessageType.Info);
33+
34+
Rect helpBoxRect = GUILayoutUtility.GetLastRect();
35+
if (!string.IsNullOrEmpty(lastCreatedScriptPath) && Event.current.type == EventType.MouseDown && helpBoxRect.Contains(Event.current.mousePosition))
36+
{
37+
var asset = AssetDatabase.LoadAssetAtPath<Object>(lastCreatedScriptPath);
38+
if (asset != null)
39+
{
40+
EditorGUIUtility.PingObject(asset);
41+
}
42+
Event.current.Use(); // Consume the click
43+
}
44+
}
45+
46+
void TryCreateScriptFromClipboard()
47+
{
48+
string clipboard = EditorGUIUtility.systemCopyBuffer;
49+
50+
if (IsProbablyCSharp(clipboard))
51+
{
52+
string folderPath = "Assets/Scripts/Generated";
53+
Directory.CreateDirectory(folderPath);
54+
55+
string className = GetClassName(clipboard) ?? "GeneratedScript";
56+
string path = AssetDatabase.GenerateUniqueAssetPath($"{folderPath}/{className}.cs");
57+
58+
File.WriteAllText(path, clipboard);
59+
AssetDatabase.Refresh();
60+
61+
statusMessage = $"Script created: {path}";
62+
lastCreatedScriptPath = path;
63+
}
64+
else
65+
{
66+
statusMessage = "Clipboard does not contain valid C# code.";
67+
lastCreatedScriptPath = "";
68+
}
69+
}
70+
71+
bool IsProbablyCSharp(string text)
72+
{
73+
if (string.IsNullOrWhiteSpace(text)) return false;
74+
75+
// Basic heuristic checks
76+
return Regex.IsMatch(text, @"\b(class|struct|interface|using|namespace)\b");
77+
}
78+
79+
string GetClassName(string text)
80+
{
81+
Match match = Regex.Match(text, @"\bclass\s+(\w+)");
82+
return match.Success ? match.Groups[1].Value : null;
83+
}
84+
}
85+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /