Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Modify C# script template?

Discussion in 'Scripting' started by Leonetienne500, Aug 14, 2019 at 4:29 AM.

  1. Leonetienne500

    Leonetienne500

    Joined:
    Dec 5, 2016
    Posts:
    34
    Is there any way to make unity automatically generate return statements for newly generated script files?

    Like every new c# file has a Start() and Update() method, but it bugs me that i have to manually add a return statement to each and every one of them. It just bugs me, if they are missing.

    Also i'd really like to have automatically generated namespaces for each new class based on its directory.
    Eg, that assets\player\locomotion\strafing.cs would automatically be set to be in the namespace player.locomotion.

    Bugs me to do this manually every time i create a new script.
     
  2. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    14,277
  3. Leonetienne500

    Leonetienne500

    Joined:
    Dec 5, 2016
    Posts:
    34
  4. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,022
    What returns are you filling in Start/Update methods?
     
  5. APSchmidt

    APSchmidt

    Joined:
    Aug 8, 2016
    Posts:
    2,311
    You'll have to do that each time you install a new version of Unity. You should save your new templates somewhere.
     
  6. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,022
    Sadly it is not possible to modify those templates at project level as it can be done with web player build templates.
     
  7. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    4,104
    Instead of doing that, you can put the templates directly in your assets folder. That's generally more convenient, as they don't dissapear when you change the Unity version, and you can customize them on a per-project basis.

    Don't know why that never got documented, it's apparently always worked, just that nobody knew about it.

    You are aware than a return statement on the end of a void method doesn't do anything, right?
     
    SparrowsNest, Ryiah, Suddoha and 2 others like this.
  8. APSchmidt

    APSchmidt

    Joined:
    Aug 8, 2016
    Posts:
    2,311
    It works indeed but you have to transport your templates from one project to the other... It will be to each their own I guess... :D
     
  9. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,022
    Idk but MAYBE this will work in a package?
     
  10. APSchmidt

    APSchmidt

    Joined:
    Aug 8, 2016
    Posts:
    2,311
    Sure, a package that you'll have to import in each project... :)
     
  11. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,022
    Now I'm not sure but IF it is possible to modify somehow default project manifest file template...
     
  12. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,161
    Lea Hayes has made a script which creates the template after your desires. You can select if you want an interface, monobehavior, a normal class etc. . And you can select a few of Unitys callbacks. Maybe this is of use for someone. And it can be extended to include all callbacks or creates other templates if you need them. It is a little more versatile than having a static file somewhere.
     
  13. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    6,365
    I'm still curious about the "what are you returning?" question @palex-nx posted above. Start and Update are void... They don't need return statements and never have.
     
    palex-nx and SparrowsNest like this.
  14. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    1,851
    Regarding namespaces:

    For my projects, I also wanted all newly created scripts to have a namespace based on their location within the project hierarchy. There are events you can hook into when assets are created that allow you to modify the resulting file contents. So, for example, if you added a new C# script to Assets/Scripts/Controllers/Enemies, it would wrap the class in a namespace "Company.Project.Scripts.Controller.Enemies".

    I ended up creating a free asset you can include in your project to do this automatically. You can either try it out, or feel free to install it and just look at the source code to understand how to intercept the script creation and modify the script content:

    https://assetstore.unity.com/packages/tools/utilities/smartns-109633