暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AnimatedPropertyUtility.cs 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using UnityEngine;
  3. namespace UnityEditor.Timeline
  4. {
  5. // Helper methods for animated properties
  6. internal static class AnimatedPropertyUtility
  7. {
  8. public static bool IsMaterialProperty(string propertyName)
  9. {
  10. return propertyName.StartsWith("material.");
  11. }
  12. /// <summary>
  13. /// Given a propertyName (from an EditorCurveBinding), and the gameObject it refers to,
  14. /// remaps the path to include the exposed name of the shader parameter
  15. /// </summary>
  16. /// <param name="gameObject">The gameObject being referenced.</param>
  17. /// <param name="propertyName">The propertyName to remap.</param>
  18. /// <returns>The remapped propertyName, or the original propertyName if it cannot be remapped</returns>
  19. public static string RemapMaterialName(GameObject gameObject, string propertyName)
  20. {
  21. if (!IsMaterialProperty(propertyName) || gameObject == null)
  22. return propertyName;
  23. var renderers = gameObject.GetComponents<Renderer>();
  24. if (renderers == null || renderers.Length == 0)
  25. return propertyName;
  26. var propertySplits = propertyName.Split('.');
  27. if (propertySplits.Length <= 1)
  28. return propertyName;
  29. // handles post fixes for texture properties
  30. var exposedParameter = HandleTextureProperties(propertySplits[1], out var postFix);
  31. foreach (var renderer in renderers)
  32. {
  33. foreach (var material in renderer.sharedMaterials)
  34. {
  35. if (material.shader == null)
  36. continue;
  37. var index = material.shader.FindPropertyIndex(exposedParameter);
  38. if (index >= 0)
  39. {
  40. propertySplits[1] = material.shader.GetPropertyDescription(index) + postFix;
  41. return String.Join(".", propertySplits);
  42. }
  43. }
  44. }
  45. return propertyName;
  46. }
  47. private static string HandleTextureProperties(string exposedParameter, out string postFix)
  48. {
  49. postFix = String.Empty;
  50. RemoveEnding(ref exposedParameter, ref postFix, "_ST");
  51. RemoveEnding(ref exposedParameter, ref postFix, "_TexelSize");
  52. RemoveEnding(ref exposedParameter, ref postFix, "_HDR");
  53. return exposedParameter;
  54. }
  55. private static void RemoveEnding(ref string name, ref string postFix, string ending)
  56. {
  57. if (name.EndsWith(ending))
  58. {
  59. name = name.Substring(0, name.Length - ending.Length);
  60. postFix = ending;
  61. }
  62. }
  63. }
  64. }