Skip to content

Commit 154a3e2

Browse files
committed
removed dependency to uniRx
made vectorgraphics optional dependency
1 parent 014c3ca commit 154a3e2

File tree

11 files changed

+264
-79
lines changed

11 files changed

+264
-79
lines changed

Editor/EditStyleWindow.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,13 @@ void DrawLayout()
150150
{
151151
var prop = EditorGUILayout.EnumPopup("Flex Direction", CurrentLayout.FlexDirection);
152152
CurrentLayout.FlexDirection = (YogaFlexDirection)prop;
153+
154+
155+
DrawFloatRowWithNaN(CurrentLayout.BorderWidth, 0, (enabled, appropriateValue) =>
156+
{
157+
var prop2 = EditorGUILayout.IntField("Border Width", (int)appropriateValue);
158+
CurrentLayout.BorderWidth = enabled ? prop2 : float.NaN;
159+
});
153160
}
154161

155162

@@ -162,6 +169,7 @@ void ApplyStyles()
162169
flex.Node.CopyStyle(CurrentLayout);
163170
flex.Component.Context.scheduleLayout();
164171
flex.Component.ResolveStyle();
172+
flex.Component.ApplyLayoutStyles();
165173
}
166174

167175
bool Toggle(bool value)
@@ -181,6 +189,21 @@ void DrawNullableRow(bool value, Action<bool> draw)
181189
GUI.enabled = true;
182190
}
183191

192+
193+
194+
void DrawFloatRowWithNaN(float value, float defaultValue, Action<bool, float> draw)
195+
{
196+
var isNan = float.IsNaN(value);
197+
GUILayout.BeginHorizontal();
198+
var enabled = Toggle(!isNan);
199+
GUI.enabled = enabled;
200+
201+
draw(enabled, isNan ? defaultValue : value);
202+
203+
GUILayout.EndHorizontal();
204+
GUI.enabled = true;
205+
}
206+
184207
void CopyStyle()
185208
{
186209
var str = new StringBuilder();

Runtime/Components/ContainerComponent.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using ReactUnity.Styling;
22
using System.Collections.Generic;
33
using System.Linq;
4-
using UniRx;
4+
using ReactUnity.Interop;
55
using UnityEngine;
66
using UnityEngine.UI;
77

@@ -68,7 +68,7 @@ public virtual void SetOpacity(float v)
6868
var group = CanvasGroup;
6969
if (!group && v == 1) return;
7070

71-
if(!group) group = GameObject.AddComponent<CanvasGroup>();
71+
if (!group) group = GameObject.AddComponent<CanvasGroup>();
7272
group.alpha = v;
7373
}
7474

@@ -86,8 +86,9 @@ protected virtual void SetBorderRadius(int radius)
8686

8787
var image = GetBackgroundGraphic();
8888

