Skip to content

Instantly share code, notes, and snippets.

@shanecelis
Last active March 26, 2022 12:41

Revisions

  1. shanecelis revised this gist Mar 26, 2022. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions CopyToUnity.csproj_
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    <?xml version="1.0" encoding="utf-8"?>
    <!-- Original code[1] Copyright (c) 2022 Shane Celis[2]
    Licensed under the MIT License[3]
  2. shanecelis revised this gist Mar 26, 2022. 1 changed file with 9 additions and 1 deletion.
    10 changes: 9 additions & 1 deletion CopyToUnity.csproj_
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,12 @@
    <!--
    <!-- Original code[1] Copyright (c) 2022 Shane Celis[2]
    Licensed under the MIT License[3]

    [1]: https://gist.github.com/shanecelis/db73854d7257fdd2a8798d3ba7d4d00f
    [2]: https://twitter.com/shanecelis
    [3]: https://opensource.org/licenses/MIT

    * * *

    CopyToUnity.csproj_
    ===================
    Copy your dotnet libraries to your Unity game project
  3. shanecelis created this gist Mar 26, 2022.
    135 changes: 135 additions & 0 deletions CopyToUnity.csproj_
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,135 @@
    <!--
    CopyToUnity.csproj_
    ===================
    Copy your dotnet libraries to your Unity game project

    Import this file in a dotnet project to automatically copy its `netstandard2.0`
    `.dll` and `.pdb` files to your Unity project when it's built.

    Usage
    =====

    After installation it will run whenever built by default.

    $ dotnet build

    Assuming build includes `netstandard2.0`, the build will copy its `.dll`
    and `.pdb` files to a Unity project's `/Assets/netstandard2.0/$(AssemblyName)/`
    directory. However, it can also be called directly and auto copying can be
    turned off.

    $ dotnet build -target:CopyToUnity

    Installation
    ============

    Choose one of the three cases that best suits your needs.

    ## Case 1: Place this file with a Unity project.

    1. Place `CopyToUnity.csproj_` file in your Unity project's root directory.
    2. Edit the `UnityProjectPaths` in `CopyToUnity.csproj_` to your Unity project's
    path.
    3. Add the following tag to your dotnet's csproj file.^*

    <Import Project="/Users/YOU/YOUR-GAME/CopyToUnity.csproj_"/>

    * Can add multiple import lines for multiple Unity project destinations.

    ## Case 2: Place this file with a dotnet project.

    1. Place `CopyToUnity.csproj_` file in your dotnet project's root directory.
    2. Import `CopyToUnity.csproj_` and add your unity paths in your `.csproj` file
    like so:

    <Import Project="CopyToUnity.csproj_"/>

    <ItemGroup>
    <UnityProjectPaths Include="/Users/YOU/YOUR-GAME1"/>
    <UnityProjectPaths Include="/Users/YOU/YOUR-GAME2"/>
    <UnityProjectPaths Include="/Users/YOU/YOUR-GAME3"/>
    </ItemGroup>

    ## Case 3: Copy and paste this straight into my veins!

    1. Do like I did before I had this file and just copy and paste
    these lines directly into all your spawling `.csproj` files.

    <Target Name="CustomAfterBuild" AfterTargets="Build">
    <ItemGroup>
    <_CustomFiles Include="bin/Debug/netstandard2.0/*.dll" />
    <_CustomFiles Include="bin/Debug/netstandard2.0/*.pdb" />
    </ItemGroup>
    <MakeDir Directories="/Users/YOU/YOUR-GAME/Assets/netstandard2.0/$(AssemblyName)"/>
    <Copy SourceFiles="@(_CustomFiles)"
    DestinationFolder="/Users/YOU/YOUR-GAME/Assets/netstandard2.0/$(AssemblyName)"
    SkipUnchangedFiles="true" />
    </Target>
    2. Kiss maintainability goodbye!

    Configuration
    =============

    The auto build feature can be turned off and the output directory within the
    Unity project can be changed.

    <PropertyGroup>
    <UnityOutputDir>/Assets/netstandard2.0/$(AssemblyName)/</UnityOutputDir>
    <CopyToUnityAfterBuild>false</CopyToUnityAfterBuild>
    </PropertyGroup>


    NOTES
    =====
    * Extension '.csproj_' chosen so that it won't be confused with the actual
    '.csproj' file by `dotnet build` and such.

    TODO
    ====
    -->
    <Project>

    <PropertyGroup>
    <UnityOutputDir>/Assets/netstandard2.0/$(AssemblyName)/</UnityOutputDir>
    <CopyToUnityAfterBuild>true</CopyToUnityAfterBuild>
    </PropertyGroup>

    <ItemGroup>
    <!-- Case 1. Step 2. Put your game or games here. -->
    <!-- <UnityProjectPaths Include="/Users/YOU/YOUR-GAME1"/> -->
    <!-- <UnityProjectPaths Include="/Users/YOU/YOUR-GAME2"/> -->
    </ItemGroup>

    <Target Name="PreCopyToUnity">
    <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    </PropertyGroup>
    </Target>

    <Target Name="CopyToUnity" DependsOnTargets="PreCopyToUnity;Build">
    <CallTarget Targets="_CopyToUnity"/>
    </Target>

    <Target Name="_CopyToUnity"
    Condition="'$(TargetFramework)' == 'netstandard2.0'"
    Outputs="%(UnityProjectPaths.Identity)">
    <ItemGroup>
    <_CustomFiles Include="$(OutputPath)/*.dll" />
    <_CustomFiles Include="$(OutputPath)/*.pdb" />
    </ItemGroup>
    <ItemGroup>
    <_UnityOutputPaths Include="@(UnityProjectPaths->'%(FullPath)$(UnityOutputDir)')"/>
    </ItemGroup>
    <MakeDir Directories="@(_UnityOutputPaths)"/>
    <Copy SourceFiles="@(_CustomFiles)"
    DestinationFolder="%(_UnityOutputPaths.FullPath)"
    SkipUnchangedFiles="true" />
    </Target>

    <Target Name="CopyToUnityAfterBuild"
    AfterTargets="Build"
    DependsOnTargets="_CopyToUnity"
    Condition="'$(CopyToUnityAfterBuild)' == 'true'">
    </Target>

    </Project>