Skip to content

Commit 9596919

Browse files
committed
simplify ReactScript and optional features
1 parent 9ed01db commit 9596919

File tree

9 files changed

+82
-101
lines changed

9 files changed

+82
-101
lines changed

Editor/Drawers/ReactScriptDrawer.cs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
1212
{
1313
var x = position.x;
1414
var width = position.width;
15-
var watchWidth = 60;
1615
var source = property.FindPropertyRelative("ScriptSource");
1716
position.y += 2;
1817
position.height = 18;
@@ -21,26 +20,13 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
2120
position.y += 20;
2221
position.height = 18;
2322

24-
var watchable = IsWatchable(source);
25-
26-
if (watchable) position.width = position.width - watchWidth;
27-
2823
if ((int)ScriptSource.TextAsset == source.intValue)
2924
EditorGUI.PropertyField(position, property.FindPropertyRelative("SourceAsset"));
3025
else if ((int)ScriptSource.Text == source.intValue)
3126
EditorGUI.PropertyField(position, property.FindPropertyRelative("SourceText"));
3227
else
3328
EditorGUI.PropertyField(position, property.FindPropertyRelative("SourcePath"));
3429

35-
if (watchable)
36-
{
37-
position.x = position.width + x;
38-
position.width = watchWidth;
39-
var watch = property.FindPropertyRelative("Watch");
40-
watch.boolValue = EditorGUI.ToggleLeft(position, "Watch", watch.boolValue);
41-
}
42-
43-
4430
var useDevServer = property.FindPropertyRelative("UseDevServer");
4531
position.x = x;
4632
position.width = width;
@@ -57,11 +43,5 @@ public override float GetPropertyHeight(SerializedProperty property, GUIContent
5743
{
5844
return 64;
5945
}
60-
61-
bool IsWatchable(SerializedProperty sourceProperty)
62-
{
63-
var val = sourceProperty.intValue;
64-
return (int)ScriptSource.TextAsset == val || (int)ScriptSource.File == val || (int)ScriptSource.Resource == val;
65-
}
6646
}
6747
}

Editor/ReactUnity.Editor.asmdef

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ReactUnity.Editor",
3-
"rootNamespace": "",
3+
"rootNamespace": "ReactUnity.Editor",
44
"references": [
55
"GUID:ed4a920eac98dc342ba18a6baa202849",
66
"GUID:6055be8ebefd69e48b49212b09b47b2f",

Runtime/Core/ReactScript.cs

Lines changed: 13 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using ReactUnity.Interop;
12
using System;
23
using System.Collections;
34
using 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
}

Runtime/Core/ReactUnityRunner.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ void CreateLocation(Jint.Engine engine, ReactScript script)
137137
var location = new DomProxies.Location(script.SourceLocation, context.OnRestart);
138138
engine.SetValue("location", location);
139139

140-
#if UNITY_EDITOR
140+
#if UNITY_EDITOR || REACT_DEV_SERVER_API
141141
engine.SetValue("ctx", context);
142142
engine.SetValue("oldWebSocket", typeof(WebSocketProxy));
143143
engine.Execute(@"WebSocket = function() { return new oldWebSocket(ctx, ...arguments); }");

Runtime/Helpers/FeatureGuards.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace ReactUnity.Helpers
6+
{
7+
internal static class FeatureGuards
8+
{
9+
private static bool? vectorGraphics;
10+
11+
public static bool VectorGraphics
12+
{
13+
get
14+
{
15+
if (vectorGraphics.HasValue) return vectorGraphics.Value;
16+
#if !REACT_VECTOR_GRAPHICS
17+
Debug.LogError("To use the 'borderRadius' feature, 'Unity.VectorGraphics' package must be installed.");
18+
vectorGraphics = false;
19+
#elif UNITY_WEBGL
20+
Debug.LogError("'borderRadius' feature cannot be used in WebGL builds.");
21+
vectorGraphics = false;
22+
#else
23+
if (!Application.isPlaying)
24+
{
25+
vectorGraphics = false;
26+
Debug.LogError("'borderRadius' feature cannot be used in Editor mode.");
27+
}
28+
else
29+
{
30+
vectorGraphics = true;
31+
}
32+
#endif
33+
34+
return vectorGraphics.Value;
35+
}
36+
}
37+
}
38+
}

Runtime/Helpers/FeatureGuards.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Interop/AdaptiveDispatcher.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,20 @@ namespace ReactUnity.Interop
88
{
99
public class AdaptiveDispatcher : MonoBehaviour
1010
{
11+
public class CoroutineHandle : IDisposable
12+
{
13+
public int Handle { get; }
14+
public CoroutineHandle(int handle)
15+
{
16+
Handle = handle;
17+
}
18+
19+
public void Dispose()
20+
{
21+
StopDeferred(Handle);
22+
}
23+
}
24+
1125
private static bool Playing;
1226

1327
public static void Initialize()

Runtime/ReactUnity.asmdef

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ReactUnity",
3-
"rootNamespace": "",
3+
"rootNamespace": "ReactUnity",
44
"references": [
55
"GUID:6055be8ebefd69e48b49212b09b47b2f",
66
"GUID:68550284b645f4b9894995579f34290a",

Runtime/Styling/BorderGraphic.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
1+
using ReactUnity.Helpers;
12
using System.Collections.Generic;
3+
using UnityEngine;
24
#if !(!REACT_VECTOR_GRAPHICS || UNITY_WEBGL)
35
using Unity.VectorGraphics;
46
#endif
5-
using UnityEngine;
67

78
namespace ReactUnity.Styling
89
{
910
public static class BorderGraphic
1011
{
1112
public static Dictionary<int, Sprite> SpriteCache = new Dictionary<int, Sprite>();
1213

13-
#if !REACT_VECTOR_GRAPHICS || UNITY_WEBGL
14-
private static bool ShowVectorGraphicsMessage = true;
15-
#endif
16-
1714
static public Sprite CreateBorderSprite(int borderRadius)
1815
{
19-
if (!Application.isPlaying) return null;
2016
borderRadius = Mathf.Max(borderRadius, 0);
2117
if (SpriteCache.ContainsKey(borderRadius)) return SpriteCache[borderRadius];
2218

@@ -31,13 +27,9 @@ static public Sprite CreateBorderSprite(int borderRadius)
3127
Sprite.Create(smallTexture, new Rect(0, 0, 4, 4), Vector2.one / 2, 1, 0, SpriteMeshType.FullRect, Vector4.one);
3228
}
3329

30+
if (!FeatureGuards.VectorGraphics) return null;
3431

3532
#if !REACT_VECTOR_GRAPHICS || UNITY_WEBGL
36-
if (ShowVectorGraphicsMessage)
37-
{
38-
Debug.LogError("To use the 'borderRadius' feeature, 'Unity.VectorGraphics' package must be installed.");
39-
ShowVectorGraphicsMessage = false;
40-
}
4133
return null;
4234
#else
4335
var svg = new Scene() { Root = new SceneNode() { Shapes = new List<Shape>() } };

0 commit comments

Comments
 (0)