89-
Observable.EveryLateUpdate().First().TakeUntilDestroy(GameObject).Subscribe((x) =>
89+
MainThreadDispatcher.OnUpdate(() =>
9090
{
91+
if (!GameObject) return;
9192
var sprite = BorderGraphic.CreateBorderSprite(radius);
9293
image.SetBorderImage(sprite);
9394
});

Runtime/Contexts/UnityUGUIContext.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
using System.Collections.Generic;
22
using UnityEngine;
3-
using UniRx;
43
using UnityEngine.EventSystems;
54
using Jint.Native;
65
using Jint;
76
using ReactUnity.Components;
8-
using ReactUnity.Converters;
97
using ReactUnity.Types;
108
using Facebook.Yoga;
119
using Jint.Native.Function;
10+
using ReactUnity.Interop;
1211

1312
namespace ReactUnity
1413
{
@@ -50,7 +49,7 @@ public UnityUGUIContext(RectTransform hostElement, Engine engine, StringObjectDi
5049
RootLayoutNode = Host.Layout;
5150

5251
// TODO: text sizes are not calculated right on the first frame they are added
53-
Observable.EveryLateUpdate().Subscribe((x) =>
52+
MainThreadDispatcher.AddCallOnLateUpdate(() =>
5453
{
5554
if (Scheduled)
5655
{
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using UnityEngine;
5+
6+
namespace ReactUnity.Interop
7+
{
8+
public class MainThreadDispatcher : MonoBehaviour
9+
{
10+
public class CoroutineForwardRef
11+
{
12+
internal IEnumerator Enumerator;
13+
internal Coroutine Coroutine;
14+
15+
internal CoroutineForwardRef(IEnumerator ie)
16+
{
17+
Enumerator = ie;
18+
}
19+
}
20+
21+
#region Singleton stuff
22+
private static MainThreadDispatcher Instance { get; set; }
23+
24+
void OnEnable()
25+
{
26+
if (Instance && Instance != this) DestroyImmediate(Instance);
27+
Instance = this;
28+
}
29+
30+
private void OnDestroy()
31+
{
32+
if (Instance == this) Instance = null;
33+
}
34+
35+
public static void Initialize()
36+
{
37+
if (Instance) return;
38+
var go = new GameObject("React Unity Main Thread Dispatcher");
39+
var dispatcher = go.AddComponent<MainThreadDispatcher>();
40+
DontDestroyOnLoad(go);
41+
Instance = dispatcher;
42+
}
43+
#endregion
44+
45+
46+
private static List<CoroutineForwardRef> ToStart = new List<CoroutineForwardRef>();
47+
private static List<CoroutineForwardRef> ToStop = new List<CoroutineForwardRef>();
48+
private static List<Action> CallOnLateUpdate = new List<Action>();
49+
50+
static public void AddCallOnLateUpdate(Action call)
51+
{
52+
CallOnLateUpdate.Add(call);
53+
}
54+
55+
static public CoroutineForwardRef OnUpdate(Action callback)
56+
{
57+
return StartDeferred(OnUpdateCoroutine(callback));
58+
}
59+
60+
static public CoroutineForwardRef Timeout(Action callback, float timeSeconds)
61+
{
62+
return StartDeferred(TimeoutCoroutine(callback, timeSeconds));
63+
}
64+
65+
static public CoroutineForwardRef Interval(Action callback, float intervalSeconds)
66+
{
67+
return StartDeferred(IntervalCoroutine(callback, intervalSeconds));
68+
}
69+
70+
static private CoroutineForwardRef StartDeferred(IEnumerator cr)
71+
{
72+
var handle = new CoroutineForwardRef(cr);
73+
ToStart.Add(handle);
74+
return handle;
75+
}
76+
77+
static public void StopDeferred(CoroutineForwardRef cr)
78+
{
79+
ToStop.Add(cr);
80+
}
81+
82+
void StartAndStopDeferreds()
83+
{
84+
for (int i = 0; i < ToStart.Count; i++)
85+
{
86+
var cr = ToStart[i];
87+
cr.Coroutine = StartCoroutine(cr.Enumerator);
88+
}
89+
ToStart.Clear();
90+
91+
for (int i = 0; i < ToStop.Count; i++)
92+
{
93+
var cr = ToStop[i];
94+
if (cr.Coroutine != null) StopCoroutine(cr.Coroutine);
95+
}
96+
ToStop.Clear();
97+
}
98+
99+
void Update()
100+
{
101+
StartAndStopDeferreds();
102+
}
103+
104+
void LateUpdate()
105+
{
106+
StartAndStopDeferreds();
107+
108+
var count = CallOnLateUpdate.Count;
109+
for (int i = 0; i < count; i++)
110+
CallOnLateUpdate[i].Invoke();
111+
}
112+
113+
114+
private static IEnumerator OnUpdateCoroutine(Action callback)
115+
{
116+
yield return null;
117+
callback();
118+
}
119+
120+
private static IEnumerator TimeoutCoroutine(Action callback, float time)
121+
{
122+
yield return new WaitForSeconds(time);
123+
callback();
124+
}
125+
126+
private static IEnumerator IntervalCoroutine(Action callback, float interval)
127+
{
128+
while (true)
129+
{
130+
yield return new WaitForSeconds(interval);
131+
callback();
132+
}
133+
}
134+
135+
}
136+
}

Runtime/Interop/MainThreadDispatcher.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/ReactScript.cs

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
3-
using UniRx;
42
using UnityEngine;
53

64
namespace ReactUnity
@@ -26,7 +24,7 @@ public class ReactScript
2624

2725

2826
#if UNITY_EDITOR || REACT_WATCH_OUTSIDE_EDITOR
29-
IObservable<string> StartWatching()
27+
IDisposable StartWatching(Action<string> callback)
3028
{
3129
string path = "";
3230

@@ -39,50 +37,49 @@ IObservable<string> StartWatching()
3937
path = UnityEditor.AssetDatabase.GetAssetPath(Resources.Load(SourcePath));
4038
#endif
4139

42-
if (string.IsNullOrWhiteSpace(path)) return Observable.Empty<string>();
40+
if (string.IsNullOrWhiteSpace(path)) return null;
4341

44-
return DetectChanges.WatchFileSystem(path)
45-
.Throttle(TimeSpan.FromMilliseconds(500))
46-
.Select(x => System.IO.File.ReadAllText(path));
42+
return DetectChanges.WatchFileSystem(path, x => callback(System.IO.File.ReadAllText(path)));
4743
}
4844
#endif
4945

50-
public IObservable<string> GetScript()
46+
public IDisposable GetScript(Action<string> changeCallback, out string result)
5147
{
52-
var res = new List<IObservable<string>>();
53-
54-
IObservable<string> obs = null;
55-
5648
switch (ScriptSource)
5749
{
5850
case ScriptSource.TextAsset:
59-
if (SourceAsset) obs = Observable.Return(SourceAsset.text);
51+
if (!SourceAsset) result = null;
52+
#if UNITY_EDITOR
53+
else result = System.IO.File.ReadAllText(UnityEditor.AssetDatabase.GetAssetPath(SourceAsset));
54+
#else
55+
else result = SourceAsset.text;
56+
#endif
6057
break;
6158
case ScriptSource.File:
62-
obs = Observable.Return(System.IO.File.ReadAllText(SourcePath));
59+
result = System.IO.File.ReadAllText(SourcePath);
6360
break;
6461
case ScriptSource.Url:
65-
#pragma warning disable CS0618
66-
obs = ObservableWWW.Get(SourcePath);
67-
#pragma warning restore CS0618
62+
result = null;
63+
// TODO: Maybe we don't need url
64+
//return new UnityWebRequest(SourcePath);
6865
break;
6966
case ScriptSource.Resource:
7067
var asset = Resources.Load(SourcePath) as TextAsset;
71-
if (asset) obs = Observable.Return(asset.text);
68+
if (asset) result = asset.text;
69+
else result = null;
7270
break;
7371
case ScriptSource.Text:
74-
obs = Observable.Return(SourceText);
72+
result = SourceText;
7573
break;
7674
default:
75+
result = null;
7776
break;
7877
}
7978

80-
if (obs != null) res.Add(obs);
81-
8279
#if UNITY_EDITOR || REACT_WATCH_OUTSIDE_EDITOR
83-
if (Watch && SourceIsWatchable) res.Add(StartWatching());
80+
if (Watch && SourceIsWatchable) return StartWatching(changeCallback);
8481
#endif
85-
return Observable.Concat(res);
82+
return null;
8683
}
8784
}
8885

@@ -99,21 +96,18 @@ public enum ScriptSource
9996
#if UNITY_EDITOR || REACT_WATCH_OUTSIDE_EDITOR
10097
public class DetectChanges
10198
{
102-
public static IObservable<string> WatchFileSystem(string path)
99+
public static IDisposable WatchFileSystem(string path, Action<string> callback)
103100
{
104-
return Observable.Create<string>(observer =>
105-
{
106-
System.IO.FileSystemWatcher fileSystemWatcher = new System.IO.FileSystemWatcher();
101+
System.IO.FileSystemWatcher fileSystemWatcher = new System.IO.FileSystemWatcher();
107102

108-
fileSystemWatcher.Path = System.IO.Path.GetDirectoryName(path);
109-
fileSystemWatcher.Filter = System.IO.Path.GetFileName(path);
110-
fileSystemWatcher.NotifyFilter = System.IO.NotifyFilters.LastWrite | System.IO.NotifyFilters.Size;
103+
fileSystemWatcher.Path = System.IO.Path.GetDirectoryName(path);
104+
fileSystemWatcher.Filter = System.IO.Path.GetFileName(path);
105+
fileSystemWatcher.NotifyFilter = System.IO.NotifyFilters.LastWrite | System.IO.NotifyFilters.Size;
111106

112-
fileSystemWatcher.Changed += (x, y) => observer.OnNext(y.FullPath);
113-
fileSystemWatcher.EnableRaisingEvents = true;
107+
fileSystemWatcher.Changed += (x, y) => callback(y.FullPath);
108+
fileSystemWatcher.EnableRaisingEvents = true;
114109

115-
return fileSystemWatcher;
116-
});
110+
return fileSystemWatcher;
117111
}
118112
}
119113
#endif

Runtime/ReactUnity.asmdef

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
"name": "ReactUnity",
33
"references": [
44
"GUID:bc838520252013847b74efc682a35cc2",
5-
"GUID:776d03a35f1b52c4a9aed9f56d7b4229",
65
"GUID:6055be8ebefd69e48b49212b09b47b2f",
7-
"GUID:560b04d1a97f54a4e82edc0cbbb69285",
86
"GUID:a2b0e588887412244ba068aa99108de7",
97
"GUID:68550284b645f4b9894995579f34290a"
108
],
@@ -15,6 +13,12 @@
1513
"precompiledReferences": [],
1614
"autoReferenced": true,
1715
"defineConstraints": [],
18-
"versionDefines": [],
16+
"versionDefines": [
17+
{
18+
"name": "com.unity.vectorgraphics",
19+
"expression": "",
20+
"define": "REACT_VECTOR_GRAPHICS"
21+
}
22+
],
1923
"noEngineReferences": false
2024
}

0 commit comments

Comments
 (0)