Skip to content

Tween/Driver script added even after Stop game in Editor during async method (actually Unity's limitation) #27

@hsandt

Description

@hsandt

Describe the bug
If adding Tween as part of an async method, and stopping the game while it's still running, the async method will keep running, possibly running tweens and adding dirty Tween Driver components on game objects in the scene, that will stay and be saved in the scene.

Sometimes, you'll also see error messages about runtime script on that game object, it's related to the Driver leftover, as removing it removes the error messages.

GameObject (named 'Team Logo Text (TMP)') references runtime script in scene file. Fixing!

Unfortunately, this is due to a limitation of Unity that will keep running all async methods after Stop in Editor, as they are not really controlled, and the user may want to use this behaviour for Tool functionality in the Editor.

See: https://forum.unity.com/threads/non-stopping-async-method-after-in-editor-game-is-stopped.558283/

Since there is not much to do to stop this behaviour without adding code on the project itself (see the 2 workarounds in the thread above), I'm only suggestion to, at least, check for if (Application.isPlaying) before adding a component to the game object, the only effect that really bothers me after stopping the game. Obviously, I put more complex stuff in my async method, that will run too and I may have troubles anyway, but at least I'll be able to avoid the unwanted runtime components sticking to my game objects and being saved in my scene.

To avoid extra code in Builds, you may surround the isPlaying check with #if UNITY_EDITOR

Tested with Graphic Alpha Tween only.

To Reproduce
Steps to reproduce the behavior:

  1. Create a new project with package "git+https://github.com/jeffreylanters/unity-tweens"
  2. Create script SplashScreenManager with multiple tweens chained:
using System.Threading.Tasks;
using UnityEngine;

using ElRaccoone.Tweens;

/// Splash Screen Manager
public class SplashScreenManager : MonoBehaviour
{
    [Header("Scene references")]
    
    [Tooltip("Team logo")]
    public GameObject teamLogo;


    private async void Start()
    {
        await PlaySplashScreenSequence();
    }

    private async Task PlaySplashScreenSequence()
    {
        // fade-in
        await teamLogo.TweenGraphicAlpha(1f, 1f).SetFrom(0f).Await();
        await Task.Delay(Mathf.RoundToInt(1000 * 1f));
        // fade-out
        await teamLogo.TweenGraphicAlpha(0f, 1f).Await();
    }
}
  1. Add it to some SplashScreenManager game object
  2. Create Image (it will automatically be added on a Canvas) "TeamLogo" and set a dummy sprite like "Background" on it, just so it's visible.
  3. Run the game, and try to stop the game at different moments. If you stop if right after fade-in is over (after 1s), it should pop a Driver (meant for the following fade-out) on TeamLogo, that sticks in the scene.

Expected behavior
No Driver should be added outside Play

(or they should be removed automatically by some editor script; they probably are on game quit actually, it's just that another one is re-added afterwards)

Screenshots

image

and if you repeat the operation:

image

Desktop (please complete the following information):

  • OS: Linux Ubuntu
  • Unity: 2021.1.14f1
  • Tween code version: v1.11.1

Additional context
I'm testing my splash screen as you may have guessed, after seeing many errors on runtime scripts I spot the issue.

Workaround
Use coroutines instead of async methods...

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions