-
Notifications
You must be signed in to change notification settings - Fork 877
Expand file tree
/
Copy pathVFXAttachPanel.cs
More file actions
97 lines (85 loc) · 3.19 KB
/
Copy pathVFXAttachPanel.cs
File metadata and controls
97 lines (85 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using UnityEngine;
using UnityEngine.UIElements;
using UnityEngine.VFX;
namespace UnityEditor.VFX.UI
{
class VFXAttachPanel : EditorWindow
{
TextField m_pickedObjectLabel;
Button m_AttachButton;
VisualElement m_VFXIcon;
VFXView m_VFXView;
public Vector2 WindowSize { get; } = new Vector2(250, 50);
public void SetView(VFXView view)
{
m_VFXView = view;
}
protected void CreateGUI()
{
rootVisualElement.styleSheets.Add(VFXView.LoadStyleSheet("VFXAttachPanel"));
var tpl = VFXView.LoadUXML("VFXAttachPanel");
var mainContainer = tpl.CloneTree();
m_AttachButton = mainContainer.Q<Button>("AttachButton");
m_AttachButton.clicked += OnAttach;
var button = mainContainer.Q<Button>("PickButton");
button.clicked += OnPickObject;
m_pickedObjectLabel = mainContainer.Q<TextField>("PickLabel");
m_pickedObjectLabel.isReadOnly = true;
m_VFXIcon = mainContainer.Q<VisualElement>("VFXIcon");
UpdateAttachedLabel();
rootVisualElement.Add(mainContainer);
}
void OnAttach()
{
if (m_VFXView.attachedComponent != null)
{
m_VFXView.Detach();
}
else
{
m_VFXView.AttachToSelection();
}
UpdateAttachedLabel();
}
void OnPickObject()
{
VFXPicker.Pick(m_VFXView.controller?.graph?.visualEffectResource.asset, SelectHandler);
}
void SelectHandler(VisualEffect vfx)
{
if (vfx != null)
{
m_VFXView.TryAttachTo(vfx, true);
}
else
{
m_VFXView.Detach();
}
UpdateAttachedLabel();
}
void UpdateAttachedLabel()
{
if (m_VFXView.controller?.graph != null)
{
var isAttached = m_VFXView.attachedComponent != null;
VisualEffect selectedVisualEffect = null;
Selection.activeGameObject?.TryGetComponent(out selectedVisualEffect);
var isCompatible = selectedVisualEffect != null && selectedVisualEffect.visualEffectAsset == m_VFXView.controller.graph.visualEffectResource.asset;
m_AttachButton.SetEnabled(isAttached || isCompatible);
m_AttachButton.text = isAttached ? "Detach" : "Attach to selection";
m_pickedObjectLabel.value = m_VFXView.attachedComponent?.name ?? "None (Visual Effect Asset)";
if (isAttached)
{
m_VFXIcon.style.display = DisplayStyle.Flex;
m_pickedObjectLabel[0].style.paddingLeft = 18;
m_VFXIcon.style.backgroundImage = VFXView.LoadImage(EditorGUIUtility.isProSkin ? "vfx_graph_icon_gray_dark" : "vfx_graph_icon_gray_light");
}
else
{
m_pickedObjectLabel[0].style.paddingLeft = 3;
m_VFXIcon.style.display = DisplayStyle.None;
}
}
}
}
}