Skip to content

Commit 0d6daed

Browse files
committed
Add support for sync mode None
- Hacky initial support that probably needs work to interact cleanly with how VRC now enforces the sync mode through the API to be the same on all components on the same game object
1 parent 5394f13 commit 0d6daed

5 files changed

Lines changed: 85 additions & 42 deletions

File tree

Assets/UdonSharp/Editor/Editors/UdonSharpComponentExtensions.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using UdonSharp;
55
using UnityEditor;
66
using UnityEngine;
7+
using VRC.SDKBase;
78
using VRC.Udon;
89

910
namespace UdonSharpEditor
@@ -150,7 +151,18 @@ public static UdonSharpBehaviour AddUdonSharpComponent(this GameObject gameObjec
150151
udonBehaviour.AllowCollisionOwnershipTransfer = false;
151152
#pragma warning restore CS0618 // Type or member is obsolete
152153

153-
udonBehaviour.Reliable = programAsset.behaviourSyncMode == BehaviourSyncMode.Manual;
154+
switch (programAsset.behaviourSyncMode)
155+
{
156+
case BehaviourSyncMode.Continuous:
157+
udonBehaviour.SyncMethod = Networking.SyncType.Continuous;
158+
break;
159+
case BehaviourSyncMode.Manual:
160+
udonBehaviour.SyncMethod = Networking.SyncType.Manual;
161+
break;
162+
case BehaviourSyncMode.None:
163+
udonBehaviour.SyncMethod = Networking.SyncType.None;
164+
break;
165+
}
154166

155167
SerializedObject componentAsset = new SerializedObject(udonBehaviour);
156168
SerializedProperty serializedProgramAssetProperty = componentAsset.FindProperty("serializedProgramAsset");

Assets/UdonSharp/Editor/Editors/UdonSharpGUI.cs

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using UdonSharp.Compiler;
1111
using UnityEditor;
1212
using UnityEngine;
13+
using VRC.SDKBase;
1314
using VRC.Udon;
1415
using VRC.Udon.Common;
1516
using VRC.Udon.Common.Interfaces;
@@ -28,11 +29,12 @@ internal class SyncModeMenu : EditorWindow
2829
private static GUIStyle descriptionStyle;
2930

3031
private static readonly List<(GUIContent, GUIContent)> Labels = new List<(GUIContent, GUIContent)>(new[] {
32+
(new GUIContent("None"), new GUIContent("Replication will be disabled. Variables cannot be synced, and this behaviour will not receive network events.")),
3133
(new GUIContent("Continuous"), new GUIContent("Continuous replication is intended for frequently-updated variables of small size, and will be tweened.")),
3234
(new GUIContent("Manual"), new GUIContent("Manual replication is intended for infrequently-updated variables of small or large size, and will not be tweened.")),
3335
});
34-
35-
static Rect GetAreaRect(Rect rect)
36+
37+
private static Rect GetAreaRect(Rect rect)
3638
{
3739
const float borderWidth = 1f;
3840

@@ -68,7 +70,7 @@ void DrawSelectionOption(GUIContent title, GUIContent descriptor, int index)
6870
checkboxStyle.padding.right = 0;
6971
checkboxStyle.margin.right = 0;
7072

71-
if (udonBehaviour.Reliable == (index == 1))
73+
if (udonBehaviour.SyncMethod == (Networking.SyncType)(index + 1))
7274
EditorGUILayout.LabelField("✔", checkboxStyle, GUILayout.Width(10f));
7375
else
7476
EditorGUILayout.LabelField("", checkboxStyle, GUILayout.Width(10f));
@@ -103,14 +105,14 @@ void DrawSelectionOption(GUIContent title, GUIContent descriptor, int index)
103105
}
104106
}
105107

106-
void SelectIndex(int idx)
108+
private void SelectIndex(int idx)
107109
{
108110
selectedIdx = idx;
109111

110-
if (udonBehaviour.Reliable != (selectedIdx == 1))
112+
if (udonBehaviour.SyncMethod != (Networking.SyncType)(selectedIdx + 1))
111113
{
112114
Undo.RecordObject(udonBehaviour, "Change sync mode");
113-
udonBehaviour.Reliable = selectedIdx == 1;
115+
udonBehaviour.SyncMethod = (Networking.SyncType)(selectedIdx + 1);
114116

115117
PrefabUtility.RecordPrefabInstancePropertyModifications(udonBehaviour);
116118
}
@@ -141,7 +143,7 @@ void DrawSelectionOutline(Rect rect)
141143
GUI.Box(new Rect(rect.x - outlineWidth, rect.y + rect.height + outlineWidth + 1f, rect.width + outlineWidth * 2f, outlineWidth), GUIContent.none, outlineStyle);
142144
}
143145

