Advertisement
Demigiant

DOTween Timeline Custom Plugins Example

Feb 3rd, 2021
1,118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.12 KB | None | 0 0
  1. using System;
  2. using DG.Tweening.Timeline.Core.Plugins;
  3. using UnityEngine;
  4.  
  5. /// <summary>
  6. /// Copy paste this code to create a custom plugin (including all #if etc which are very important)
  7. /// then change the code inside GetTweenPlugin to add your own custom plugins for your own custom Components.
  8. /// SUPER IMPORTANT: every plugin is composed of multiple sub-plugins (one for each property you want to include).
  9. /// ALL SUB-PLUGIN MUST HAVE A UNIQUE ID because it's used to distinguish, retrieve and cache them
  10. /// (it can be just an string-integer as long as you make sure that that integer is never repeated)
  11. /// </summary>
  12. #if UNITY_EDITOR
  13. [UnityEditor.InitializeOnLoad]
  14. #endif
  15. // Class name is not important, you can refactor it to whatever you prefer
  16. public static class CustomPluginsExample
  17. {
  18. #if UNITY_EDITOR
  19.     static CustomPluginsExample()
  20.     {
  21.         // Needed to register custom plugins for the editor's timeline (runtime uses Register method directly)
  22.         if (!UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode) Register();
  23.     }
  24. #endif
  25.  
  26.     [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
  27.     static void Register()
  28.     {
  29.         // Registers custom plugins for runtime usage
  30.         DOVisualPluginsManager.RegisterTweenPlugins(GetTweenPlugin);
  31.     }
  32.    
  33.     // Create target-based tweens here
  34.     static DOVisualTweenPlugin GetTweenPlugin(Type targetType, string targetTypeFullName)
  35.     {
  36.         // ██████████████████████████████████████████████████████████████
  37.         // CUSTOMIZATION START ██████████████████████████████████████████
  38.  
  39.         // A plugin (and an IF) for each target type. Only one plugin per target type is allowed.
  40.  
  41.         // First target type
  42.         if (targetType == typeof(CustomComponentA)) {
  43.             return DOVisualPluginsManager.CacheAndReturn(targetType,
  44.                 // This first one is expanded so I can comment it and explain things clearer, while the others are one-liners
  45.                 new PlugDataTween(
  46.                     // Unique ID used to assign and retrieve this sub-plugin.
  47.                     // If you change it you will lose all references to this sub-plugin in all your DOTweenClips,
  48.                     // so be sure you make it unique when you write it the first time and then avoid changing it.
  49.                     // - You can set it to a GUID, like "4c65d6f3-e73f-4cb5-b8e1-bd196be4322b".
  50.                     //   Here's the nifty GUID generator for Visual Studio that I use:
  51.                     //   https://marketplace.visualstudio.com/items?itemName=MadsKristensen.insertguid
  52.                     // - You can set it to the name of the property you're adding it for,
  53.                     //   like "sampleFloat" in this case. Just be sure it's unique
  54.                     // - You can set it to any string, really. Again, as long as it's unique
  55.                     "4c65d6f3-e73f-4cb5-b8e1-bd196be4322b",
  56.                     // Label displayed in the clip element's editor. You can change this at any time, it's just decoration
  57.                     "sampleFloat",
  58.                     // Getter for the property to tween (similar to DOTween.To getters),
  59.                     // retrieves the current value of the property
  60.                     (c,s,i) => ()=> ((CustomComponentA)c).sampleFloat,
  61.                     // Setter for the property to tween (similar to DOTween.To setters),
  62.                     // sets the current value of the property
  63.                     (c,s,i) => x => ((CustomComponentA)c).sampleFloat = x
  64.                 ),
  65.                 new PlugDataTween("5cabee8c-a90a-4e03-b861-58c8b5aca3b6", "sampleVector2", (c,s,i) => ()=> ((CustomComponentA)c).sampleVector2, (c,s,i) => x => ((CustomComponentA)c).sampleVector2 = x),
  66.                 new PlugDataTween("71c78ae4-8976-48cd-8c94-793b1e454b5e", "sampleString", (c,s,i) => ()=> ((CustomComponentA)c).sampleString, (c,s,i) => x => ((CustomComponentA)c).sampleString = x)
  67.             );
  68.         }
  69.  
  70.         // Second target type
  71.         if (targetType == typeof(CustomComponentB)) {
  72.             return DOVisualPluginsManager.CacheAndReturn(targetType,
  73.                 new PlugDataTween("e6f9edc0-1d2f-4883-8fa8-ab3fb5c8e368", "sampleVector3", (c,s,i) => ()=> ((CustomComponentB)c).sampleVector3, (c,s,i) => x => ((CustomComponentB)c).sampleVector3 = x),
  74.                 new PlugDataTween("ce9f06a4-17ba-45ed-bc10-49ab10cbd9a4", "sampleColor", (c,s,i) => ()=> ((CustomComponentB)c).sampleColor, (c,s,i) => x => ((CustomComponentB)c).sampleColor = x)
  75.             );
  76.         }
  77.  
  78.         // CUSTOMIZATION END ████████████████████████████████████████████
  79.         // ██████████████████████████████████████████████████████████████
  80.    
  81.         return null;
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement