99// --------------------------------------------------------------------------------------------------------------------
1010
1111using System ;
12+ using System . Collections ;
1213using System . Collections . Generic ;
14+ using System . Linq ;
15+ using System . Text ;
1316using Supyrb . Attributes ;
1417using UnityEngine ;
1518using UnityEngine . Rendering ;
@@ -21,6 +24,11 @@ namespace Supyrb
2124 /// </summary>
2225 public class CommonCommands : WebCommands
2326 {
27+ /// <summary>
28+ /// List that stores allocated byte arrays to prevent them from being garbage collected
29+ /// </summary>
30+ private List < byte [ ] > byteArrayMemory = new List < byte [ ] > ( ) ;
31+
2432 /// <summary>
2533 /// Disable capturing all keyboard input, e.g. for using native html input fields
2634 /// Browser Usage: <code>unityGame.SendMessage("WebGL","DisableCaptureAllKeyboardInput");</code>
@@ -58,6 +66,41 @@ public void LogMemory()
5866 WebToolPlugins . LogMemory ( ) ;
5967 }
6068
69+ /// <summary>
70+ /// Allocate memory to test memory usage and limits
71+ /// The memory will be stored in a list to prevent it from being garbage collected
72+ /// </summary>
73+ /// <param name="mb">MB to allocate</param>
74+ [ WebCommand ( Description = "Allocate memory to test memory usage and limits" ) ]
75+ public void AllocateByteArrayMemory ( int mb )
76+ {
77+ byte [ ] memory = new byte [ mb * 1024 * 1024 ] ;
78+ byteArrayMemory . Add ( memory ) ;
79+ Debug . Log ( $ "Allocated { mb } MB of memory, total memory usage: { WebToolPlugins . GetTotalMemorySize ( ) : 0.00} MB") ;
80+ }
81+
82+ /// <summary>
83+ /// Release all allocated byte array memory
84+ /// </summary>
85+ [ WebCommand ( Description = "Release all allocated byte array memory" ) ]
86+ [ ContextMenu ( nameof ( ReleaseByteArrayMemory ) ) ]
87+ public void ReleaseByteArrayMemory ( )
88+ {
89+ byteArrayMemory . Clear ( ) ;
90+ Debug . Log ( "Released all allocated byte array memory, it can now be garbage collected" ) ;
91+ }
92+
93+ /// <summary>
94+ /// Trigger garbage collection
95+ /// </summary>
96+ [ WebCommand ( Description = "Trigger garbage collection" ) ]
97+ [ ContextMenu ( nameof ( TriggerGarbageCollection ) ) ]
98+ public void TriggerGarbageCollection ( )
99+ {
100+ GC . Collect ( ) ;
101+ Debug . Log ( "Garbage collection triggered" ) ;
102+ }
103+
61104 /// <summary>
62105 /// Unloads all unused assets <see cref="Resources.UnloadUnusedAssets"/>
63106 /// Browser Usage: <code>unityGame.SendMessage("WebGL","UnloadUnusedAssets");</code>
@@ -67,6 +110,7 @@ public void LogMemory()
67110 public void UnloadUnusedAssets ( )
68111 {
69112 Resources . UnloadUnusedAssets ( ) ;
113+ Debug . Log ( "Unloaded unused assets" ) ;
70114 }
71115
72116 /// <summary>
@@ -79,6 +123,7 @@ public void UnloadUnusedAssets()
79123 public void SetApplicationRunInBackground ( int runInBackground )
80124 {
81125 Application . runInBackground = runInBackground == 1 ;
126+ Debug . Log ( $ "Application.runInBackground: { Application . runInBackground } ") ;
82127 }
83128
84129 /// <summary>
@@ -90,6 +135,7 @@ public void SetApplicationRunInBackground(int runInBackground)
90135 public void SetApplicationTargetFrameRate ( int targetFrameRate )
91136 {
92137 Application . targetFrameRate = targetFrameRate ;
138+ Debug . Log ( $ "Application.targetFrameRate: { Application . targetFrameRate } ") ;
93139 }
94140
95141 /// <summary>
@@ -101,6 +147,7 @@ public void SetApplicationTargetFrameRate(int targetFrameRate)
101147 public void SetTimeFixedDeltaTime ( float fixedDeltaTime )
102148 {
103149 Time . fixedDeltaTime = fixedDeltaTime ;
150+ Debug . Log ( $ "Time.fixedDeltaTime: { Time . fixedDeltaTime } ") ;
104151 }
105152
106153 /// <summary>
@@ -113,6 +160,48 @@ public void SetTimeFixedDeltaTime(float fixedDeltaTime)
113160 public void SetTimeTimeScale ( float timeScale )
114161 {
115162 Time . timeScale = timeScale ;
163+ Debug . Log ( $ "Time.timeScale: { Time . timeScale } ") ;
164+ }
165+
166+ /// <summary>
167+ /// Finds GameObject(s) by name and logs the found GameObject(s) and their components
168+ /// </summary>
169+ /// <param name="name">The name of the GameObject to find</param>
170+ [ WebCommand ( Description = "Find GameObject by name and log its components" ) ]
171+ [ ContextMenu ( nameof ( FindGameObjectByName ) ) ]
172+ public void FindGameObjectByName ( string name )
173+ {
174+ var gameObjects = GameObject . FindObjectsOfType < GameObject > ( ) . Where ( go => go . name == name ) . ToArray ( ) ;
175+ if ( gameObjects . Length == 0 )
176+ {
177+ Debug . Log ( $ "No GameObject found with the name: { name } ") ;
178+ }
179+ else
180+ {
181+ StringBuilder sb = new StringBuilder ( ) ;
182+ foreach ( var go in gameObjects )
183+ {
184+ int pathStartIndex = 0 ;
185+ var currentTransform = go . transform ;
186+ sb . Insert ( pathStartIndex , currentTransform . name ) ;
187+ currentTransform = currentTransform . parent ;
188+ while ( currentTransform != null )
189+ {
190+ sb . Insert ( pathStartIndex , currentTransform . name + "/" ) ;
191+ currentTransform = currentTransform . parent ;
192+ }
193+
194+ sb . AppendLine ( $ ", Tag: { go . tag } , Layer: { go . layer } , ActiveSelf: { go . activeSelf } , ActiveInHierarchy: { go . activeInHierarchy } ") ;
195+ sb . AppendLine ( "Attached Components:" ) ;
196+ var components = go . GetComponents < Component > ( ) ;
197+ foreach ( var component in components )
198+ {
199+ sb . AppendLine ( $ "- { component . GetType ( ) . Name } ") ;
200+ }
201+ Debug . Log ( sb . ToString ( ) ) ;
202+ sb . Clear ( ) ;
203+ }
204+ }
116205 }
117206
118207 /// <summary>
@@ -214,6 +303,7 @@ public void LogTextureSupport()
214303 public void DeleteAllPlayerPrefs ( )
215304 {
216305 PlayerPrefs . DeleteAll ( ) ;
306+ Debug . Log ( "Deleted all player prefs" ) ;
217307 }
218308
219309 /// <summary>
@@ -226,6 +316,86 @@ public void DeleteAllPlayerPrefs()
226316 public void LogShaderCompilation ( int enabled )
227317 {
228318 GraphicsSettings . logWhenShaderIsCompiled = enabled == 1 ;
319+ Debug . Log ( $ "GraphicsSettings.logWhenShaderIsCompiled: { GraphicsSettings . logWhenShaderIsCompiled } ") ;
320+ }
321+
322+ /// <summary>
323+ /// Copy text to clipboard using the browser's clipboard API
324+ /// </summary>
325+ /// <param name="text">Text to copy to clipboard</param>
326+ [ WebCommand ( Description = "Copy text to clipboard" ) ]
327+ public void CopyToClipboard ( string text )
328+ {
329+ WebToolPlugins . CopyToClipboard ( text ) ;
330+ }
331+
332+ /// <summary>
333+ /// Check if the browser has an internet connection
334+ /// </summary>
335+ [ WebCommand ( Description = "Check if browser is online" ) ]
336+ public void CheckOnlineStatus ( )
337+ {
338+ bool isOnline = WebToolPlugins . IsOnline ( ) ;
339+ Debug . Log ( $ "<color=#4D65A4>Online Status:</color> { ( isOnline ? "<color=#3bb508>Connected</color>" : "<color=#b50808>Disconnected</color>" ) } ") ;
340+ }
341+
342+ /// <summary>
343+ /// Captures the current screen and saves it as a PNG file.
344+ /// </summary>
345+ [ WebCommand ( Description = "Save current screen as PNG" ) ]
346+ public void SaveScreenshot ( )
347+ {
348+ SaveScreenshotSuperSize ( 1 ) ;
349+ }
350+
351+ /// <summary>
352+ /// Captures the current screen and saves it as a PNG file.
353+ /// </summary>
354+ /// <param name="superSize">1 for normal size, 2 for double size, 4 for quadruple size</param>
355+ [ WebCommand ( Description = "Save current screen as PNG with variable super size" ) ]
356+ public void SaveScreenshotSuperSize ( int superSize )
357+ {
358+ StartCoroutine ( CaptureScreenshot ( superSize ) ) ;
359+ }
360+
361+ private IEnumerator CaptureScreenshot ( int superSize )
362+ {
363+ // Wait for the end of frame to ensure everything is rendered
364+ yield return new WaitForEndOfFrame ( ) ;
365+
366+ string filename = "screenshot.png" ;
367+ try
368+ {
369+ // Capture the screen
370+ Texture2D screenshot = ScreenCapture . CaptureScreenshotAsTexture ( superSize ) ;
371+
372+ try
373+ {
374+ // Convert to PNG
375+ byte [ ] pngData = screenshot . EncodeToPNG ( ) ;
376+
377+ // Download through browser
378+ WebToolPlugins . DownloadBinaryFile ( filename , pngData , "image/png" ) ;
379+
380+ Debug . Log ( $ "Screenshot saved as { filename } ({ screenshot . width } x{ screenshot . height } ) with size { pngData . Length } bytes") ;
381+ }
382+ finally
383+ {
384+ // Clean up the texture
385+ if ( Application . isPlaying )
386+ {
387+ Destroy ( screenshot ) ;
388+ }
389+ else
390+ {
391+ DestroyImmediate ( screenshot ) ;
392+ }
393+ }
394+ }
395+ catch ( System . Exception e )
396+ {
397+ Debug . LogError ( $ "Failed to save screenshot: { e . Message } ") ;
398+ }
229399 }
230400 }
231401}
0 commit comments