144-
internal static Rect GUIToScreenRect(Rect rect)
146+
private static Rect GUIToScreenRect(Rect rect)
145147
{
146148
Vector2 point = GUIUtility.GUIToScreenPoint(new Vector2(rect.x, rect.y));
147149
rect.x = point.x;
@@ -190,7 +192,7 @@ public static void Show(Rect controlRect, UdonBehaviour[] behaviours)
190192
menu.ShowDropDown(controlRect, dropdownSize);
191193
}
192194

193-
static Vector2 CalculateDropdownSize(Rect controlRect)
195+
private static Vector2 CalculateDropdownSize(Rect controlRect)
194196
{
195197
Rect areaRect = GetAreaRect(controlRect);
196198
areaRect.width -= 30f; // Checkbox width
@@ -200,7 +202,7 @@ static Vector2 CalculateDropdownSize(Rect controlRect)
200202
for (int i = 0; i < Labels.Count; ++i)
201203
{
202204
totalHeight += EditorStyles.boldLabel.CalcHeight(Labels[i].Item1, areaRect.width);
203-
totalHeight += 6f; // Space()
205+
totalHeight += 13f; // Space()
204206
totalHeight += descriptionStyle.CalcHeight(Labels[i].Item2, areaRect.width);
205207
totalHeight += selectionStyle.margin.vertical;
206208
totalHeight += selectionStyle.padding.vertical;
@@ -211,22 +213,22 @@ static Vector2 CalculateDropdownSize(Rect controlRect)
211213
return new Vector2(controlRect.width, totalHeight);
212214
}
213215

214-
static Array popupLocationArray;
216+
private static Array _popupLocationArray;
215217

216218
void ShowDropDown(Rect controlRect, Vector2 size)
217219
{
218-
if (popupLocationArray == null)
220+
if (_popupLocationArray == null)
219221
{
220222
System.Type popupLocationType = AppDomain.CurrentDomain.GetAssemblies().First(e => e.GetName().Name == "UnityEditor").GetType("UnityEditor.PopupLocation");
221223

222-
popupLocationArray = (Array)Activator.CreateInstance(popupLocationType.MakeArrayType(), 2);
223-
popupLocationArray.SetValue(0, 0); // PopupLocation.Below
224-
popupLocationArray.SetValue(4, 1); // PopupLocation.Overlay
224+
_popupLocationArray = (Array)Activator.CreateInstance(popupLocationType.MakeArrayType(), 2);
225+
_popupLocationArray.SetValue(0, 0); // PopupLocation.Below
226+
_popupLocationArray.SetValue(4, 1); // PopupLocation.Overlay
225227
}
226228

227229
MethodInfo showAsDropDownMethod = typeof(EditorWindow).GetMethods(BindingFlags.NonPublic | BindingFlags.Instance).First(e => e.GetParameters().Length == 3);
228230

229-
showAsDropDownMethod.Invoke(this, new object[] { controlRect, size, popupLocationArray });
231+
showAsDropDownMethod.Invoke(this, new object[] { controlRect, size, _popupLocationArray });
230232
}
231233
}
232234
#endregion
@@ -1364,7 +1366,7 @@ internal static void DrawSyncSettings(UdonBehaviour behaviour)
13641366
if (otherBehaviour.programSource is UdonSharpProgramAsset otherBehaviourProgram && otherBehaviourProgram.behaviourSyncMode == BehaviourSyncMode.NoVariableSync)
13651367
continue;
13661368

1367-
if (otherBehaviour.Reliable)
1369+
if (otherBehaviour.SyncMethod == Networking.SyncType.Manual)
13681370
hasReliableSync = true;
13691371
else
13701372
hasContinuousSync = true;
@@ -1381,7 +1383,7 @@ internal static void DrawSyncSettings(UdonBehaviour behaviour)
13811383
}
13821384

