123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- using System;
- using System.Text;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEngine;
- using UnityEditor.VFX;
- using UnityEngine.VFX;
- using UnityEngine.Profiling;
- using System.Reflection;
- using Object = UnityEngine.Object;
- namespace UnityEditor.VFX
- {
- [Serializable]
- struct VFXParameterInfo
- {
- public VFXParameterInfo(string exposedName, string realType)
- {
- name = exposedName;
- this.realType = realType;
- path = null;
- tooltip = null;
- defaultValue = null;
- min = Mathf.NegativeInfinity;
- max = Mathf.Infinity;
- descendantCount = 0;
- sheetType = null;
- enumValues = null;
- }
- public string name;
- public string path;
- public string tooltip;
- public string sheetType;
- public string realType;
- public VFXSerializableObject defaultValue;
- public float min;
- public float max;
- public List<string> enumValues;
- public int descendantCount;
- public static VFXParameterInfo[] BuildParameterInfo(VFXGraph graph)
- {
- var categories = graph.UIInfos.categories;
- if (categories == null)
- categories = new List<VFXUI.CategoryInfo>();
- 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();
- var infos = new List<VFXParameterInfo>();
- BuildCategoryParameterInfo(parameters, infos);
- foreach (var cat in categories)
- {
- parameters = graph.children.OfType<VFXParameter>().Where(t => t.exposed && t.category == cat.name && !t.isOutput).OrderBy(t => t.order).ToArray();
- if (parameters.Length > 0)
- {
- VFXParameterInfo paramInfo = new VFXParameterInfo(cat.name, "");
- paramInfo.descendantCount = 0;
- infos.Add(paramInfo);
- BuildCategoryParameterInfo(parameters, infos);
- }
- }
- return infos.ToArray();
- }
- static void BuildCategoryParameterInfo(VFXParameter[] parameters, List<VFXParameterInfo> infos)
- {
- var subList = new List<VFXParameterInfo>();
- foreach (var parameter in parameters)
- {
- string rootFieldName = VisualEffectSerializationUtility.GetTypeField(parameter.type);
- VFXParameterInfo paramInfo = new VFXParameterInfo(parameter.exposedName, parameter.type.Name);
- paramInfo.tooltip = parameter.tooltip;
- if (rootFieldName != null)
- {
- paramInfo.sheetType = rootFieldName;
- paramInfo.path = paramInfo.name;
- if (parameter.valueFilter == VFXValueFilter.Range)
- {
- float min = (float)System.Convert.ChangeType(parameter.min, typeof(float));
- float max = (float)System.Convert.ChangeType(parameter.max, typeof(float));
- paramInfo.min = min;
- paramInfo.max = max;
- }
- else if (parameter.valueFilter == VFXValueFilter.Enum)
- {
- paramInfo.enumValues = parameter.enumValues.ToList();
- }
- paramInfo.defaultValue = new VFXSerializableObject(parameter.type, parameter.value);
- paramInfo.descendantCount = 0;
- }
- else
- {
- paramInfo.descendantCount = RecurseBuildParameterInfo(subList, parameter.type, parameter.exposedName, parameter.value);
- }
- infos.Add(paramInfo);
- infos.AddRange(subList);
- subList.Clear();
- }
- }
- static int RecurseBuildParameterInfo(List<VFXParameterInfo> infos, System.Type type, string path, object obj)
- {
- if (!type.IsValueType) return 0;
- int count = 0;
- var fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public);
- var subList = new List<VFXParameterInfo>();
- foreach (var field in fields)
- {
- var info = new VFXParameterInfo(field.Name, field.FieldType.Name);
- var subObj = field.GetValue(obj);
- info.path = path + "_" + field.Name;
- var tooltip = field.GetCustomAttributes(typeof(TooltipAttribute), true).FirstOrDefault() as TooltipAttribute;
- if (tooltip != null)
- {
- info.tooltip = tooltip.tooltip;
- }
- var fieldName = VisualEffectSerializationUtility.GetTypeField(field.FieldType);
- if (fieldName != null)
- {
- info.sheetType = fieldName;
- RangeAttribute attr = field.GetCustomAttributes(true).OfType<RangeAttribute>().FirstOrDefault();
- if (attr != null)
- {
- info.min = attr.min;
- info.max = attr.max;
- }
- info.descendantCount = 0;
- info.defaultValue = new VFXSerializableObject(field.FieldType, subObj);
- }
- else
- {
- if (field.FieldType == typeof(VFXCoordinateSpace)) // For space
- continue;
- info.descendantCount = RecurseBuildParameterInfo(subList, field.FieldType, info.path, subObj);
- }
- infos.Add(info);
- infos.AddRange(subList);
- subList.Clear();
- count++;
- }
- return count;
- }
- }
- }
|