How to: Using Sequence.Create()
, Instantiating GameObject
with delay then animating them, destroying and repeat N number of time
#43
-
How may we create one
Using |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
To create a Sequence, you should first create all objects the Sequence will animate. So I'm afraid the concept of creating objects on the fly and then animating them doesn't work well with sequences. In addition, Object.Instantiate() and Object.Destroy() are heavy operations that may lead to fps drops, so I can't recommend this approach either. What you can do is to create these objects in advance, then enable/disable them with GameObject[] gos;
public Sequence AnimateGameObjects() {
if (gos == null) {
gos = new GameObject[25];
for (int i = 0; i < gos.Length; i++) {
var go = GameObject.CreatePrimitive(PrimitiveType.Cube);
go.SetActive(false);
go.transform.position = new Vector3(i, 0f);
gos[i] = go;
}
}
PrimeTweenConfig.warnTweenOnDisabledTarget = false;
var seq = Sequence.Create(cycles: 5);
for (int i = 0; i < gos.Length; i++) {
seq.Group(Sequence.Create()
.Group(Tween.Delay(0.05f * i))
.ChainCallback(gos[i], target => target.SetActive(true))
.Chain(Tween.Scale(gos[i].transform, Vector3.one, Vector3.one * 2f, 0.5f)));
}
seq.ChainDelay(5f);
seq.ChainCallback(gos, target => {
foreach (var go in target) {
go.SetActive(false);
}
});
return seq;
} |
Beta Was this translation helpful? Give feedback.
To create a Sequence, you should first create all objects the Sequence will animate. So I'm afraid the concept of creating objects on the fly and then animating them doesn't work well with sequences. In addition, Object.Instantiate() and Object.Destroy() are heavy operations that may lead to fps drops, so I can't recommend this approach either.
What you can do is to create these objects in advance, then enable/disable them with
.SetActive(bool isActive)
. Also, this approach can now be used with a Sequence because all GameObject references are known in advance. Here is the code: