Search Unity

Manage Script Templates

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

  1. v01pe_

    v01pe_

    Joined:
    Mar 25, 2015
    Posts:
    66
    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.
     
    HiddenTess, rmb303, cxode and 3 others like this.
  2. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,488
    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:
    66
    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 cxode like this.
  4. hadynlander

    hadynlander

    Joined:
    Jan 24, 2014
    Posts:
    30
    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!
     
    cxode likes this.
  5. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,441
    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:
    66
    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:
    30
    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:
    5,618
    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:
    30
    Nice one @Baste! I'll try to give it a shot later this week :)
     
  10. sand_lantern

    sand_lantern

    Joined:
    Sep 15, 2017
    Posts:
    189
    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:
    62
    Ping @superpig

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

    superpig

    Drink more water! Unity Technologies

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

    sand_lantern

    Joined:
    Sep 15, 2017
    Posts:
    189
    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:
    24
    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:
    189
    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:
    66
    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. Francoimora

    Francoimora

    Joined:
    Aug 1, 2013
    Posts:
    21
    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:
    25
    This would be super helpful.
     
  20. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    5,618
    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.
     
  21. jura_z

    jura_z

    Unity Technologies

    Joined:
    Oct 10, 2016
    Posts:
    25
    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
     
  22. v01pe_

    v01pe_

    Joined:
    Mar 25, 2015
    Posts:
    66
    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:
    66
    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:
    5,618
    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

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,488
    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:
    5,618
    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

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,488
    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.
  28. Darkonekt

    Darkonekt

    Joined:
    Dec 19, 2019
    Posts:
    1
    Any news on this feature? For me putting script templates per project is worst than per installation.... This does not solve the issue. The whole point of custom templates is reusability.... There should be a "per user" folder where to put custom templates.. This could not be that difficult to develop. After all unity already looks into a folder to pull the templates. Can it look at 2 folders?
     
    sand_lantern and v01pe_ like this.
  29. jura_z

    jura_z

    Unity Technologies

    Joined:
    Oct 10, 2016
    Posts:
    25
    It can. But what folder that could be? It should work on all 3 platforms and with several Unity Editor versions installed. Any idea?
     
  30. v01pe_

    v01pe_

    Joined:
    Mar 25, 2015
    Posts:
    66
    I'd suggest on e.g. MacOS
    ~/Library/Application Support/Unity/ScriptTemplates
    Windows and Linux have their equivalent and the root `Unity` folder is already used by Unity.

    EDIT: Another possibility would be to use the package manager… when building a custom template there's already the `type` field that could be used for script templates as well – there's already the value `template`, but I guess that refers to something else? Only downside it that the initial setup is much more complicated if you don't use custom packages already.
     
    Last edited: Dec 20, 2019
  31. AccentDave

    AccentDave

    Joined:
    Nov 16, 2015
    Posts:
    43
    ... plus: Something like this for Project Templates
     
    v01pe_ likes this.
  32. Fressbrett

    Fressbrett

    Joined:
    Apr 11, 2018
    Posts:
    39
    Any updates on this? Personally I would love a project based solution, since for instance the MonoBehaviour template needs to contain different namespaces depending on the project/team, which pretty much defeats the purpose of Templates if that's something I need to change manually every time, since one template for all projects doesn't cut it.
     
  33. sand_lantern

    sand_lantern

    Joined:
    Sep 15, 2017
    Posts:
    189
    There should be a folder in your project under Assets called ScriptTemplates. There you can edit your templates as desired on a project by project basis.

    upload_2020-5-7_13-27-51.png
     
  34. Fressbrett

    Fressbrett

    Joined:
    Apr 11, 2018
    Posts:
    39
    Thank you! From the other posts I've got that this isn't possible anymore, but it worked well.
     
  35. MFKJ

    MFKJ

    Joined:
    May 13, 2015
    Posts:
    264
    I found the latest 2018 scripting template edit guide! Hope that it will be helpful for future users.
     
    sand_lantern likes this.
  36. v01pe_

    v01pe_

    Joined:
    Mar 25, 2015
    Posts:
    66
    This is described already in my initial post, and that's the problem… the script templates should not be something specific to one editor version, as the current approach is. You have to copy these around for every new install and for every user that want's to use them.

    A per project solution, and a per user (one for all Unity versions) solution would be very welcome!
     
  37. MFKJ

    MFKJ

    Joined:
    May 13, 2015
    Posts:
    264
    Oops Sorry, do i delete the post ? If it is not helpful? Anyway writing single solution for every unity version can be complex. Maybe an editor script will help in this context?
     
  38. sand_lantern

    sand_lantern

    Joined:
    Sep 15, 2017
    Posts:
    189
    There is a per project solution. It is detailed in my post from may. Not sure when it was added, but I've been using it for quite a while now.
     
    v01pe_ likes this.
  39. sand_lantern

    sand_lantern

    Joined:
    Sep 15, 2017
    Posts:
    189
    Keep your post up, imo. Seems pretty helpful to me.
     
    v01pe_ likes this.
  40. v01pe_

    v01pe_

    Joined:
    Mar 25, 2015
    Posts:
    66
    No worries, keep it. Just wanted to point out the intent of the original post.

    Oh, thanks I somehow missed that… that's definitely useful. For me the per user suggestion (just a folder in the user's home folder, would suffice) would be the main place to use the custom scripts though.
     
    sand_lantern likes this.
  41. sand_lantern

    sand_lantern

    Joined:
    Sep 15, 2017
    Posts:
    189
    Yeah, that would be pretty useful too. Glad there's at least a per project method now though.
     
  42. iscf

    iscf

    Joined:
    Aug 30, 2018
    Posts:
    24
    Something new about it ?

    I would make an automatised workflow with ScriptsTemplate folder in order to add new templates but... I don't want to make my programm admin to write in program files from script ^^'
     
  43. Mikael-H

    Mikael-H

    Joined:
    Apr 26, 2013
    Posts:
    307
    I have put my script templates in a git repo that I pullö in as a submodule into Assets/ScriptTemplates
    For me that is the perfect solution because any new template I add can easily be pulled by every other member of the team and on any project
     
    sand_lantern likes this.
  44. hyagogow

    hyagogow

    Joined:
    Apr 25, 2014
    Posts:
    20
    Have you tried to put ScriptTemplates folder inside a package? It's not working here.
     
    Last edited: Mar 9, 2021
    jamesbean and v01pe_ like this.
  45. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,441
    Is this working in Unity 2020LTS?
     
  46. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,441
    Ok is working
    Under Asset create a folder name: "ScriptTemplates"
    And in it make a .txt file containing the template with the name "01-C# Script-MyScript.cs.txt"
     
unityunity