ShaderPort.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System;
  2. using UnityEngine;
  3. using UnityEditor.Experimental.GraphView;
  4. using UnityEngine.UIElements;
  5. namespace UnityEditor.ShaderGraph.Drawing
  6. {
  7. sealed class ShaderPort : Port
  8. {
  9. ShaderPort(Orientation portOrientation, Direction portDirection, Capacity portCapacity, Type type)
  10. : base(portOrientation, portDirection, portCapacity, type)
  11. {
  12. styleSheets.Add(Resources.Load<StyleSheet>("Styles/ShaderPort"));
  13. }
  14. MaterialSlot m_Slot;
  15. public static Port Create(MaterialSlot slot, IEdgeConnectorListener connectorListener)
  16. {
  17. var port = new ShaderPort(Orientation.Horizontal, slot.isInputSlot ? Direction.Input : Direction.Output,
  18. slot.isInputSlot ? Capacity.Single : Capacity.Multi, null)
  19. {
  20. m_EdgeConnector = new EdgeConnector<Edge>(connectorListener),
  21. };
  22. port.AddManipulator(port.m_EdgeConnector);
  23. port.slot = slot;
  24. port.portName = slot.displayName;
  25. port.visualClass = slot.concreteValueType.ToClassName();
  26. return port;
  27. }
  28. public MaterialSlot slot
  29. {
  30. get { return m_Slot; }
  31. set
  32. {
  33. if (ReferenceEquals(value, m_Slot))
  34. return;
  35. if (value == null)
  36. throw new NullReferenceException();
  37. if (m_Slot != null && value.isInputSlot != m_Slot.isInputSlot)
  38. throw new ArgumentException("Cannot change direction of already created port");
  39. m_Slot = value;
  40. portName = slot.displayName;
  41. visualClass = slot.concreteValueType.ToClassName();
  42. }
  43. }
  44. }
  45. static class ShaderPortExtensions
  46. {
  47. public static MaterialSlot GetSlot(this Port port)
  48. {
  49. var shaderPort = port as ShaderPort;
  50. return shaderPort != null ? shaderPort.slot : null;
  51. }
  52. }
  53. }