https://learn.microsoft.com/zh-cn/dotnet/csharp/roslyn-sdk/source-generators-overview

语法分析 (Roslyn API) 入门 | Microsoft Learn

https://github.com/dotnet/roslyn/blob/main/docs/features/source-generators.md

roslyn/source-generators.cookbook.md at main · dotnet/roslyn · GitHub

https://blog.csdn.net/weixin_39977488/article/details/110123441

https://docs.unity3d.com/Manual/roslyn-analyzers.html

Source Generator实战 – 知乎 (zhihu.com)

好代码是管出来的——.Net中的代码规范工具及使用 – 7m鱼 – 博客园 (cnblogs.com)

How to Debug C# 9 Source Code Generators – Nick’s .NET Travels (builttoroam.com)

Incremental Roslyn Source Generators: Using 3rd-Party Libraries – Part 6 – Thinktecture AG

microsoft.codeanalysis.diagnostics

https://learn.microsoft.com/en-us/dotnet/csharp/roslyn-sdk/tutorials/how-to-write-csharp-analyzer-code-fix

How to write a Roslyn Analyzer – .NET Blog (microsoft.com)

Writing a Roslyn analyzer – Meziantou’s blog 重点推荐

已知问题 :

vs2022中,只有重新打开才会生成(已经解决)
vs2022中,只有重新打开才能查看新生成的代码。

一、Unity中的使用流程

遇到的问题:
1、生成的代码,在VS里面没有提示,但在unity中编译没问题。
2、生成的代码存放位置还没找到。Unity官方文档不太全。

1、新建一个库项目

项目配置(严格一样)如下:

<PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="System.Text.Json" Version="7.0.1" />
	<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.8.0-5.final" />
    <PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.3">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
  </ItemGroup>
  1. In Visual Studio, create a .NET standard library project that targets .NET Standard 2.0. (适用3.8)
  2. Install the Microsoft.CodeAnalysis NuGet package. Your source generator must use Microsoft.CodeAnalysis 3.8 to work with Unity.
  3. In your Visual Studio project, create a new C# file and add the following code:
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
using System.Text;
namespace ExampleSourceGenerator
{
    [Generator]
    public class ExampleSourceGenerator : ISourceGenerator
    {
        public void Execute(GeneratorExecutionContext context)
        {
            System.Console.WriteLine(System.DateTime.Now.ToString());

            var sourceBuilder = new StringBuilder(
            @"
            using System;
            namespace ExampleSourceGenerated
            {
                public static class ExampleSourceGenerated
                {
                    public static string GetTestText() 
                    {
                        return ""This is from source generator ");

            sourceBuilder.Append(System.DateTime.Now.ToString());

            sourceBuilder.Append(
                @""";
                    }
    }
}
");

            context.AddSource("exampleSourceGenerator",
SourceText.From(sourceBuilder.ToString(), Encoding.UTF8));
        }

        public void Initialize(GeneratorInitializationContext context) { }
    }
}

2、将生成的DLL放入Unity项目并设置

  1. Build your source generator for release. To do this, go to Build and select the Batch Build option.
  2. In your source generator’s project folder, find the bin/Release/netstandard2.0/ExampleSourceGenerator.dll file.
  3. Copy this file into your Unity project, inside the Assets folder.
  4. Inside the Asset Browser, click on the .dll file to open the Plugin Inspector
     window.
  5. Go to Select platforms for plugin and disable Any Platform.
  6. Go to Include Platforms and disable Editor and Standalone.
  7. Go to Asset Labels and open the Asset Labels sub-menu.
  8. Create and assign a new label called RoslynAnalyzer. To do this, enter “RoslynAnalyzer” into the text input window in the Asset Labels sub-menu. This label must match exactly and is case sensitive. After you create the label for the first analyzer, The label appears in the Asset Labels sub-menu. You can click on the name of the label in the menu to assign it to other analyzers.
  9. To test the source generator is working, create a new C# script in the editor with the following code:
using UnityEngine;

public class HelloFromSourceGenerator : MonoBehaviour
{
    static string GetStringFromSourceGenerator()
    {
        return ExampleSourceGenerated.ExampleSourceGenerated.GetTestText();
    }

    // Start is called before the first frame update
    void Start()
    {
        var output = "Test";
        output = GetStringFromSourceGenerator();
        Debug.Log(output);
    }
}
  1. Add this script to a GameObject in the scene and enter Play mode. You should see a message from the source generator in the Console window, including the time stamp.

发表评论

电子邮件地址不会被公开。 必填项已用*标注