Add Components at runtime
The asset wasn’t really designed with AddComponent in mind. Adding a SpringComponent in the Inspector, setting it up and optionally referencing it from some other script is always the ideal recommended use case.
The four steps
Section titled “The four steps”But if, for some reason, you want or need to add a SpringComponent at runtime this is how you do it:
- Add a SpringComponent in runtime
- Setup the SpringComponent
- Initialize the SpringComponent
- Use it normally
Examples
Section titled “Examples”Here 2 examples:
colorSpring = gameObject.AddComponent<ColorSpringComponent>();colorSpring.SetAutoUpdateToTrue(isRenderer: false, uiImage.gameObject);colorSpring.Initialize();colorSpring.SetCurrentValue(Color.white);colorSpring.SetTarget(Color.yellow);And:
transformSpringComponent = uiImage.gameObject.AddComponent<TransformSpringComponent>();transformSpringComponent.useTransformAsTarget = true;transformSpringComponent.followerTransform = uiImage.transform;transformSpringComponent.targetTransform = uiImageTarget.transform;transformSpringComponent.SpringScaleEnabled = false;transformSpringComponent.Initialize();You can find the full example from above in:
Assets\Plugins\AllIn1Assets\AllIn1SpringsToolkit\Demo\Scripts\ExampleAddComponent.cs
