説明なし
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

IdentifierControl.cs 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 IdentifierControlAttribute : Attribute, IControlAttribute
  10. {
  11. string m_Label;
  12. public IdentifierControlAttribute(string label = null)
  13. {
  14. m_Label = label;
  15. }
  16. public VisualElement InstantiateControl(AbstractMaterialNode node, PropertyInfo propertyInfo)
  17. {
  18. return new IdentifierControlView(m_Label, node, propertyInfo);
  19. }
  20. }
  21. class IdentifierControlView : VisualElement
  22. {
  23. AbstractMaterialNode m_Node;
  24. PropertyInfo m_PropertyInfo;
  25. public IdentifierControlView(string label, AbstractMaterialNode node, PropertyInfo propertyInfo)
  26. {
  27. var style = Resources.Load<StyleSheet>("Styles/Controls/IdentifierControlView");
  28. if (style) styleSheets.Add(style);
  29. m_Node = node;
  30. m_PropertyInfo = propertyInfo;
  31. if (propertyInfo.PropertyType != typeof(string))
  32. throw new ArgumentException("Property must be of type string.", "propertyInfo");
  33. label = label ?? ObjectNames.NicifyVariableName(propertyInfo.Name);
  34. if (!string.IsNullOrEmpty(label))
  35. Add(new Label(label));
  36. var strField = new IdentifierField() { value = (string)m_PropertyInfo.GetValue(m_Node, null) };
  37. strField.RegisterValueChangedCallback(OnChange);
  38. Add(strField);
  39. }
  40. void OnChange(ChangeEvent<string> evt)
  41. {
  42. m_Node.owner.owner.RegisterCompleteObjectUndo("Identifier Change");
  43. m_PropertyInfo.SetValue(m_Node, evt.newValue, null);
  44. this.MarkDirtyRepaint();
  45. }
  46. }
  47. }