Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Manage Script Templates

Discussion in 'Unity Hub' started by v01pe_, Feb 8, 2018.

  1. v01pe_

    v01pe_

    Joined:
    Mar 25, 2015
    Posts:
    43
    I like to replace the standard script templates with my own and also add a few custom ones. The script templates I'm talking about are those used when selecting e.g. Create -> C# Script from the context menu in the project window, or form the Assets menu.

    They are located inside the Unity install folder at Unity.app/Contents/Resources/ScriptTemplates on OS X and at Editor\Data\Resources\ScriptTemplates on Windows. Therein also lies the problem of modifying them… every time you install a new version of Unity, those are not present any more and have to be copied over to the new install folder, or are lost, if you install over the same folder.

    I wrote a script to place them where they belong for new installs, but through the Unity Hub would be a convenient way to manage these templates and make them available on all Unity versions installed.
     
  2. superpig

    superpig

    Quis aedificabit ipsos aedificatores? Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,146
    What if we made it that you could just put your custom templates in your project folder somewhere instead, so that you didn't need to modify the install?
     
  3. v01pe_

    v01pe_

    Joined:
    Mar 25, 2015
    Posts:
    43
    Actually better would be if they could be put in a user folder like other Unity settings. At least the files that I'm using aren't really project specific. That's p.e. my `81-C# Script-NewBehaviourScript.cs.txt`:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class #SCRIPTNAME# : MonoBehaviour
    4. {
    5.     void Start()
    6.     {
    7.        
    8.     }
    9.    
    10.     void Update()
    11.     {
    12.        
    13.     }
    14. }
    I thought about hooking them up with Unity Hub, because I often work on two different computers and the Hub would be a great way to not only keep them with different Unity versions, but also on different machines if they are synced.

    But I'd already very happy if I could override and add to the default ones by placing them somewhere in my Library folder.
     
    AlanMattano and JimmyCushnie like this.
  4. hadynlander

    hadynlander

    Joined:
    Jan 24, 2014
    Posts:
    26
    I realise I'm late to the party here, but it made more sense to reply to this comment than start a fresh thread.

    Anyway, I'd really like this! It'd be super useful to have template scripts with the correct namespaces baked in for different projects, both as a freelancer (jumping between many projects) and as a team contributor (ensuring consistency across the codebase). I could even see myself creating more specialised templates per project if I wasn't concerned about cluttering up the Create context menu in unrelated projects...

    So that's a resounding yes please from me, if it's not too late!
     
    JimmyCushnie likes this.
  5. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,028
    I'm afraid that if Unity for some technical reason changes the default templates, having my own template, I will not notice the difference and I will lose the upgrade.

    Is annoying searching inside C:\Program Files\Unity\Hub\Editor folder because folders of different Unity versions are not deleted. A fast link to the C# folder of that particular Vr can be a very good starting solution?

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. /********************************************
    4. *
    5. *  2018
    6. *  #SCRIPTNAME#
    7. *  
    8. *      .
    9. *  
    10. *          .
    11. *
    12. * *****************************************/
    13.  
    14. public class #SCRIPTNAME# : MonoBehaviour
    15. {
    16.     #region VARIABLES
    17.     [Space][Header("#SCRIPTNAME#")]
    18.     [Space(25)]
    19.     [Space]
    20.  
    21.     // PUBLIC
    22.     public bool consoleDebug;
    23.  
    24.     // PRIVATE
    25.     #endregion
    26.  
    27.     void Start ()
    28.     {
    29.         #NOTRIM#
    30.     }
    31.  
    32.     void Update ()
    33.     {
    34.         #NOTRIM#
    35.     }
    36. }
    37.  
    38. /*
    39. using System.Collections;        // For sub rutine & making list
    40. using System.Collections.Generic;    // for making list
    41. using UnityEngine.UI;
    42. using UnityEngine.EventSystems;        // pass on what the button do when is click
    43.     [Range(0, 3.5f)]
    44.     [Tooltip("Info")]
    45.     [Space][Header("INPUTS")][Space]
    46.     [Space][Header("DRAG & DROP")][Space]
    47.     [Space][Header("OUTPUTS")][Space]
    48.     if (consoleDebug) Debug.Log("" + "\n");
    49.     void OnGUI(){GUILayout.TextArea("text " + VARIABLE.ToString() );}
    50.     GUIStyle guiStyle = new GUIStyle();
    51.     void OnGUI()
    52.     {
    53.         guiStyle.fontSize = 50;
    54.         guiStyle.normal.textColor = Color.white;
    55.         GUI.Label(new Rect(10, 10, 100, 20), "Notas: " + myVar1, guiStyle);
    56.         GUI.Label(new Rect(10, 65, 100, 20), "Infos: " + myVar2, guiStyle);
    57.     }
    58. /*------------------------------- FUNCTIONS -----------------------------*/
    59.  
     
    Last edited: Jun 13, 2018
  6. v01pe_

    v01pe_

    Joined:
    Mar 25, 2015
    Posts:
    43
    I know this is already kind of an old thread, but here and then I think about it and the more I do so the more I like the idea of being able to put the templates into a folder somewhere inside the project. This way one could add even more specialised presets that only make sense for that project.

    Additionally it would still be nice to have a collection of template overrides that I want to include in every new project. This could be solved by a more general approach by defining custom project presets like "2D" and "3D" that already exist as dropdown in the new project view!
     
    hadynlander likes this.
  7. hadynlander

    hadynlander

    Joined:
    Jan 24, 2014
    Posts:
    26
    It would also be useful for students at schools and universities who don't have permissions to access the Unity install directory, or for people working in offices with shared machines, etc.

    I used to teach at a uni where both of those situations applied, and it was annoying enough having the default script template with curly braces on the same line as declarations... which was bad not because one standard's better than another (that part of the argument is entirely subjective / pointless), but because it confuses newbies with a standard that differs from almost every other code example they'll ever find in the Unity API / Unity Manual / MSDN / DotNetPerls / etc...
     
  8. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    3,836
    I did some major sleuthing through Unity's csharp reference over the weekend, and I baked up Unity-Script-Templates, which is a little plugin to create custom script templates, and edit the built-in ones though an editor window.

    Editing the built-in ones requires you to launch Unity as root/Administrator, as it's just a simple File.Write operation, so it won't solve @hadynlander's problem. That being said, it's a pretty good stop gap measure until we get an official solution.
     
  9. hadynlander

    hadynlander

    Joined:
    Jan 24, 2014
    Posts:
    26
    Nice one @Baste! I'll try to give it a shot later this week :)
     
  10. sand_lantern

    sand_lantern

    Joined:
    Sep 15, 2017
    Posts:
    48
    As usual @Baste, thank you for your contributions to the Unity Community. That said, it seems like we shouldn't need to rely on the goodwill of users for something basic to be integrated into Unity Hub.

    Can we get an official statement on if this is still on the radar? It's pretty obnoxious having to copy my script templates every time I update my unity version (Right now it's weekly since I'm running on the beta builds).
     
  11. snusfen

    snusfen

    Joined:
    May 20, 2015
    Posts:
    39
    Ping @superpig

    Any update on the above? It would be a super duper feature
     
  12. superpig

    superpig

    Quis aedificabit ipsos aedificatores? Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,146
    No news on this, sorry.
     
  13. sand_lantern

    sand_lantern

    Joined:
    Sep 15, 2017
    Posts:
    48
    That's very disappointing.
     
  14. matazematratze

    matazematratze

    Joined:
    May 3, 2015
    Posts:
    9
    Would you be willing to share that? Interested in both - Mac and Win. Much appreciated!
     
    v01pe_ likes this.
  15. rmb303

    rmb303

    Joined:
    Jan 6, 2016
    Posts:
    18
    I'd be really happy with this too.
    Whenever I've downloaded an update or new Beta, I always get annoyed when creating a script, and remembering that I've got to copy my edited versions into the script templates folder within the app.
    Having the ability to specify a folder where these are kept for all versions would save this unnecessary hassle!
     
  16. sand_lantern

    sand_lantern

    Joined:
    Sep 15, 2017
    Posts:
    48
    As it stands, it's almost a "useless" feature, being able to customize scripts. At some point, someone in the project won't realize they lost their script templates and the entire benefit of the templates will be left moot. But maybe this is indicating that templates should be project specific rather than editor specific...
     
  17. v01pe_

    v01pe_

    Joined:
    Mar 25, 2015
    Posts:
    43
    Here's an OS X version, I have no Windows equivalent unfortunately… maybe someone wants to port? :)
    Manual:
    • To use this, you need a folder next to the script called unity templates. Inside this folder you have your custom scripts. If you want to replace built in scripts, name them the same as the original ones.
    • If you moved your Unity folder, you can pass it in as argument.
    • When executing the script for the first time, a backup .zip file is made in the templates folder.
     
  18. Kreep

    Kreep

    Joined:
    Aug 1, 2013
    Posts:
    8
    Would be a killer feature for productivity. Unity should spend some time on minor features that can lead in major benefits for teams.
     
    hadynlander and sand_lantern like this.
  19. snarlynarwhal

    snarlynarwhal

    Joined:
    Jan 16, 2014
    Posts:
    23
    This would be super helpful.
     
  20. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    3,836
    Actually this has been implemented! I don't know when, and I don't know if it happened after this thread happened. Here's @jura_z's instructions:

    I recommend just copy-pasting the entire ScriptTemplates folder from the install folder to Assets, so you get all of them, and changing them as you see fit.
     
    sand_lantern and hadynlander like this.
  21. jura_z

    jura_z

    Unity Technologies

    Joined:
    Oct 10, 2016
    Posts:
    22
    This feature was there since the beginning of Unity, but a bit broken. I fixed it in 2017.1. It is still not ideal, but at least something.

    >I recommend just copy-pasting the entire ScriptTemplates folder from the install folder to Assets, so you get all of them, and changing them as you see fit.

    You can also create new ones. But you have to restart Unity Editor on each change
     
    sand_lantern and hadynlander like this.
  22. v01pe_

    v01pe_

    Joined:
    Mar 25, 2015
    Posts:
    43
    Huh! Interesting, and good to know!
    Because you recommend to copy paste all scripts… does that mean, that the built in ones disappear if this folder is present?
     
  23. v01pe_

    v01pe_

    Joined:
    Mar 25, 2015
    Posts:
    43
    If enough people are interested I could extend my script to include the option to copy the script templates to a project folder (optionally including the original ones)
     
  24. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    3,836
    Nice. It seems like the location of the pre-existing ones are hard-coded, and we can't prefix out own with number to influence the MenuItem index, nor put them in fold-outs in any way. Still, as you say, it's something.

    Does the reloading on restart happen on the engine side, or is there some internal refresh call or list we could reflect into to update the scripts?
     
  25. superpig

    superpig

    Quis aedificabit ipsos aedificatores? Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,146
    It's on the engine side.

    FWIW, I'm currently working on exposing the API needed to let you write an Editor script to create a new asset from a template (i.e. the thing that actually powers ScriptTemplates). When I'm done, you will be able to just write a regular static method tagged with the [MenuItem] attribute, and have it create new scripts based on templates that you store wherever you want, instead of relying on a magic 'ScriptTemplates' folder. We probably don't want to keep the ScriptTemplates folder itself in the long run.
     
    AndreasO, sand_lantern and Baste like this.
  26. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    3,836
    Any chance you'd improve the handling of the menu layout with eg. [MenuItem]? I'd love to be able to specify that my new "Create/C# ScriptableObject script" should be next to "Create/C# Script", but right now that requires figuring out the priority value I need to give [MenuItem] through trial and error.

    I mean, I guess that "Create/C# Script" is 81 since the script is named that, but this isn't the only instance where I want one menu-item to be next to a different one.
     
    hadynlander likes this.
  27. superpig

    superpig

    Quis aedificabit ipsos aedificatores? Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,146
    It's out of scope for what I'm doing at the moment, but I can certainly imagine improvements to MenuItem being made later (and they'd have much wider benefits than anything specific to ScriptTemplates).
     
    Baste likes this.