JetBrains / resharper-unity

Unity support for both ReSharper and Rider
Apache License 2.0
1.21k stars 134 forks source link

When calling GameObject.AddComponent<Foo> offer to create a new MonoBehaviour #2

Open justinlueders opened 9 years ago

justinlueders commented 9 years ago

if Foo does not exist (or cant be found and used) offer to create a new class that derives from MonoBehaviour and has a start and update method defined, much like when creating a script inside of the Unity3D editor.

gazialankus commented 8 years ago

Agreed! For me, trying to call gameObject.GetComponent() and letting ReSharper create Foo would be awesome.

cmcpasserby commented 8 years ago

i like this since it is consistent with how jetbrains products handle things when you when do something like try to create a instance of a class that does not exist. Problem is how to do so. In the case of it create a class if you type new MyClass() it creates it in the same file you are in. This would not work for components since unity requires them to be in their own files.

citizenmatt commented 8 years ago

Would this simply be a case of creating a new class that derives from MonoBehaviour? Why would Unity require this to be in a separate file?

cmcpasserby commented 8 years ago

its a new class that Derives from MonoBehaviour, but Unity Requires that all MonoBehaviours be in their own file that has the same filename as the class name.

I feel if you felt it was worth the work, the best approach would be to create the new file, in the same folder as the current file.

alexzzzz commented 8 years ago

I prefer to derive all the components from my custom ScriptBase class rather from MonoBehaviour directly. The feature will be useless for me unless the base class name could be customized.

citizenmatt commented 8 years ago

It would most likely create a new class with a template style "hotspot" for the base class name, defaulted to MonoBehaviour. Does that sound about right?

alexzzzz commented 8 years ago

Sounds perfect.

WeslomPo commented 7 years ago

Unity attachs components to gameObject by guid in meta files, it has only one per file, thats why mb need to be in separated files.

citizenmatt commented 6 years ago

This actually looks more like an issue with the underlying ReSharper engine. I've raised a couple of issues - RSRP-470158 and RSRP-470159.

When creating a type from usage in creating a generic class, ReSharper will create a new class that includes the inheritance constraint (e.g. public class Foo : IFoo), but when doing the same from a generic method invocation, it doesn't include the constraint (e.g. it only creates public class Foo).

But fixing this won't quite fix it - ideally, R# should show a code completion list for the inherited type, so AddComponent<T>() where T : Component would initially create public class MyComponent : Component, but the inherited type would be a hotspot that shows code completion when you tab to it, allowing you to select MonoBehaviour.