Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Create a namespace for Unity (For all the new projects, like a global namespace).

Discussion in 'Scripting' started by JavierTomas, Aug 31, 2016.

  1. JavierTomas

    JavierTomas

    Joined:
    Aug 31, 2016
    Posts:
    9
    Hi, I was wondering if it possible to create a global namespace which I can use in every new Unity project that I create.

    I try it like this :

    1º Create a namespace C# in Visual Studio , (lets call it namespace Libbrary)
    2º Build it like managed dll.
    3º Drag the namespace dll to the Unity root folder ( C:\Program Files\Unity\Editor )

    Once done this, whenever I create new project in Unity and I start to write some C# script in Visual Studio ,I write on the top the reference to my new namespace created "using Libbrary; " , but it doesnt work since Visual Studio tells me: "the namespace name Libbrary could not be found". So I maybe I need to drag the dll in another local folder, I dont really know.
    Of course If I copy the managed namespace dll to my Folder current Project it works, but it is not my goal.
    My goal is whenever I create a new project in Unity and I try to create a New C# Script, I will be able to use my new namespace.

    Thanks.
     
  2. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    The DLLs in the Unity\Editor\Data\Managed\ are actually duplicated into each new Unity project (not the whole directory I don't think, as you might want, but explicitly), so the ones that are loaded in your current project and Visual Studio are actually in Unity Projects\Project Name\Library\UnityAssemblies\. That said, I have no idea if it'll work just by dropping a new DLL in there, and it won't make it work for all Unity projects, just the one that you drop it into, so the best you could hope for is the exact same functionality that you get from importing it into your project view directly, but hidden in the background. *shrugs*

    In order to get a DLL to auto-copy into every new Unity project automatically, you'd likely have to edit the core code in Unity itself, so I don't think it'll happen. Sorry.
     
  3. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Looks like I was mistaken about some of that. I'm looking into it though *curious*.
     
  4. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    I guess the best thing you can do is create a batch job to create a project and copy the DLL into the assets folder automatically. A starting point would be to read up on the Command Line arguments Unity supports.
     
  5. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Yeah, I'm not finding any way to accomplish this. Aside from the UnityAssemblies bit in my first post, which from what I can tell is actually some sort of cache and which doesn't even exist until you go to build a project, everything I said appears to be more or less accurate.

    The DLLs that Unity use internally are the managed code, but each of them have un-managed portions (in C++) that aren't accessible there. This tells me that every DLL in the Data\Managed\ folder is explicitly loaded when the program starts and not just imported en-masse from the folder, which is the only way you'd be able to worm your own library into it. This is seemingly confirmed by the fact that I just dumped a couple of my own DLLs in that folder and created a new project, but couldn't access the namespaces.

    Because the UnityAssemblies folder is only a cache, you can't even drop your DLL in there (not that it would really solve your problem, but I was hoping at least it might be an "in the background" sort of option)- you'll have to put it right in the Assets folder instead.
     
    Last edited: Aug 31, 2016
  6. Brominion

    Brominion

    Joined:
    Sep 30, 2012
    Posts:
    48
    Not sure if this is what you actually want but:
    If you export an Asset Package with your desired setup and move it to the correct folder. It will show up as an option in the assets menu for new projects. It should be possible to make some kind of "default setup" asset including the DLL, folder structures etc. (admittedly I am not sure I have tested specifically for namespace access for DLL but assets with DLLs should work normally).

    It will require ticking the box for the asset for new projects (could be good if you have more than one?), and will create a copy in the project folder.
    I am not sure how far you can stretch this solution with regards to various other project settings.

    I have been placing various re-use assets in the same folder that the Asset store uses (because it worked..), but I am guessing there may be another intended place for this.​
     
  7. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    The UnityEngine stuff will always be global. It would be nice, however to have a Project Setting much like Visual Studio does for .csproj's and be able to specify a "Default Namespace" which would be used for creating new scripts (this is now VS handles it).
     
    Brominion likes this.
  8. Brominion

    Brominion

    Joined:
    Sep 30, 2012
    Posts:
    48
    I think I missed the original question. Got focused on accessing DLLs, and default includes rather than creating your own default namespace. =)
     
  9. IsaiahKelly

    IsaiahKelly

    Joined:
    Nov 11, 2012
    Posts:
    418
    You can add whatever code you want to newly created scripts automatically by editing one of Unity's script template files.

    The script templates are located in your Unity install folder under > Editor > Data > Resources > ScriptTemplates (Unity 5.3 on Windows OS).

    For example, locate the C# template named "81-C# Script-NewBehaviourScript.cs.txt" and edit the code to include your name space like so:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. namespace MyNameSpace
    5. {
    6.     public class #SCRIPTNAME# : MonoBehaviour
    7.     {
    8.         // Use this for initialization
    9.         void Start () {
    10.      
    11.         }
    12.      
    13.         // Update is called once per frame
    14.         void Update () {
    15.      
    16.         }
    17.     }
    18. }
    Now any new scripts created in Unity will have your namespace by default.

    Note: You will probably need to change the read/write permissions on the template file before you can save any changes.
     
    WereVarg and Brominion like this.