VFXTypeExtension.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.Linq;
  3. using System.Reflection;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using UnityEngine.VFX;
  7. namespace UnityEditor.VFX
  8. {
  9. static class VFXTypeExtension
  10. {
  11. static Dictionary<Type, string> s_FriendlyName = new Dictionary<Type, string>();
  12. static VFXTypeExtension()
  13. {
  14. s_FriendlyName[typeof(float)] = "float";
  15. s_FriendlyName[typeof(int)] = "int";
  16. s_FriendlyName[typeof(bool)] = "bool";
  17. s_FriendlyName[typeof(uint)] = "uint";
  18. s_FriendlyName[typeof(char)] = "char";
  19. s_FriendlyName[typeof(bool)] = "bool";
  20. s_FriendlyName[typeof(double)] = "double";
  21. s_FriendlyName[typeof(short)] = "short";
  22. s_FriendlyName[typeof(ushort)] = "ushort";
  23. s_FriendlyName[typeof(long)] = "long";
  24. s_FriendlyName[typeof(ulong)] = "ulong";
  25. s_FriendlyName[typeof(byte)] = "byte";
  26. s_FriendlyName[typeof(sbyte)] = "sbyte";
  27. s_FriendlyName[typeof(decimal)] = "decimal";
  28. s_FriendlyName[typeof(char)] = "char";
  29. s_FriendlyName[typeof(string)] = "string";
  30. }
  31. public static string UserFriendlyName(this Type type)
  32. {
  33. if (type == null)
  34. return "null";
  35. if (s_FriendlyName.TryGetValue(type, out var result))
  36. return result;
  37. result = type.Name;
  38. var typeAttribute = type.GetCustomAttributes(typeof(VFXTypeAttribute), true).FirstOrDefault() as VFXTypeAttribute;
  39. if (typeAttribute != null && typeAttribute.name != null)
  40. result = typeAttribute.name;
  41. s_FriendlyName.Add(type, result);
  42. return result;
  43. }
  44. // needed only for .NET 4.6 which make the GetNestedType non recursive
  45. public static Type GetRecursiveNestedType(this Type type, string typeName)
  46. {
  47. do
  48. {
  49. Type nestedType = type.GetNestedType(typeName);
  50. if (nestedType != null)
  51. return nestedType;
  52. type = type.BaseType;
  53. }
  54. while (type != null);
  55. return null;
  56. }
  57. public static object GetDefaultField(Type type)
  58. {
  59. if (type == typeof(Matrix4x4))
  60. {
  61. return Matrix4x4.identity;
  62. }
  63. else if (type == typeof(AnimationCurve))
  64. {
  65. return VFXResources.defaultResources.animationCurve;
  66. }
  67. else if (type == typeof(Gradient))
  68. {
  69. return VFXResources.defaultResources.gradient;
  70. }
  71. else if (type == typeof(Mesh))
  72. {
  73. return VFXResources.defaultResources.mesh;
  74. }
  75. else if (type == typeof(Shader))
  76. {
  77. return VFXResources.defaultResources.shader;
  78. }
  79. else if (type == typeof(Texture2D))
  80. {
  81. return VFXResources.defaultResources.particleTexture;
  82. }
  83. else if (type == typeof(Texture3D))
  84. {
  85. return VFXResources.defaultResources.vectorField;
  86. }
  87. var defaultField = type.GetField("defaultValue", BindingFlags.Public | BindingFlags.Static);
  88. if (defaultField != null)
  89. {
  90. return defaultField.GetValue(null);
  91. }
  92. return null;
  93. }
  94. }
  95. }