Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Jammyo · Aug 04, 2017 at 12:26 PM · buildbug-perhapscompile-errormenuitem

Custom Build step before compilation

Now that Unity has upgraded .NET support to 4.6, I am looking to update my package dependency process which I setup using this great guide: http://www.what-could-possibly-go-wrong.com/unity-and-nuget/.

While I'm at it, I thought I would update the process to make it easier to use. Since Unity doesn't respect NuGet conventions which download .dll's for each .NET runtime which a package supports, I can't simply tell NuGet to install to Assets/Packages because (for example) NewtonSoft.Json downloads multiple .NET versions like this:alt text

Unity (understandably) complains that an assembly `Newtonsoft.Json' with the same identity has already been imported. I thought that a simple solution would be to write a custom build step which simply loops through all dependencies in the default NuGet storage location (./packages), and copies the relevant folder to ./Assets/Packages. This way we don't need to store NuGet dependencies in source control, and both NuGet and Unity stick with the default conventions.

Unfortunately it seems that Unity cannot execute custom build steps if there is a compilation error. I have tried creating a class which implements the build hook interfaces:

 public class NuGetBuildProcessor : IPreprocessBuild, IActiveBuildTargetChanged, IProcessScene
 {
     public int callbackOrder => int.MinValue;
     public void OnProcessScene(UnityEngine.SceneManagement.Scene scene)
     {
         Debug.Log("OnProcessScene");
     }
 
     public void OnActiveBuildTargetChanged(BuildTarget previousTarget, BuildTarget newTarget)
     {
         Debug.Log("OnActiveBuildTargetChanged");
     }
 
     public void OnPreprocessBuild(BuildTarget target, string path)
     { 
         Debug.Log("OnPreprocessBuild");
     }
 }

But it seems that none of these callbacks execute if there is a compilation error. The only exception to this rule is if this class is created before the compilation errors are introduced in which case I guess it enters the Unity cache until Unity is closed. This doesn't help me because it means that a team-mate checking out new changes for the first time needs to remove all the code with compilation errors in order for the script to run.

The other thing I tried was to add a MenuItem (a less desirable but acceptable solution) so that the user can manually trigger the NuGet restore+copy. It seems that custom menu items also fail to load though if Unity starts up with a compilation error.

Is there another build hook which I'm missing? Is this a bug? To my mind a 'build' represents every step required to turn text-based code files into an executable. With this in mind, I feel that something called 'PreprocessBuild' would be a pre-compilation step, followed by compilation, assembly and finally post-processing (and I imagine many steps in between).

newtonsoft.png (17.2 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jammyo · Aug 08, 2017 at 09:54 AM 0
Share

I think I've found a solution by using the [InitializeOnLoad] annotation. This executes before compilation and allows me to do some extra steps. I'll post back if I get something working.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Simon-O · Oct 27, 2020 at 05:55 PM

It seems that custom menu items also fail to load though if Unity starts up with a compilation error.

That's because Unity compiles the assembly, then loads it to read classes.

So... Put your helper utilities/menu items in their own asset bndle?

The bundle should load even if your main project fails to compile

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

72 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Unity changes the collisionDetectionMode in builds 1 Answer

Unity project wont build 0 Answers

issue with mouse input in windowed mode (Bug ?) 1 Answer

MenuItem Priority is not being considered when itemName has a space 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges