1+ using System ;
12using UnityEditor ;
23using UnityEngine ;
34using System . IO ;
45using UnityEngine . Assertions ;
56using System . Text ;
67using System . Collections ;
8+ using System . Linq ;
9+ using System . Text . RegularExpressions ;
710using Unity . EditorCoroutines . Editor ;
811
912namespace EG . ScriptableObjectSystem . Editor
@@ -46,7 +49,7 @@ static void OpenScriptableObjectEventCreationWindow()
4649 ScriptableObjectEventCreationWindow . OpenWindow ( ) ;
4750 }
4851
49- public static void CreateSOEventScripts ( string eventSOName , string eventListenerName , ArgInfo [ ] args )
52+ public static void CreateSOEventScripts ( string eventSOName , string eventListenerName , string eventNamespace , ArgInfo [ ] args )
5053 {
5154 //Picking the first one by default.
5255 //This will return the selected items' path, or the directory path, if no assets are selected
@@ -79,14 +82,14 @@ public static void CreateSOEventScripts(string eventSOName, string eventListener
7982
8083 var eventArgs = GenerateEventArguments ( args ) ;
8184
82- CreateSOEventScript ( creationPath , eventSOName , eventListenerName , eventArgs ) ;
83- CreateSOEventListenerScript ( creationPath , eventSOName , eventListenerName , eventArgs ) ;
85+ CreateSOEventScript ( creationPath , eventSOName , eventListenerName , eventNamespace , eventArgs ) ;
86+ CreateSOEventListenerScript ( creationPath , eventSOName , eventListenerName , eventNamespace , eventArgs ) ;
8487
8588 //Refresh the Asset Database
8689 AssetDatabase . Refresh ( ) ;
8790 }
8891
89- private static void CreateSOEventScript ( string creationPath , string eventName , string listenerName , string [ ] argTypes )
92+ private static void CreateSOEventScript ( string creationPath , string eventName , string listenerName , string eventNamespace , string [ ] argTypes )
9093 {
9194 //1-Load template asset
9295 TextAsset soEventTemplate ;
@@ -110,13 +113,18 @@ private static void CreateSOEventScript(string creationPath, string eventName, s
110113 contents = contents . Replace ( "<SCRIPT_NAME>" , eventName ) ;
111114 contents = contents . Replace ( "<SO_FILE_NAME>" , eventName ) ;
112115 contents = contents . Replace ( "<SO_MENU_NAME>" , eventName ) ;
116+
117+ contents = ReplaceNamespaceTag ( eventNamespace , contents ) ;
118+
113119 //Event order?
114120 contents = contents . Replace ( "<LISTENER_NAME>" , listenerName ) ;
115121 if ( argTypes != null ) {
116122 contents = contents . Replace ( "<ARGUMENT_LIST_DEFINITION>" , argTypes [ 0 ] ) ;
117123 contents = contents . Replace ( "<ARGUMENT_LIST>" , argTypes [ 1 ] ) ;
118124 contents = contents . Replace ( "<CUSTOM_NAMESPACE_LIST>" , argTypes [ 3 ] ) ;
119125 }
126+
127+ contents = FinalizeIndent ( contents ) ;
120128
121129 //4-Create file
122130 var filePath = creationPath + Path . DirectorySeparatorChar + eventName + ".cs" ;
@@ -131,7 +139,7 @@ private static void CreateSOEventScript(string creationPath, string eventName, s
131139 EditorCoroutineUtility . StartCoroutineOwnerless ( iconClass . AddIcon ( filePathInProject , EventIconRelativepath ) ) ;
132140 }
133141
134- private static void CreateSOEventListenerScript ( string creationPath , string eventName , string listenerName , string [ ] argTypes )
142+ private static void CreateSOEventListenerScript ( string creationPath , string eventName , string listenerName , string eventNamespace , string [ ] argTypes )
135143 {
136144 //1-Load template asset
137145 TextAsset soEventListenerTemplate ;
@@ -156,6 +164,9 @@ private static void CreateSOEventListenerScript(string creationPath, string even
156164 contents = contents . Replace ( "<SCRIPT_NAME>" , listenerName ) ;
157165 contents = contents . Replace ( "<SO_EVENT_NAME>" , eventName ) ;
158166 contents = contents . Replace ( "<SO_EVENT_FIELD_NAME>" , eventName . Replace ( "SO" , "so" ) ) ;
167+
168+ contents = ReplaceNamespaceTag ( eventNamespace , contents ) ;
169+
159170 if ( argTypes != null )
160171 {
161172 contents = contents . Replace ( "<ARGUMENT_LIST_DEFINITION>" , argTypes [ 0 ] ) ;
@@ -164,6 +175,8 @@ private static void CreateSOEventListenerScript(string creationPath, string even
164175 contents = contents . Replace ( "<CUSTOM_NAMESPACE_LIST>" , argTypes [ 3 ] ) ;
165176 }
166177
178+ contents = FinalizeIndent ( contents ) ;
179+
167180 //4-Create file
168181 var filePath = creationPath + Path . DirectorySeparatorChar + listenerName + ".cs" ;
169182 using ( var sw = new StreamWriter ( string . Format ( filePath ) ) )
@@ -177,6 +190,18 @@ private static void CreateSOEventListenerScript(string creationPath, string even
177190 EditorCoroutineUtility . StartCoroutineOwnerless ( iconClass . AddIcon ( filePathInProject , EventListenerIconRelativePath ) ) ;
178191 }
179192
193+ private static string ReplaceNamespaceTag ( string eventNamespace , string contents )
194+ {
195+ var nameSpaceOpening = string . IsNullOrWhiteSpace ( eventNamespace ) ? string . Empty : $ "namespace { eventNamespace } \n {{\n ";
196+ var nameSpaceClosing = string . IsNullOrWhiteSpace ( eventNamespace ) ? string . Empty : "}" ;
197+
198+ //Using regex so the extra line is removed without doing extra tricks or line by line checks
199+ contents = Regex . Replace ( contents , @"<NAMESPACE_DECLARATION_START>\n?\r?" , nameSpaceOpening , RegexOptions . Multiline ) ;
200+ contents = contents . Replace ( "<NAMESPACE_DECLARATION_END>" , nameSpaceClosing ) ;
201+
202+ return contents ;
203+ }
204+
180205 private static string [ ] GenerateEventArguments ( ArgInfo [ ] argsList )
181206 {
182207 var result = new string [ 4 ] ;
@@ -233,6 +258,43 @@ private static string[] GenerateEventArguments(ArgInfo[] argsList)
233258 return result ;
234259 }
235260
261+ private static string FinalizeIndent ( string rawCode , int spacesPerLevel = 4 )
262+ {
263+ var lines = rawCode . Split ( new [ ] { "\r \n " , "\r " , "\n " } , StringSplitOptions . None ) ;
264+ var sb = new StringBuilder ( ) ;
265+ var currentLevel = 0 ;
266+
267+ foreach ( string line in lines )
268+ {
269+ var trimmedLine = line . Trim ( ) ;
270+
271+ if ( string . IsNullOrEmpty ( trimmedLine ) )
272+ {
273+ sb . AppendLine ( ) ;
274+ continue ;
275+ }
276+
277+ if ( trimmedLine . StartsWith ( "}" ) )
278+ {
279+ currentLevel = Math . Max ( 0 , currentLevel - 1 ) ;
280+ }
281+
282+ var indentation = new string ( ' ' , currentLevel * spacesPerLevel ) ;
283+ sb . AppendLine ( indentation + trimmedLine ) ;
284+
285+ // if (trimmedLine.EndsWith("{") || trimmedLine.EndsWith("{ //") || trimmedLine.Contains("{"))
286+ if ( trimmedLine . EndsWith ( "{" ) )
287+ {
288+ if ( trimmedLine . Count ( f => f == '{' ) > trimmedLine . Count ( f => f == '}' ) )
289+ {
290+ currentLevel ++ ;
291+ }
292+ }
293+ }
294+
295+ return sb . ToString ( ) . Trim ( ) ;
296+ }
297+
236298 private static string GetPathInProjectAssets ( string fullPath )
237299 {
238300 const string AssetsFolder = "Assets" ;
@@ -269,7 +331,7 @@ private static string GetPathInProjectAssets(string fullPath)
269331
270332 return path ;
271333 }
272-
334+
273335 private static void SetPackageData ( )
274336 {
275337 var assembly = typeof ( AssetCreationMenu ) . Assembly ;
0 commit comments