Domanda

I just started using Reshaper today, and to get a lot of warnings about unused methods provided by Unity3D, like Awake(), Start()and Update() etc. Those are called by Unity3D with Reflection, Resharper cannot know that, and therefore warns about them.
Is there a better way to suppress those, then just disabling the whole "type or method is never used" warning, or adding the [UsedImplicitly] attribute to everything?

È stato utile?

Soluzione

ReSharper 10 has a 'Unity Support' extension that does a great job at marking Unity's implicit MonoBehaviour functions and public variables as 'used'.

https://github.com/JetBrains/resharper-unity

You can install it direclty from inside Visual Studio:

ReSharper > Extension Manager... > Search for 'Unity' > Click 'Install' on 'Unity Support'

Altri suggerimenti

As @BlueRaja comments - you've pretty much listed all possible solutions. The best solution is to indeed use the [UsedImplicitly] attribute, which is a part of ReSharper Annotations. However, instead of applying it in code to your types, it would be best to annotate the base types (I'm guessing MonoBehavior? I'm sorry, I'm not familiar with Unity3D API). This way, every type you implement that derives from those, will automatically inherit the [UsedImplicitly] attribute.

The best and non-intrusive way this could be done is by External Annotations - same as code annotations, but applicable externally via XML.

There are 3 choices here: either you do this yourself (take a look at ExternalAnnotations directory, located either at

%localappdata%\JetBrains\ReSharper\vAny\packages\ReSharper.ExternalAnnotations...

since ReSharper 8.2, or

C:\Program Files (x86)\JetBrains\ReSharper\vX.X\ExternalAnnotations

in previous versions)

You can find examples of external annotations XMLs, and create one yourself, following the same naming conventions. To help you with copying method names in the correct format, you could use Copy XML-Doc ID to Clipboard option under the Edit menu in ReSharper.

This way you can annotate the Unity3D base classes, and then distribute this XML as a ReSharper extension for others' benefit.

(the other two things you can do is either convince Unity3D to do this themselves, or open an issue on ReSharper's bug tracker, and maybe JetBrains will do that. But your best bet is to do this yourself).

Hope this helps.

I usually just make them public, downside being that they then show up in the list of methods you can call from other scripts. The methods will probably also still show up grey (as in not called anywhere from within your solution) but at least you won't get the warnings any more.

The annotations thing never works for me, so this is my workaround.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top