Create your own springs (extend the asset)

The toolkit allows advanced users to extend its functionality by creating custom SpringComponents tailored to their specific needs. If the existing SpringComponents don't meet your requirements, you can create your own by inheriting from the SpringComponent class.

To create your own SpringComponent, follow these steps:

  1. Create a new script: Start by creating a new script that inherits from SpringComponent. This will be the foundation of your custom component.

  2. Declare your Springs: In your custom SpringComponent class, declare the Spring(s) that you will use in the component. For example, you might declare a SpringFloat like this: public SpringFloat mySpringFloat;

  3. Register Springs: Implement the RegisterSprings method and call RegisterSpring(spring) for each spring you declared. This ensures that the asset manages the lifecycle and update of every spring.

  4. Implement IsValidSpringComponent(): This method should return true or false depending on whether the component is properly set up. It's used to ensure that all necessary references are present and that all configurations are correct. If false is returned, the SpringComponent is automatically disabled.

  5. Set Initial Values: In the SetInitialValues() method, set the initial values of your springs. This step is crucial, as otherwise, the springs will start at 0. You can use any logic to handle the initial values here. You can also make use of hasCustomInitialValues and hasCustomTarget that are part of SpringComponent. These booleans are used in the toolkit included implementations and you may want to use them too to branch the logic and set some other custom value such a serialized field when true. This approach provides flexibility in initializing your springs, allowing you to adapt to different scenarios and requirements within your custom SpringComponent.

  6. Update Logic: In the Update() method (or any other method you prefer), implement the logic to use your springs. This is where you'll update values based on the registered springs' current values and apply them to your game objects or components.

By following these steps, you can create custom SpringComponents that integrate seamlessly with the toolkit. This allows you to leverage its powerful spring functionality while tailoring it to your specific project requirements.

For a practical example, you can refer to the NewSpringComponentExample.cs class included in the toolkit, which demonstrates these steps in action and documents the process.

Last updated