Skip to content

Commit e9d5cd4

Browse files
author
Prashant Singh
authored
Add files via upload
1 parent 4be7de3 commit e9d5cd4

File tree

6 files changed

+471
-0
lines changed

6 files changed

+471
-0
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
using System.IO;
2+
using UnityEditor;
3+
using UnityEngine;
4+
using UnityEditorInternal;
5+
using System.Text.RegularExpressions;
6+
using UnityEditor.ProjectWindowCallback;
7+
using UnityEditor.Experimental;
8+
namespace Prashant
9+
{
10+
11+
public static class CustomMono
12+
{
13+
14+
[MenuItem("Assets/Create/Scripts/C# Script", false, 0)]
15+
public static void CreateScript()
16+
{
17+
var templatePath = Path.Combine(Application.dataPath + "/prashantsingh/CustomMono/", "MyBehaviourTemplate.cs.txt");
18+
// CreateNewCSScriptWithTemplate("NewFileName", templatePath);
19+
var endNameEditAction = ScriptableObject.CreateInstance<DoCreateAssetWithContent>();
20+
endNameEditAction.filecontent = templatePath;
21+
ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, endNameEditAction, "NewFileName", EditorGUIUtility.IconContent("cs Script Icon").image as Texture2D, null);
22+
}
23+
24+
public static void CreateScriptWithCustomBase(string _baseName)
25+
{
26+
var templatePath = Path.Combine(Application.dataPath + "/prashantsingh/CustomMono/", "MyBehaviourTemplate.cs.txt");
27+
var endNameEditAction = ScriptableObject.CreateInstance<DoCreateAssetWithContent>();
28+
endNameEditAction.filecontent = templatePath;
29+
endNameEditAction.baseClassName = _baseName;
30+
ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, endNameEditAction, "NewFileName", EditorGUIUtility.IconContent("cs Script Icon").image as Texture2D, null);
31+
}
32+
33+
public static Object CreateScriptAssetFromTemplate(string pathName, string resourceFile, string baseClassName = "")
34+
{
35+
string content = File.ReadAllText(resourceFile);
36+
37+
// #NOTRIM# is a special marker that is used to mark the end of a line where we want to leave whitespace. prevent editors auto-stripping it by accident.
38+
content = content.Replace("#NOTRIM#", "");
39+
40+
// macro replacement
41+
string baseFile = Path.GetFileNameWithoutExtension(pathName);
42+
//Set the namespace now
43+
content = content.Replace("#NAMESPACENAME#", EditorPrefs.GetString("MyNamespace", "GameName"));
44+
content = content.Replace("#NAME#", baseFile);
45+
var m_baseClassName = string.IsNullOrEmpty(baseClassName) ? "MonoBehaviour" : baseClassName;
46+
content = content.Replace("#BASECLASS#", m_baseClassName);
47+
content = InsertMethods(content);
48+
string baseFileNoSpaces = baseFile.Replace(" ", "");
49+
content = content.Replace("#SCRIPTNAME#", baseFileNoSpaces);
50+
51+
// if the script name begins with an uppercase character we support a lowercase substitution variant
52+
if (char.IsUpper(baseFileNoSpaces, 0))
53+
{
54+
baseFileNoSpaces = char.ToLower(baseFileNoSpaces[0]) + baseFileNoSpaces.Substring(1);
55+
content = content.Replace("#SCRIPTNAME_LOWER#", baseFileNoSpaces);
56+
}
57+
else
58+
{
59+
// still allow the variant, but change the first character to upper and prefix with "my"
60+
baseFileNoSpaces = "my" + char.ToUpper(baseFileNoSpaces[0]) + baseFileNoSpaces.Substring(1);
61+
content = content.Replace("#SCRIPTNAME_LOWER#", baseFileNoSpaces);
62+
}
63+
64+
return CreateScriptAssetWithContent(pathName, content);
65+
}
66+
67+
static string InsertMethods(string content)
68+
{
69+
// MethodCollection m_col = new MethodCollection();
70+
string[] m_methodsList = File.ReadAllText(Application.dataPath + "/prashantsingh/CustomMono/FinalMethodsList.txt").Split(',');
71+
string completeMethods = "";
72+
for (int count = 0; count < m_methodsList.Length; count++)
73+
{
74+
completeMethods = completeMethods +
75+
"\n\t\tvoid " + m_methodsList[count] + " ()\n" + "\t\t{" +
76+
"\n\t\t\t//do something here\n\t\t}\n";
77+
}
78+
// var completeMethods = "";
79+
content = content.Replace("#METHODS#", completeMethods);
80+
return content;
81+
}
82+
83+
public static Object CreateScriptAssetWithContent(string pathName, string templateContent)
84+
{
85+
templateContent = SetLineEndings(templateContent, EditorSettings.lineEndingsForNewScripts);
86+
87+
string fullPath = Path.GetFullPath(pathName);
88+
89+
// utf8-bom encoding was added for case 510374 in 2012. i think this was the wrong solution. BOM's are
90+
// problematic for diff tools, naive readers and writers (of which we have many!), and generally not
91+
// something most people think about. you wouldn't believe how many unity source files have BOM's embedded
92+
// in the middle of them for no reason. copy paste problem? bad tool? unity should instead have been fixed
93+
// to read all files that have no BOM as utf8 by default, and then we just strip them all, always, from
94+
// files we control. perhaps we'll do this one day and this next line can be removed. -scobi
95+
var encoding = new System.Text.UTF8Encoding(/*encoderShouldEmitUTF8Identifier:*/ true);
96+
97+
File.WriteAllText(fullPath + ".cs", templateContent, encoding);
98+
try
99+
{
100+
AssetDatabase.ImportAsset(pathName + ".cs");
101+
AssetDatabase.Refresh();
102+
}
103+
catch (System.Exception ex)
104+
{
105+
Debug.LogException(ex);
106+
}
107+
// Import the asset
108+
return AssetDatabase.LoadAssetAtPath(pathName, typeof(Object));
109+
}
110+
internal static string SetLineEndings(string content, LineEndingsMode lineEndingsMode)
111+
{
112+
const string windowsLineEndings = "\r\n";
113+
const string unixLineEndings = "\n";
114+
115+
string preferredLineEndings;
116+
117+
switch (lineEndingsMode)
118+
{
119+
case LineEndingsMode.OSNative:
120+
if (Application.platform == RuntimePlatform.WindowsEditor)
121+
preferredLineEndings = windowsLineEndings;
122+
else
123+
preferredLineEndings = unixLineEndings;
124+
break;
125+
case LineEndingsMode.Unix:
126+
preferredLineEndings = unixLineEndings;
127+
break;
128+
case LineEndingsMode.Windows:
129+
preferredLineEndings = windowsLineEndings;
130+
break;
131+
default:
132+
preferredLineEndings = unixLineEndings;
133+
break;
134+
}
135+
136+
content = Regex.Replace(content, @"\r\n?|\n", preferredLineEndings);
137+
138+
return content;
139+
}
140+
141+
}
142+
143+
class DoCreateAssetWithContent : EndNameEditAction
144+
{
145+
public string filecontent;
146+
public string baseClassName;
147+
public override void Action(int instanceId, string pathName, string resourceFile)
148+
{
149+
Object o = CustomMono.CreateScriptAssetFromTemplate(pathName, filecontent, baseClassName);
150+
ProjectWindowUtil.ShowCreatedAsset(o);
151+
}
152+
}
153+
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
using System.Collections.Generic;
2+
using System.IO;
3+
using UnityEditor;
4+
using UnityEngine;
5+
6+
namespace Prashant
7+
{
8+
public class CustomDerivedMonoEditor : EditorWindow
9+
{
10+
static GUIStyle styleHelpboxInner;
11+
static GUIStyle titleLabel, normalLabel, subtitleLabel;
12+
static List<string> m_baseClasses;
13+
14+
static string m_path = "/prashantsingh/CustomMono/baseclassnames.txt";
15+
string m_baseClassName = "";
16+
int m_index = 0;
17+
const string m_baseClassKey = "baseClassKey";
18+
static EditorWindow window;
19+
[MenuItem("Assets/Create/Scripts/Drevied C# Script", false, 0)]
20+
// [MenuItem("[Master_Tools]/CustomMono")]
21+
private static void ShowWindow()
22+
{
23+
// LoadSettings();
24+
LoadBaseClasses();
25+
window = (CustomDerivedMonoEditor)EditorWindow.GetWindow(typeof(CustomDerivedMonoEditor), true, "Mono Settings");
26+
window.minSize = new Vector2(600, 450);
27+
window.Show();
28+
}
29+
// static void LoadSettings()
30+
// {
31+
// customNameSpaceName = EditorPrefs.GetString("MyNamespace", "GameName");
32+
// }
33+
34+
static void SetupStyle()
35+
{
36+
37+
normalLabel = new GUIStyle();
38+
normalLabel.fontSize = 11;
39+
normalLabel.normal.textColor = EditorGUIUtility.isProSkin ? Color.white : Color.black;
40+
// normalLabel.normal.textColor = Color.white;
41+
normalLabel.fixedHeight = 25;
42+
normalLabel.alignment = TextAnchor.MiddleCenter;
43+
44+
styleHelpboxInner = new GUIStyle("HelpBox");
45+
styleHelpboxInner.padding = new RectOffset(6, 6, 6, 6);
46+
}
47+
48+
static void LoadBaseClasses()
49+
{
50+
var m_data = File.ReadAllText(Application.dataPath + m_path);
51+
if (!string.IsNullOrEmpty(m_data))
52+
{
53+
m_baseClasses = new List<string>(m_data.Split(','));
54+
// Debug.Log(m_baseClasses.Count + " data " + m_baseClasses[1]);
55+
m_popupArrayContents = m_baseClasses.ToArray();
56+
}
57+
else
58+
{
59+
m_baseClasses = new List<string>();
60+
SaveBaseClasses();
61+
}
62+
}
63+
64+
static void SaveBaseClasses()
65+
{
66+
var temp_data = "";
67+
for (int count = 0; count < m_baseClasses.Count; count++)
68+
{
69+
if (count > 0)
70+
temp_data = temp_data + "," + m_baseClasses[count];
71+
else
72+
temp_data = temp_data + m_baseClasses[count];
73+
}
74+
File.WriteAllText(Application.dataPath + m_path, temp_data);
75+
}
76+
77+
static string[] m_popupArrayContents;
78+
79+
private void OnGUI()
80+
{
81+
SetupStyle();
82+
GUILayout.BeginVertical(styleHelpboxInner);
83+
// EditorGUILayout.Space();
84+
85+
if (m_baseClasses.Count > 0)
86+
{
87+
GUILayout.BeginVertical(styleHelpboxInner);
88+
GUILayout.BeginHorizontal(styleHelpboxInner);
89+
m_baseClassName = EditorGUILayout.TextField("New Base Class Name", m_baseClassName, GUILayout.MinWidth(300));
90+
GUILayout.FlexibleSpace();
91+
if (GUILayout.Button("Create", GUILayout.MaxWidth(100)))
92+
{
93+
if (!m_baseClasses.Contains(m_baseClassName))
94+
{
95+
m_baseClasses.Add(m_baseClassName);
96+
m_popupArrayContents = m_baseClasses.ToArray();
97+
SaveBaseClasses();
98+
}
99+
CustomMono.CreateScriptWithCustomBase(m_baseClassName);
100+
window.Close();
101+
}
102+
GUILayout.EndHorizontal();
103+
104+
GUILayout.BeginHorizontal(styleHelpboxInner);
105+
GUILayout.Label("Select Base Class");
106+
m_index = EditorGUILayout.Popup("", m_index, m_popupArrayContents, GUILayout.Width(200));
107+
GUILayout.FlexibleSpace();
108+
if (GUILayout.Button("Create", GUILayout.MaxWidth(100)))
109+
{
110+
CustomMono.CreateScriptWithCustomBase(m_popupArrayContents[m_index]);
111+
window.Close();
112+
}
113+
GUILayout.EndHorizontal();
114+
GUILayout.EndVertical();
115+
}
116+
else
117+
{
118+
GUILayout.BeginHorizontal(styleHelpboxInner);
119+
m_baseClassName = EditorGUILayout.TextField("New Base Class Name", m_baseClassName, GUILayout.MinWidth(300));
120+
GUILayout.FlexibleSpace();
121+
if (GUILayout.Button("Create", GUILayout.MaxWidth(100)))
122+
{
123+
if (!m_baseClasses.Contains(m_baseClassName))
124+
{
125+
m_baseClasses.Add(m_baseClassName);
126+
m_popupArrayContents = m_baseClasses.ToArray();
127+
SaveBaseClasses();
128+
}
129+
CustomMono.CreateScriptWithCustomBase(m_baseClassName);
130+
window.Close();
131+
}
132+
GUILayout.EndHorizontal();
133+
}
134+
GUILayout.EndVertical();
135+
}
136+
}
137+
}

0 commit comments

Comments
 (0)