1+ using System ;
2+ using System . IO ;
3+ using System . Linq ;
4+ using UnityEditor ;
5+ using UnityEngine ;
6+
7+ namespace RaCoding . GameEvents
8+ {
9+ public class VariableWindow : EditorWindow
10+ {
11+ private static readonly string TAB = " " ;
12+ private static readonly string PACKAGE_NAMESPACE = "RaCoding.Variables" ;
13+ private static readonly string IMMUTABLEVARIABLE_CLASS_SUFFIX = "ImmutableVariable" ;
14+ private static readonly string MUTABLEVARIABLE_CLASS_SUFFIX = "MutableVariable" ;
15+ private static readonly string MUTABLEVARIABLEREFERENCE_CLASS_SUFFIX = "MutableVariableReference" ;
16+ private static readonly string VARIABLEEDITOR_CLASS_SUFFIX = "VariableEditor" ;
17+
18+ private string _newNamespace = "RaCoding.Variables" ;
19+ private string _newType = "Sprite" ;
20+ private string _newTypeNamespace = "UnityEngine" ;
21+
22+ private bool _groupEnabled ;
23+ private bool _overrideExistingFiles = false ;
24+
25+ [ MenuItem ( "Window/RaCoding/SO Variable Creator" ) ]
26+ static void Init ( )
27+ {
28+ // Get existing open window or if none, make a new one:
29+ var window = ( VariableWindow ) EditorWindow . GetWindow ( typeof ( VariableWindow ) ) ;
30+ window . Show ( ) ;
31+ }
32+
33+ void OnGUI ( )
34+ {
35+ GUILayout . Label ( "Base Settings" , EditorStyles . boldLabel ) ;
36+ _newNamespace = EditorGUILayout . TextField ( "Namespace" , _newNamespace ) ;
37+ _newType = EditorGUILayout . TextField ( "Type" , _newType ) ;
38+ _newTypeNamespace = EditorGUILayout . TextField ( "Type Namespace" , _newTypeNamespace ) ;
39+
40+ _groupEnabled = EditorGUILayout . BeginToggleGroup ( "Optional Settings" , _groupEnabled ) ;
41+ _overrideExistingFiles = EditorGUILayout . Toggle ( "Override existing files" , _overrideExistingFiles ) ;
42+ EditorGUILayout . EndToggleGroup ( ) ;
43+
44+ GUILayout . Space ( 10 ) ;
45+
46+ if ( GUILayout . Button ( "Generate" ) )
47+ {
48+ GenerateScripts ( ) ;
49+ }
50+ }
51+
52+ private string GenerateClassNames ( string suffix )
53+ {
54+ if ( string . IsNullOrEmpty ( _newType ) )
55+ {
56+ throw new ArgumentException ( "Type can not be null or empty!" ) ;
57+ }
58+ return _newType . First ( ) . ToString ( ) . ToUpper ( ) + _newType [ 1 ..] + suffix ;
59+ }
60+
61+ private string GenerateVariableNamesPrefix ( )
62+ {
63+ if ( string . IsNullOrEmpty ( _newType ) )
64+ {
65+ throw new ArgumentException ( "Type can not be null or empty!" ) ;
66+ }
67+ return _newType . First ( ) . ToString ( ) . ToLower ( ) + _newType [ 1 ..] ;
68+ }
69+
70+ private string GenerateClassPath ( string className )
71+ {
72+ return "Assets/" + className + ".cs" ;
73+ }
74+
75+ private void GenerateScripts ( )
76+ {
77+ Debug . Log ( "Generating scripts..." ) ;
78+
79+ // class names
80+ string immutableVariableClassName = GenerateClassNames ( IMMUTABLEVARIABLE_CLASS_SUFFIX ) ;
81+ string mutableVariableClassName = GenerateClassNames ( MUTABLEVARIABLE_CLASS_SUFFIX ) ;
82+ string mutableVariableReferenceClassName = GenerateClassNames ( MUTABLEVARIABLEREFERENCE_CLASS_SUFFIX ) ;
83+ string variableEditorClassName = GenerateClassNames ( VARIABLEEDITOR_CLASS_SUFFIX ) ;
84+
85+ // class paths
86+ string immutableVariableClassPath = GenerateClassPath ( immutableVariableClassName ) ;
87+ string mutableVariableClassPath = GenerateClassPath ( mutableVariableClassName ) ;
88+ string mutableVariableReferenceClassPath = GenerateClassPath ( mutableVariableReferenceClassName ) ;
89+ string variableEditorClassPath = GenerateClassPath ( variableEditorClassName ) ;
90+
91+ if ( CheckFilesGeneration ( new string [ ] { immutableVariableClassPath , mutableVariableClassPath ,
92+ mutableVariableReferenceClassPath , variableEditorClassPath } ) )
93+ {
94+ GenerateImmutableVariable ( immutableVariableClassName , immutableVariableClassPath ) ;
95+ GenerateMutableVariable ( mutableVariableClassName , mutableVariableClassPath ) ;
96+ GenerateMutableVariableReference ( mutableVariableReferenceClassName , GenerateVariableNamesPrefix ( ) , mutableVariableClassName , mutableVariableReferenceClassPath ) ;
97+ GenerateVariableEditor ( variableEditorClassName , mutableVariableClassName , variableEditorClassPath ) ;
98+ }
99+ else
100+ {
101+ Debug . LogError ( "Could not generate files because they already exist!" ) ;
102+ }
103+
104+ AssetDatabase . Refresh ( ) ;
105+ }
106+
107+ // returns true if files can be generated
108+ private bool CheckFilesGeneration ( string [ ] classPath )
109+ {
110+ bool doesFileExist ;
111+ for ( int i = 0 ; i < classPath . Length ; i ++ )
112+ {
113+ doesFileExist = File . Exists ( classPath [ i ] ) ;
114+ if ( doesFileExist == true && _overrideExistingFiles == false )
115+ {
116+ return false ;
117+ }
118+ }
119+ return true ;
120+ }
121+
122+ private void GenerateImmutableVariable ( string className , string classPath )
123+ {
124+ Debug . Log ( "Writing ImmutableVariable classfile..." ) ;
125+
126+ using ( StreamWriter outfile = new ( classPath ) )
127+ {
128+ outfile . WriteLine ( "using UnityEngine;" ) ;
129+ if ( string . IsNullOrEmpty ( _newTypeNamespace ) == false
130+ && _newTypeNamespace != "UnityEngine" )
131+ {
132+ outfile . WriteLine ( "using " + _newTypeNamespace + ";" ) ;
133+ }
134+ if ( _newNamespace != PACKAGE_NAMESPACE )
135+ {
136+ outfile . WriteLine ( "using RaCoding.Variables;" ) ;
137+ }
138+ outfile . WriteLine ( "" ) ;
139+ outfile . WriteLine ( "namespace " + _newNamespace ) ;
140+ outfile . WriteLine ( "{" ) ;
141+ outfile . WriteLine ( TAB + "[CreateAssetMenu(fileName = \" " + className + "\" ," +
142+ " menuName = \" RaCoding/Variables/Immutable/Create new immutable " + _newType + " variable\" )]" ) ;
143+ outfile . WriteLine ( TAB + "public class " + className + " : ImmutableVariable<" + _newType + "> {}" ) ;
144+ outfile . WriteLine ( "}" ) ;
145+ }
146+ }
147+
148+ private void GenerateMutableVariable ( string className , string classPath )
149+ {
150+ Debug . Log ( "Writing MutableVariable classfile..." ) ;
151+
152+ using ( StreamWriter outfile = new ( classPath ) )
153+ {
154+ outfile . WriteLine ( "using UnityEngine;" ) ;
155+ if ( string . IsNullOrEmpty ( _newTypeNamespace ) == false
156+ && _newTypeNamespace != "UnityEngine" )
157+ {
158+ outfile . WriteLine ( "using " + _newTypeNamespace + ";" ) ;
159+ }
160+ if ( _newNamespace != PACKAGE_NAMESPACE )
161+ {
162+ outfile . WriteLine ( "using RaCoding.Variables;" ) ;
163+ }
164+ outfile . WriteLine ( "" ) ;
165+ outfile . WriteLine ( "namespace " + _newNamespace ) ;
166+ outfile . WriteLine ( "{" ) ;
167+ outfile . WriteLine ( TAB + "[CreateAssetMenu(fileName = \" " + className + "\" ," +
168+ " menuName = \" RaCoding/Variables/Mutable/Create new mutable " + _newType + " variable\" )]" ) ;
169+ outfile . WriteLine ( TAB + "public class " + className + " : MutableVariable<" + _newType + "> {}" ) ;
170+ outfile . WriteLine ( "}" ) ;
171+ }
172+ }
173+
174+ private void GenerateMutableVariableReference ( string className , string variableNamePrefix ,
175+ string mutableVariableClassName , string classPath )
176+ {
177+ Debug . Log ( "Writing MutableVariableReference classfile..." ) ;
178+
179+ using ( StreamWriter outfile = new ( classPath ) )
180+ {
181+ outfile . WriteLine ( "using UnityEngine;" ) ;
182+ if ( string . IsNullOrEmpty ( _newTypeNamespace ) == false
183+ && _newTypeNamespace != "UnityEngine" )
184+ {
185+ outfile . WriteLine ( "using " + _newTypeNamespace + ";" ) ;
186+ }
187+ if ( _newNamespace != PACKAGE_NAMESPACE )
188+ {
189+ outfile . WriteLine ( "using RaCoding.Variables;" ) ;
190+ }
191+ outfile . WriteLine ( "" ) ;
192+ outfile . WriteLine ( "namespace " + _newNamespace ) ;
193+ outfile . WriteLine ( "{" ) ;
194+ outfile . WriteLine ( TAB + "[System.Serializable]" ) ;
195+ outfile . WriteLine ( TAB + "public class " + className + " : MutableVariableReference<" + _newType + ">" ) ;
196+ outfile . WriteLine ( TAB + "{" ) ;
197+ outfile . WriteLine ( TAB + TAB + "[SerializeField] private " + mutableVariableClassName + " " + variableNamePrefix + "MutableVariable;" ) ;
198+ outfile . WriteLine ( "" ) ;
199+ outfile . WriteLine ( TAB + TAB + "protected override MutableVariable<" + _newType + "> Reference => " + variableNamePrefix + "MutableVariable; " ) ;
200+ outfile . WriteLine ( TAB + "}" ) ;
201+ outfile . WriteLine ( "}" ) ;
202+ }
203+ }
204+ private void GenerateVariableEditor ( string className , string mutableVariableClassName ,
205+ string classPath )
206+ {
207+ Debug . Log ( "Writing VariableEditor classfile..." ) ;
208+
209+ using ( StreamWriter outfile = new ( classPath ) )
210+ {
211+ outfile . WriteLine ( "using UnityEditor;" ) ;
212+ outfile . WriteLine ( "using UnityEngine;" ) ;
213+ if ( string . IsNullOrEmpty ( _newTypeNamespace ) == false
214+ && _newTypeNamespace != "UnityEngine" )
215+ {
216+ outfile . WriteLine ( "using " + _newTypeNamespace + ";" ) ;
217+ }
218+ if ( _newNamespace != PACKAGE_NAMESPACE )
219+ {
220+ outfile . WriteLine ( "using RaCoding.Variables;" ) ;
221+ }
222+ outfile . WriteLine ( "" ) ;
223+ outfile . WriteLine ( "namespace " + _newNamespace ) ;
224+ outfile . WriteLine ( "{" ) ;
225+ outfile . WriteLine ( TAB + "[CustomEditor(typeof(" + mutableVariableClassName + "), editorForChildClasses: true)]" ) ;
226+ outfile . WriteLine ( TAB + "public class " + className + " : VariableEditor<" + _newType + ">" ) ;
227+ outfile . WriteLine ( TAB + "{" ) ;
228+ outfile . WriteLine ( TAB + TAB + "protected override void AssignResetValue()" ) ;
229+ outfile . WriteLine ( TAB + TAB + "{" ) ;
230+ outfile . WriteLine ( TAB + TAB + TAB + "resetValue.objectReferenceValue = value.objectReferenceValue;" ) ;
231+ outfile . WriteLine ( TAB + TAB + "}" ) ;
232+ outfile . WriteLine ( TAB + "}" ) ;
233+ outfile . WriteLine ( "}" ) ;
234+ }
235+ }
236+ }
237+ }
0 commit comments