Texture3DControl.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System;
  2. using System.Reflection;
  3. using UnityEngine;
  4. using UnityEditor.UIElements;
  5. using UnityEngine.UIElements;
  6. namespace UnityEditor.ShaderGraph.Drawing.Controls
  7. {
  8. [AttributeUsage(AttributeTargets.Property)]
  9. class Texture3DControlAttribute : Attribute, IControlAttribute
  10. {
  11. string m_Label;
  12. public Texture3DControlAttribute(string label = null)
  13. {
  14. m_Label = label;
  15. }
  16. public VisualElement InstantiateControl(AbstractMaterialNode node, PropertyInfo propertyInfo)
  17. {
  18. return new Texture3DControlView(m_Label, node, propertyInfo);
  19. }
  20. }
  21. class Texture3DControlView : VisualElement
  22. {
  23. AbstractMaterialNode m_Node;
  24. PropertyInfo m_PropertyInfo;
  25. public Texture3DControlView(string label, AbstractMaterialNode node, PropertyInfo propertyInfo)
  26. {
  27. m_Node = node;
  28. m_PropertyInfo = propertyInfo;
  29. if (propertyInfo.PropertyType != typeof(Texture3D))
  30. throw new ArgumentException("Property must be of type Texture 3D.", "propertyInfo");
  31. label = label ?? ObjectNames.NicifyVariableName(propertyInfo.Name);
  32. if (!string.IsNullOrEmpty(label))
  33. Add(new Label(label));
  34. var textureField = new ObjectField { value = (Texture3D)m_PropertyInfo.GetValue(m_Node, null), objectType = typeof(Texture3D) };
  35. textureField.RegisterValueChangedCallback(OnChange);
  36. Add(textureField);
  37. }
  38. void OnChange(ChangeEvent<UnityEngine.Object> evt)
  39. {
  40. m_Node.owner.owner.RegisterCompleteObjectUndo("Texture Change");
  41. m_PropertyInfo.SetValue(m_Node, evt.newValue, null);
  42. this.MarkDirtyRepaint();
  43. }
  44. }
  45. }