13831385
// Dropdown for the sync settings
1384-
if (programAsset.behaviourSyncMode != BehaviourSyncMode.NoVariableSync)
1386+
if (programAsset.behaviourSyncMode != BehaviourSyncMode.NoVariableSync && programAsset.behaviourSyncMode != BehaviourSyncMode.None)
13851387
{
13861388
bool allowsSyncConfig = programAsset.behaviourSyncMode == BehaviourSyncMode.Any;
13871389

@@ -1396,7 +1398,21 @@ internal static void DrawSyncSettings(UdonBehaviour behaviour)
13961398
if (dropdownButtonMethod == null)
13971399
dropdownButtonMethod = typeof(EditorGUI).GetMethod("DropdownButton", BindingFlags.NonPublic | BindingFlags.Static, null, new Type[] { typeof(int), typeof(Rect), typeof(GUIContent), typeof(GUIStyle) }, null);
13981400

1399-
if ((bool)dropdownButtonMethod.Invoke(null, new object[] { id, dropdownRect, new GUIContent(behaviour.Reliable ? "Manual" : "Continuous"), EditorStyles.miniPullDown }))
1401+
string dropdownText;
1402+
switch (behaviour.SyncMethod)
1403+
{
1404+
case Networking.SyncType.Continuous:
1405+
dropdownText = "Continuous";
1406+
break;
1407+
case Networking.SyncType.Manual:
1408+
dropdownText = "Manual";
1409+
break;
1410+
default:
1411+
dropdownText = "None";
1412+
break;
1413+
}
1414+
1415+
if ((bool)dropdownButtonMethod.Invoke(null, new object[] { id, dropdownRect, new GUIContent(dropdownText), EditorStyles.miniPullDown }))
14001416
{
14011417
SyncModeMenu.Show(syncMethodRect, new UdonBehaviour[] { behaviour });
14021418

@@ -1405,18 +1421,18 @@ internal static void DrawSyncSettings(UdonBehaviour behaviour)
14051421

14061422
EditorGUI.EndDisabledGroup();
14071423

1408-
bool newReliableState = behaviour.Reliable;
1424+
bool newReliableState = behaviour.SyncMethod == Networking.SyncType.Manual;
14091425

14101426
// Handle auto setting of sync mode if the component has just been created
1411-
if (programAsset.behaviourSyncMode == BehaviourSyncMode.Continuous && behaviour.Reliable)
1427+
if (programAsset.behaviourSyncMode == BehaviourSyncMode.Continuous && behaviour.SyncMethod == Networking.SyncType.Manual)
14121428
newReliableState = false;
1413-
else if (programAsset.behaviourSyncMode == BehaviourSyncMode.Manual && !behaviour.Reliable)
1429+
else if (programAsset.behaviourSyncMode == BehaviourSyncMode.Manual && behaviour.SyncMethod != Networking.SyncType.Manual)
14141430
newReliableState = true;
14151431

1416-
if (newReliableState != behaviour.Reliable)
1432+
if (newReliableState != (behaviour.SyncMethod == Networking.SyncType.Manual))
14171433
{
14181434
Undo.RecordObject(behaviour, "Update sync mode");
1419-
behaviour.Reliable = newReliableState;
1435+
behaviour.SyncMethod = newReliableState ? Networking.SyncType.Manual : Networking.SyncType.Continuous;
14201436
}
14211437
}
14221438

Assets/UdonSharp/Editor/UdonSharpCompiler.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
using System.CodeDom;
1+

2+
using System.CodeDom;
23
using System.CodeDom.Compiler;
3-
using System.Collections.Concurrent;
44
using System.Collections.Generic;
5-
using System.Globalization;
65
using System.IO;
76
using System.Linq;
87
using System.Reflection;
98
using System.Text;
10-
using System.Threading;
119
using System.Threading.Tasks;
1210
using JetBrains.Annotations;
1311
using Microsoft.CodeAnalysis;

Assets/UdonSharp/Editor/UdonSharpEditorManager.cs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using UnityEditor.SceneManagement;
1111
using UnityEngine;
1212
using UnityEngine.SceneManagement;
13+
using VRC.SDKBase;
1314
using VRC.Udon;
1415
using VRC.Udon.Common;
1516
using VRC.Udon.Common.Interfaces;
@@ -733,15 +734,27 @@ static void UpdateSyncModes(List<UdonBehaviour> udonBehaviours)
733734

734735
behaviourGameObjects.Add(behaviour.gameObject);
735736

736-
if (behaviour.Reliable == true && programAsset.behaviourSyncMode == BehaviourSyncMode.Continuous)
737-
{
738-
behaviour.Reliable = false;
739-
modificationCount++;
740-
PrefabUtility.RecordPrefabInstancePropertyModifications(behaviour);
741-
}
742-
else if (behaviour.Reliable == false && programAsset.behaviourSyncMode == BehaviourSyncMode.Manual)
737+
if (programAsset.behaviourSyncMode == BehaviourSyncMode.Any ||
738+
programAsset.behaviourSyncMode == BehaviourSyncMode.NoVariableSync)
739+
continue;
740+
741+
if (behaviour.SyncMethod == Networking.SyncType.None && programAsset.behaviourSyncMode != BehaviourSyncMode.None ||
742+
behaviour.SyncMethod == Networking.SyncType.Continuous && programAsset.behaviourSyncMode != BehaviourSyncMode.Continuous ||
743+
behaviour.SyncMethod == Networking.SyncType.Manual && programAsset.behaviourSyncMode != BehaviourSyncMode.Manual)
743744
{
744-
behaviour.Reliable = true;
745+
switch (programAsset.behaviourSyncMode)
746+
{
747+
case BehaviourSyncMode.Continuous:
748+
behaviour.SyncMethod = Networking.SyncType.Continuous;
749+
break;
750+
case BehaviourSyncMode.Manual:
751+
behaviour.SyncMethod = Networking.SyncType.Manual;
752+
break;
753+
case BehaviourSyncMode.None:
754+
behaviour.SyncMethod = Networking.SyncType.None;
755+
break;
756+
}
757+
745758
modificationCount++;
746759
PrefabUtility.RecordPrefabInstancePropertyModifications(behaviour);
747760
}
@@ -766,9 +779,9 @@ static void UpdateSyncModes(List<UdonBehaviour> udonBehaviours)
766779
continue;
767780
}
768781

769-
if (objectBehaviour.Reliable)
782+
if (objectBehaviour.SyncMethod == Networking.SyncType.Manual)
770783
hasManual = true;
771-
else
784+
else if (objectBehaviour.SyncMethod == Networking.SyncType.Continuous)
772785
hasContinuous = true;
773786

774787
#pragma warning disable CS0618 // Type or member is obsolete
@@ -808,16 +821,16 @@ static void UpdateSyncModes(List<UdonBehaviour> udonBehaviours)
808821
{
809822
if (behaviour.programSource is UdonSharpProgramAsset programAsset && programAsset.behaviourSyncMode == BehaviourSyncMode.NoVariableSync)
810823
{
811-
if (hasManual && !behaviour.Reliable)
824+
if (hasManual && behaviour.SyncMethod != Networking.SyncType.Manual)
812825
{
813-
behaviour.Reliable = true;
826+
behaviour.SyncMethod = Networking.SyncType.Manual;
814827
modificationCount++;
815828

816829
PrefabUtility.RecordPrefabInstancePropertyModifications(behaviour);
817830
}
818-
else if (behaviour.Reliable)
831+
else if (behaviour.SyncMethod == Networking.SyncType.Manual)
819832
{
820-
behaviour.Reliable = false;
833+
behaviour.SyncMethod = Networking.SyncType.Continuous;
821834
modificationCount++;
822835

823836
PrefabUtility.RecordPrefabInstancePropertyModifications(behaviour);

Assets/UdonSharp/Scripts/UdonSharpAttributes.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ public enum BehaviourSyncMode
5252
/// </summary>
5353
Any,
5454
/// <summary>
55+
/// Enforces no synced variables on the behaviour and hides the selection dropdown in the UI for the sync mode. Nothing is synced and SendCustomNetworkEvent will not work on the behaviour
56+
/// </summary>
57+
None,
58+
/// <summary>
5559
/// Enforces no synced variables on the behaviour and hides the selection dropdown in the UI for the sync mode, SendCustomNetworkEvent() will still work on this behaviour
5660
/// </summary>
5761
NoVariableSync,

0 commit comments

Comments
 (0)