How to setup namespaces based on folder structure for Unity - c#

I downloaded Unity 20.2 and want to use namespaces for my code. I modified my project settings to this
In the editor I now have this folder structure for my code
The problem is that the created script Test has the namespace Sources although I would expect it to be Sources.Scripts. I also tried Assets.Sources but it still is not able to create Assets.Sources.Scripts from it.
How can I achieve this?

If you create a class in Visual Studio, then it automatically gets the namespace from the folder structure. Like assets.scripts.something. Just create classes in the correct place and you will get all needed namespaces.

I'm going to describe a situation with some packages in a project to explain this:
Imagine a project in Unity3D with a directory structure like this:
Assets
Resources
Scenes
Source/Scripts
UI
Buttons
Modals
Namespaces convention in this case is driven by the programming language used (C#) in the project (First letter in uppercase, matching the name of the folders).
I suggest you to ignore the directory path until the "Source" or "Scripts" folder when you are choosing the namespaces of your packages. Then in this project we would have this 2 packages:
ProjectName.UI.Buttons
ProjectName.UI.Modals
Conclusion: Write your own namespaces, because Unity namespaces generation is based on folders structure from root and I think that is not the best if you want to share your code by modules, for example.
Good luck, bro 💚

If you are using Rider , then try this. Resharper has the same option. I don't use neither of them , so , have to use Select(Assets) + Del everytime making a new class.

Related

How to find classes, namespace and project folders in C#

I am handling a big WPF application that contains more than 100 projects and and 100s of classes. To modify some areas, I need to find the xmal files and the right projects, classes and namespace. Unfortunatelly, naming is not always helpful. Is there any helpful options in Visual studio.
You can use the search input in the solution explorer tab on the right
You can use view class diagram for this. You can check how to install and run the component from the link below. I hope it solves your problem.
https://learn.microsoft.com/en-us/visualstudio/ide/class-designer/how-to-add-class-diagrams-to-projects?view=vs-2019

Change complete project namespace, assemblies, folders and references

I have a big solution with about 250 projects.
I need to change the prefix of solution (my company name) from "X" to "Y".
Of course some of the projects' names start with the prefix - X.Utilties, X.Dal...
Means I should change project namespaces, assemblies, folders and references and even injection (IoC).
What is the best and safest easy way to do it?
I´d suggest getting the trial version of Resharper and using the available refactoring tools to change all namespaces.
From the top of my head you´d do it this way:
Manually edit your project files to have the new name (or do it with find/replace)
Reopen the solution in visual studio
Right click on the solution > Refactor > Adjust namespaces

how to use same name class libraries on a solution

Need to add two same name .csproj class libraries in my solution.Have two project but unfortunately those project class libraries names are same,like: Hello.csproj.I try to add existing project on solution then show me error
http://blogs.msdn.com/b/ansonh/archive/2006/09/27/774692.aspx
from above url I learned how to use same namespace dll on same project ,but I need help how to use same classlibraries on a solution
if have any query please ask,thanks in advanced.
Note:ok people want to know the reason,i have two project on Autocat 2005 and 2010,now want to merge those project on one solution,2010 update base on 2005 so class libraries are same,but i need to use both of them.So problem arise and seeking help.
You can have projects with the same name as long as they are already created in different folders and they are in different solution folders. If the projects are already created, do this to add them to your solution:
Add your first project to the solution.
In Solution Explorer window, right click your solution and select Add->New Solution Folder
Give a name to the newly created folder.
Right click the folder and select Add->Existing Project
Navigate to your second project and double-click the .csproj file.
You're done.
If you really must do this, then ensure the second project has a different name, and then change the namespaces of the classes in the second project (normally the project name comprises the first part of the namespace - just change that part). The classes will still be identical internally, but because they have a different namespace they will be distinct entities. This will lead to very smelly code though when you start mixing them up in the ClientApp - to avoid confusion make sure you always refer to them by their full namespace (i.e. do not have a using xyz.myclassname; statement at the top of the class file that uses them).
Maybe you want to run two (almost identical) instances of the same service or something, but as mentioned it is hard to think of a genuine reason why you would need to do this. If you are looking to have two identical looking instances but different implementation then you will want to use interfaces instead.
Edit: Visual Studio will not allow you to have two identically named projects, and you are playing crazy games if you change a project name but don't change its project GUID (in the .proj file and the .sln file).
The simplest thing for you to do here is to create a new empty project in the solution explorer, right click on it and Open folder in Explorer, then copy the class files from the original project to the new one, then back in the solution explorer choose Show all files (little button at the top of the solution explorer), then select the newly added files under the new project, right click, Add to project. (These menu options are from memory, they should be roughly right).

Syntax highlighting for non-project files in Visual Studio

I have a simple C# project which loads external C# files at startup to be used as scripts. Unfortunately when editing any of these 'non-project' files in Visual Studio I only get the most basic of syntax highlighting, since classes and types within the project are not known in the context of this external file.
Without adding the files to my project (defeating the purpose of them being external scripts), is there any way I can define an external interface or somehow otherwise convince Visual Studio (2008) to parse the code within these files in the context of the classes in the project?
A couple of clarifications (with thanks to the early answerers)
People should be able to edit these scripts without access to my source code
People shouldn't have to set up an entire Visual Studio project to edit one source file that's likely to contain less that 10 lines of actual code.
You will always need a reference to these classes. Maybe you can add these files as a link to the project or to a new project with a reference.
Visualstudio needs some informations to accomplish that.
I would think about the Bridge Pattern and you need to add the class body in the same file
http://en.wikipedia.org/wiki/Bridge_pattern
or using mock objects- you can easily use them to provide syntax highlighting without sharing your code (the same here - all in one file):
http://en.wikipedia.org/wiki/Mock_object
You can separate the script and the assiting classes if you would allow having a project file.

C# Project Standard Directory Layout

The java build tool "maven" has a standard directory layout for projects. What is the equivalent type of directory layout for a C# project? I'm just learning C# and .NET and it seems that the IDE just dumps everything into one directory, heh. Does C#/.NET follow a convention for the directory structure?
EDIT: This would be for a desktop application (windows forms or wpf). I'm looking at one of these two, but I don't know enough yet to really know which one. Ultimately I would like to learn both.
In C# directory layout should be strictly related to the namespaces you have. Each directory creates subnamespace. Here you may find instructions how to name namespaces properly.
If you use FxCop, it will inform you if your directory (namespace) layout is OK. The main rule is that there shouldn't be namespaces with a few classes/interfaces (FxCop suggests merging two namespaces into one in such a situation).
I think that's all I can say without knowing of some special project type (Uwe mentioned some of them in his comment) you have in mind.
Usually you get the following structure in your project :
/bin/Debug (after a DEBUG build)
/bin/Release (after a Release build)
/obj/Debug
/obj/Release
/Properties (contains AssemblyInfo.cs + Resource files when added via the assembly's properties)
/ServiceReferences (contains service references if any are present in your assembly)
In addition to the above, any solution folders you create (via r-click solution explorer -> add new / folder) will be present in your project's folder.
Does this help?

Categories

Resources