1+ using ReactUnity . Interop ;
12using System ;
23using System . Collections ;
34using UnityEngine ;
@@ -14,28 +15,17 @@ public class ReactScript
1415 public string SourceText ;
1516 public string ResourcesPath ;
1617
17- #pragma warning disable CS0414
18- [ SerializeField ]
19- [ Tooltip ( @"Editor only. Watches file for changes and refreshes the view on change.
20- Can be enabled outside the editor by adding define symbol REACT_WATCH_OUTSIDE_EDITOR to build." ) ]
21- private bool Watch = false ;
22- #pragma warning restore CS0414
23-
2418 public bool UseDevServer = true ;
2519 public string DevServer = "http://localhost:3000" ;
2620 static string DevServerFilename = "" ;
2721 public string DevServerFile => DevServer + DevServerFilename ;
2822
29- private bool SourceIsTextAsset => ScriptSource == ScriptSource . TextAsset ;
30- private bool SourceIsPath => ScriptSource != ScriptSource . TextAsset && ScriptSource != ScriptSource . Text ;
31- private bool SourceIsText => ScriptSource == ScriptSource . Text ;
32- private bool SourceIsWatchable => ScriptSource != ScriptSource . Url && ScriptSource != ScriptSource . Text ;
3323
3424 public string SourceLocation
3525 {
3626 get
3727 {
38- #if UNITY_EDITOR
28+ #if UNITY_EDITOR || REACT_DEV_SERVER_API
3929 if ( UseDevServer && ! string . IsNullOrWhiteSpace ( DevServer ) ) return DevServerFile ;
4030#endif
4131 return GetResolvedSourcePath ( ) ;
@@ -56,47 +46,28 @@ public string GetResolvedSourcePath()
5646 return path ;
5747 }
5848
59- #if UNITY_EDITOR || REACT_WATCH_OUTSIDE_EDITOR
60- IDisposable StartWatching ( Action < string , bool > callback )
61- {
62- string path = GetResolvedSourcePath ( ) ;
63- if ( string . IsNullOrWhiteSpace ( path ) ) return null ;
64-
65- return DetectChanges . WatchFileSystem ( path , x => callback ( System . IO . File . ReadAllText ( path ) , false ) ) ;
66- }
67- #endif
68-
6949 public IDisposable GetScript ( Action < string , bool > callback , bool useDevServer = true , bool disableWarnings = false )
7050 {
71- #if UNITY_EDITOR
51+ #if UNITY_EDITOR || REACT_DEV_SERVER_API
7252 if ( useDevServer && UseDevServer && ! string . IsNullOrWhiteSpace ( DevServer ) )
7353 {
7454 var request = UnityEngine . Networking . UnityWebRequest . Get ( DevServerFile ) ;
7555
76- if ( Application . isPlaying ) return new Interop . MainThreadDispatcher . CoroutineHandle (
77- Interop . MainThreadDispatcher . StartDeferred ( WatchWebRequest ( request , callback , err =>
78- {
79- Debug . LogWarning ( "DevServer seems to be unaccessible. Falling back to the original script." ) ;
80- GetScript ( callback , false ) ;
81- } , true ) ) ) ;
82- else return new Interop . EditorDispatcher . CoroutineHandle (
83- Interop . EditorDispatcher . StartDeferred ( WatchWebRequest ( request , callback , err =>
84- {
85- Debug . LogWarning ( "DevServer seems to be unaccessible. Falling back to the original script." ) ;
86- GetScript ( callback , false ) ;
87- } , true ) ) ) ;
56+ return new AdaptiveDispatcher . CoroutineHandle (
57+ AdaptiveDispatcher . StartDeferred (
58+ WatchWebRequest ( request , callback , err =>
59+ {
60+ Debug . LogWarning ( "DevServer seems to be unaccessible. Falling back to the original script." ) ;
61+ GetScript ( callback , false ) ;
62+ } , true ) ) ) ;
8863 }
8964#endif
9065
9166 switch ( ScriptSource )
9267 {
9368 case ScriptSource . TextAsset :
9469 if ( ! SourceAsset ) callback ( null , false ) ;
95- #if UNITY_EDITOR
96- else callback ( System . IO . File . ReadAllText ( UnityEditor . AssetDatabase . GetAssetPath ( SourceAsset ) ) , false ) ;
97- #else
9870 else callback ( SourceAsset . text , false ) ;
99- #endif
10071 break ;
10172 case ScriptSource . File :
10273#if UNITY_EDITOR || REACT_FILE_API
@@ -115,10 +86,8 @@ public IDisposable GetScript(Action<string, bool> callback, bool useDevServer =
11586#endif
11687 var request = UnityEngine . Networking . UnityWebRequest . Get ( SourcePath ) ;
11788
118- if ( Application . isPlaying ) return new Interop . MainThreadDispatcher . CoroutineHandle (
119- Interop . MainThreadDispatcher . StartDeferred ( WatchWebRequest ( request , callback ) ) ) ;
120- else return new Interop . EditorDispatcher . CoroutineHandle (
121- Interop . EditorDispatcher . StartDeferred ( WatchWebRequest ( request , callback ) ) ) ;
89+ return new AdaptiveDispatcher . CoroutineHandle (
90+ AdaptiveDispatcher . StartDeferred ( WatchWebRequest ( request , callback ) ) ) ;
12291#else
12392 throw new Exception ( "REACT_URL_API must be defined to use Url API outside the editor. Add REACT_URL_API to build symbols to use this feature." ) ;
12493#endif
@@ -135,13 +104,10 @@ public IDisposable GetScript(Action<string, bool> callback, bool useDevServer =
135104 break ;
136105 }
137106
138- #if UNITY_EDITOR || REACT_WATCH_OUTSIDE_EDITOR
139- if ( Watch && SourceIsWatchable ) return StartWatching ( callback ) ;
140- #endif
141107 return null ;
142108 }
143109
144- #if UNITY_EDITOR || REACT_URL_API
110+ #if UNITY_EDITOR || REACT_URL_API || REACT_DEV_SERVER_API
145111 private IEnumerator WatchWebRequest (
146112 UnityEngine . Networking . UnityWebRequest request ,
147113 Action < string , bool > callback ,
@@ -166,24 +132,4 @@ public enum ScriptSource
166132 Resource = 3 ,
167133 Text = 4 ,
168134 }
169-
170-
171- #if UNITY_EDITOR || REACT_WATCH_OUTSIDE_EDITOR
172- public class DetectChanges
173- {
174- public static IDisposable WatchFileSystem ( string path , Action < string > callback )
175- {
176- System . IO . FileSystemWatcher fileSystemWatcher = new System . IO . FileSystemWatcher ( ) ;
177-
178- fileSystemWatcher . Path = System . IO . Path . GetDirectoryName ( path ) ;
179- fileSystemWatcher . Filter = System . IO . Path . GetFileName ( path ) ;
180- fileSystemWatcher . NotifyFilter = System . IO . NotifyFilters . LastWrite | System . IO . NotifyFilters . Size ;
181-
182- fileSystemWatcher . Changed += ( x , y ) => callback ( y . FullPath ) ;
183- fileSystemWatcher . EnableRaisingEvents = true ;
184-
185- return fileSystemWatcher ;
186- }
187- }
188- #endif
189135}
0 commit comments