VFXParameterInfo.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using System;
  2. using System.Text;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine;
  6. using UnityEditor.VFX;
  7. using UnityEngine.VFX;
  8. using UnityEngine.Profiling;
  9. using System.Reflection;
  10. using Object = UnityEngine.Object;
  11. namespace UnityEditor.VFX
  12. {
  13. [Serializable]
  14. struct VFXParameterInfo
  15. {
  16. public VFXParameterInfo(string exposedName, string realType)
  17. {
  18. name = exposedName;
  19. this.realType = realType;
  20. path = null;
  21. tooltip = null;
  22. defaultValue = null;
  23. min = Mathf.NegativeInfinity;
  24. max = Mathf.Infinity;
  25. descendantCount = 0;
  26. sheetType = null;
  27. enumValues = null;
  28. }
  29. public string name;
  30. public string path;
  31. public string tooltip;
  32. public string sheetType;
  33. public string realType;
  34. public VFXSerializableObject defaultValue;
  35. public float min;
  36. public float max;
  37. public List<string> enumValues;
  38. public int descendantCount;
  39. public static VFXParameterInfo[] BuildParameterInfo(VFXGraph graph)
  40. {
  41. var categories = graph.UIInfos.categories;
  42. if (categories == null)
  43. categories = new List<VFXUI.CategoryInfo>();
  44. var parameters = graph.children.OfType<VFXParameter>().Where(t => t.exposed && (string.IsNullOrEmpty(t.category) || !categories.Any(u => u.name == t.category)) && !t.isOutput).OrderBy(t => t.order).ToArray();
  45. var infos = new List<VFXParameterInfo>();
  46. BuildCategoryParameterInfo(parameters, infos);
  47. foreach (var cat in categories)
  48. {
  49. parameters = graph.children.OfType<VFXParameter>().Where(t => t.exposed && t.category == cat.name && !t.isOutput).OrderBy(t => t.order).ToArray();
  50. if (parameters.Length > 0)
  51. {
  52. VFXParameterInfo paramInfo = new VFXParameterInfo(cat.name, "");
  53. paramInfo.descendantCount = 0;
  54. infos.Add(paramInfo);
  55. BuildCategoryParameterInfo(parameters, infos);
  56. }
  57. }
  58. return infos.ToArray();
  59. }
  60. static void BuildCategoryParameterInfo(VFXParameter[] parameters, List<VFXParameterInfo> infos)
  61. {
  62. var subList = new List<VFXParameterInfo>();
  63. foreach (var parameter in parameters)
  64. {
  65. string rootFieldName = VisualEffectSerializationUtility.GetTypeField(parameter.type);
  66. VFXParameterInfo paramInfo = new VFXParameterInfo(parameter.exposedName, parameter.type.Name);
  67. paramInfo.tooltip = parameter.tooltip;
  68. if (rootFieldName != null)
  69. {
  70. paramInfo.sheetType = rootFieldName;
  71. paramInfo.path = paramInfo.name;
  72. if (parameter.valueFilter == VFXValueFilter.Range)
  73. {
  74. float min = (float)System.Convert.ChangeType(parameter.min, typeof(float));
  75. float max = (float)System.Convert.ChangeType(parameter.max, typeof(float));
  76. paramInfo.min = min;
  77. paramInfo.max = max;
  78. }
  79. else if (parameter.valueFilter == VFXValueFilter.Enum)
  80. {
  81. paramInfo.enumValues = parameter.enumValues.ToList();
  82. }
  83. paramInfo.defaultValue = new VFXSerializableObject(parameter.type, parameter.value);
  84. paramInfo.descendantCount = 0;
  85. }
  86. else
  87. {
  88. paramInfo.descendantCount = RecurseBuildParameterInfo(subList, parameter.type, parameter.exposedName, parameter.value);
  89. }
  90. infos.Add(paramInfo);
  91. infos.AddRange(subList);
  92. subList.Clear();
  93. }
  94. }
  95. static int RecurseBuildParameterInfo(List<VFXParameterInfo> infos, System.Type type, string path, object obj)
  96. {
  97. if (!type.IsValueType) return 0;
  98. int count = 0;
  99. var fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public);
  100. var subList = new List<VFXParameterInfo>();
  101. foreach (var field in fields)
  102. {
  103. var info = new VFXParameterInfo(field.Name, field.FieldType.Name);
  104. var subObj = field.GetValue(obj);
  105. info.path = path + "_" + field.Name;
  106. var tooltip = field.GetCustomAttributes(typeof(TooltipAttribute), true).FirstOrDefault() as TooltipAttribute;
  107. if (tooltip != null)
  108. {
  109. info.tooltip = tooltip.tooltip;
  110. }
  111. var fieldName = VisualEffectSerializationUtility.GetTypeField(field.FieldType);
  112. if (fieldName != null)
  113. {
  114. info.sheetType = fieldName;
  115. RangeAttribute attr = field.GetCustomAttributes(true).OfType<RangeAttribute>().FirstOrDefault();
  116. if (attr != null)
  117. {
  118. info.min = attr.min;
  119. info.max = attr.max;
  120. }
  121. info.descendantCount = 0;
  122. info.defaultValue = new VFXSerializableObject(field.FieldType, subObj);
  123. }
  124. else
  125. {
  126. if (field.FieldType == typeof(VFXCoordinateSpace)) // For space
  127. continue;
  128. info.descendantCount = RecurseBuildParameterInfo(subList, field.FieldType, info.path, subObj);
  129. }
  130. infos.Add(info);
  131. infos.AddRange(subList);
  132. subList.Clear();
  133. count++;
  134. }
  135. return count;
  136. }
  137. }
  138. }