A game about forced loneliness, made by TACStudios
1using System;
2using System.Reflection;
3using UnityEngine;
4using Object = UnityEngine.Object;
5using UnityEditor.UIElements;
6using UnityEngine.UIElements;
7
8namespace UnityEditor.ShaderGraph.Drawing.Controls
9{
10 [AttributeUsage(AttributeTargets.Property)]
11 class ObjectControlAttribute : Attribute, IControlAttribute
12 {
13 string m_Label;
14
15 public ObjectControlAttribute(string label = null)
16 {
17 m_Label = label;
18 }
19
20 public VisualElement InstantiateControl(AbstractMaterialNode node, PropertyInfo propertyInfo)
21 {
22 return new ObjectControlView(m_Label, node, propertyInfo);
23 }
24 }
25
26 class ObjectControlView : VisualElement
27 {
28 AbstractMaterialNode m_Node;
29 PropertyInfo m_PropertyInfo;
30
31 public ObjectControlView(string label, AbstractMaterialNode node, PropertyInfo propertyInfo)
32 {
33 if (!typeof(Object).IsAssignableFrom(propertyInfo.PropertyType))
34 throw new ArgumentException("Property must be assignable to UnityEngine.Object.");
35 m_Node = node;
36 m_PropertyInfo = propertyInfo;
37 label = label ?? propertyInfo.Name;
38
39 if (!string.IsNullOrEmpty(label))
40 Add(new Label { text = label });
41
42 var value = (Object)m_PropertyInfo.GetValue(m_Node, null);
43 var objectField = new ObjectField { objectType = propertyInfo.PropertyType, value = value };
44 objectField.RegisterValueChangedCallback(OnValueChanged);
45 Add(objectField);
46 }
47
48 void OnValueChanged(ChangeEvent<Object> evt)
49 {
50 var value = (Object)m_PropertyInfo.GetValue(m_Node, null);
51 if (evt.newValue != value)
52 {
53 m_Node.owner.owner.RegisterCompleteObjectUndo("Change + " + m_Node.name);
54 m_PropertyInfo.SetValue(m_Node, evt.newValue, null);
55 }
56 }
57 }
58}