I need to create snapshots of a complex Transform hierarchy. About 2000 Transform components. After I changed the positions/orientations/scales of some of them, I want to save a snapshot. Then I want to scrub through a timeline of snapshots with a slider, and I need to be able to change the order of the snapshots.
The problem is that setting the transforms of 2000 objects takes a lot of time. Saving seems to work instantly.
Does anyone have an idea for an efficient way for making and replaying snapshots?
Basically, writing into Transform.localPosition, Transform.localRotation and Transform.localScale should be faster than writing into Transform.position/rotation.
My suggestion was a guess which is very likely to be a correct. I haven’t verified it.
There shouldn’t be any noticeable overhead for root transforms when accessing Transform.position comapred to accessing Transform.localPosition.
Basically, if you write into “position”, you’re specifying coordinates in world space, and parent transform needs to be known. Then position/rotation will have to be transformed into parent space. This will have some overhead (because you’ll need either multiplication by inverted matrix or an equivalent operation). If the transform is a root transform, there’s no parent, and no need to transform anything.
Traditional advice is that a hand crafted loop can outperform LINQ. Normally I wouldn’t bother to mention it. But since you are having performance issues, its worth considering.
Its the filtering that’s producing the speed up. The OP is now doing the work on 100 transforms instead of 2000. Which is a good optimization.
Making the filter more efficient might be useful, or it may be a waste of time. Its entirely possible the performance gain is minimal compared to not using a filter at all. But it is something worth checking in the profiler.