UnityCoroutinesAnalyzer 1.0.3.5

dotnet add package UnityCoroutinesAnalyzer --version 1.0.3.5                
NuGet\Install-Package UnityCoroutinesAnalyzer -Version 1.0.3.5                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="UnityCoroutinesAnalyzer" Version="1.0.3.5" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add UnityCoroutinesAnalyzer --version 1.0.3.5                
#r "nuget: UnityCoroutinesAnalyzer, 1.0.3.5"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install UnityCoroutinesAnalyzer as a Cake Addin
#addin nuget:?package=UnityCoroutinesAnalyzer&version=1.0.3.5

// Install UnityCoroutinesAnalyzer as a Cake Tool
#tool nuget:?package=UnityCoroutinesAnalyzer&version=1.0.3.5                

A set of roslyn analyzers for convinient use of Unity coroutines

Git Hub repo

Analyzers

1. Coroutine invocation analyzer

As You know there is few ways to start a coroutine in unity:

  • Passing coroutine name:

      StartCoroutine("Coroutine");
    
      IEnumerator Coroutine() {}
    
  • Executing coroutine method:

      StartCoroutine(Coroutine());
    
      IEnumerator Coroutine() {}
    
Problems

Using the first one it is easy to make a mistake in coroutine name when writing string literal.

And if you would use the second one, you won't be able to stop the coroutine. Because you are creating an independent coroutine instance.

Solution
  • The first analyzer encourages you to start the coroutine using 'nameof' syntax:

      StartCoroutine(nameof(Coroutine));
    
      IEnumerator Coroutine() {}
    

    So now you are able to call 'StopCoroutine' method with the expected result and avoid excessive mistakes.

2. Yield return in while(true) block

Coroutines in Unity are often used in combinations with while(true) block:

  IEnumerator Coroutine()
  {
    while(true)
    {
      ...
    }
  }
Problems

Although it is easy to forget to put yield return statement inside while(true) that leads to Unity crash, Visual Studio doesn't have any embedded analyzers that prevents you from such problems. So the code above won't trigger any VisualStudio warnings or
recommendations.

Solution

Code analyzers in this package would warn you about possible issues and suggest to put yield return null; on the end of while block:

  IEnumerator Coroutine()
  {
    while(true)
    {
      ...
      yield return null;
    }
  }
There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.3.5 420 11/4/2021
1.0.0 391 10/29/2021

Yield return in coroutines analyzer