Skip to content

Commit eb6f548

Browse files
PaulDemeulenaereGitHub Enterprise
authored andcommitted
[Fix] PropertyBinder : Reset & Undo (#212)
* Fix missing record object for proper undo/redo & reset * *Update changelog.md * Fix issue https://favro.com/organization/c564ede4ed3337f7b17986b6/1973edb4634ba00a90689144?card=Uni-160638 * Avoid VFXPropertyBinder to be added twice * Fix incorrect reference after copy past * Fix RemoveComponent clearing property binder first * Handle Copy/Past \o/ * Let the LateUpdate without any editor code & use Update (avoid the one frame of latency for inspector) * Remove useless recompute of GetComponents every frame, and use Reload (safer) * Remove dead code * *Add new entry in changelog (side improvement/fix) * *Update comment
1 parent 415ed6c commit eb6f548

File tree

3 files changed

+67
-6
lines changed

3 files changed

+67
-6
lines changed

com.unity.visualeffectgraph/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1818
- Move Assets/Create/Visual Effects/Visual Effect Subgraph Block to Assets/Create/VFX/VFX Subgraph Block
1919
- Allow remaking an existing link.
2020
- Sphere and Cube outputs are now experimental
21+
- Property Binder : Handle Remove Component removing linked hidden scriptable objectfields
22+
- Property Binder : Prevent multiple VFXPropertyBinder within the same game object
2123

2224
### Fixed
2325
- VFXEventBinderBase throwing a null reference exception in runtime
@@ -37,6 +39,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
3739
- Sample Mesh Color when value is stored as float.
3840
- Compilation error due to direct access to GetWorldToObjectMatrix instead of VFXGetWorldToObjectMatrix [Case 1308481](https://issuetracker.unity3d.com/product/unity/issues/guid/1308481/)
3941
- Safe Normalization of Cross Products in Orient blocks [Case 1272724](https://issuetracker.unity3d.com/product/unity/issues/guid/1272724)
42+
- Property Binder : Undo after reset [Case 1293794](https://issuetracker.unity3d.com/product/unity/issues/guid/1293794/)
43+
- Property Binder : Allow copy/past from a game object to another
4044

4145
## [11.0.0] - 2020-10-21
4246

com.unity.visualeffectgraph/Editor/Utilities/PropertyBinding/VFXPropertyBinderEditor.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ namespace UnityEditor.Experimental.VFX.Utility
1414
[CustomEditor(typeof(VFXPropertyBinder))]
1515
class VFXPropertyBinderEditor : Editor
1616
{
17+
[MenuItem("CONTEXT/VFXPropertyBinder/Remove Component", false, 5000)]
18+
public static void RemoveComponent(MenuCommand command)
19+
{
20+
var propertyBinder = (VFXPropertyBinder)command.context;
21+
propertyBinder.ClearPropertyBinders();
22+
Undo.DestroyObjectImmediate(propertyBinder);
23+
}
24+
1725
ReorderableList m_List;
1826
SerializedProperty m_Elements;
1927
SerializedProperty m_Component;
@@ -246,7 +254,6 @@ public void RemoveElement(ReorderableList list)
246254
if (element != null)
247255
{
248256
Undo.DestroyObjectImmediate(element);
249-
m_Elements.DeleteArrayElementAtIndex(index); // Delete object reference
250257
}
251258
else
252259
{

com.unity.visualeffectgraph/Runtime/Utilities/PropertyBinding/VFXPropertyBinder.cs

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace UnityEngine.VFX.Utility
1111
/// </summary>
1212
[RequireComponent(typeof(VisualEffect))]
1313
[DefaultExecutionOrder(1)]
14+
[DisallowMultipleComponent]
1415
[ExecuteInEditMode]
1516
public class VFXPropertyBinder : MonoBehaviour
1617
{
@@ -33,28 +34,77 @@ public class VFXPropertyBinder : MonoBehaviour
3334

3435
private void OnEnable()
3536
{
36-
m_VisualEffect = GetComponent<VisualEffect>();
37+
Reload();
38+
}
39+
40+
private void OnValidate()
41+
{
42+
Reload();
3743
}
3844

3945
static private void SafeDestroy(Object toDelete)
4046
{
47+
#if UNITY_EDITOR
4148
if (Application.isPlaying)
42-
Destroy(toDelete);
49+
Destroy(toDelete); //Undo.DestroyObjectImmediate is needed only for Reset, which can't be called in play mode
4350
else
44-
DestroyImmediate(toDelete);
51+
UnityEditor.Undo.DestroyObjectImmediate(toDelete);
52+
#else
53+
Destroy(toDelete);
54+
#endif
4555
}
4656

47-
private void Reset()
57+
#if UNITY_EDITOR
58+
List<VFXBinderBase> m_BinderToCopyPaste;
59+
#endif
60+
61+
private void Reload()
4862
{
4963
m_VisualEffect = GetComponent<VisualEffect>();
64+
#if UNITY_EDITOR
65+
//Handle probable copy/paste component saving list of inappropriate entries.
66+
m_BinderToCopyPaste = new List<VFXBinderBase>();
67+
foreach (var bindings in m_Bindings)
68+
{
69+
if (bindings != null && bindings.gameObject != gameObject)
70+
m_BinderToCopyPaste.Add(bindings);
71+
}
72+
if (m_BinderToCopyPaste.Count == 0)
73+
m_BinderToCopyPaste = null;
74+
#endif
5075
m_Bindings = new List<VFXBinderBase>();
5176
m_Bindings.AddRange(gameObject.GetComponents<VFXBinderBase>());
77+
}
78+
79+
private void Reset()
80+
{
81+
Reload();
5282
ClearPropertyBinders();
5383
}
5484

85+
#if UNITY_EDITOR
86+
void Update()
87+
{
88+
if (m_BinderToCopyPaste != null)
89+
{
90+
//We can't add a component during a OnInvalidate, restore & copy linked binders (from copy/past) here
91+
foreach (var copyPaste in m_BinderToCopyPaste)
92+
{
93+
var type = copyPaste.GetType();
94+
var newComponent = gameObject.AddComponent(type);
95+
UnityEditor.EditorUtility.CopySerialized(copyPaste, newComponent);
96+
}
97+
98+
m_BinderToCopyPaste = null;
99+
Reload();
100+
}
101+
}
102+
103+
#endif
55104
void LateUpdate()
56105
{
57-
if (!m_ExecuteInEditor && Application.isEditor && !Application.isPlaying) return;
106+
if (!m_ExecuteInEditor && Application.isEditor && !Application.isPlaying)
107+
return;
58108

59109
for (int i = 0; i < m_Bindings.Count; i++)
60110
{

0 commit comments

Comments
 (0)