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

Question Can I get rid of a part of script template name for struct name?

Discussion in 'Scripting' started by SlumberPenguin, Sep 24, 2023.

  1. SlumberPenguin

    SlumberPenguin

    Joined:
    Sep 20, 2020
    Posts:
    25
    Code (CSharp):
    1. using UnityEngine;
    2. using Unity.Burst;
    3. using Unity.Entities;
    4. using Unity.Mathematics;
    5.  
    6.     #ROOTNAMESPACEBEGIN#
    7. public class #SCRIPTNAME# : MonoBehaviour
    8. {
    9.     class Baker : Baker<#SCRIPTNAME#>
    10.     {
    11.         public override void Bake(#SCRIPTNAME# authoring)
    12.         {
    13.             var entity = GetEntity(TransformUsageFlags.None);
    14.         }
    15.     }
    16. }
    17.  
    18. #ROOTNAMESPACEEND#
    19.  
    That's my DOTS Authoring Script Template, for example, my script name is PlayerAuthoring. OK, It automatically generate "PlayerAuthoring" for the script name. and I wanna automatically generate "Player" for a struct name, It's like:

    Code (CSharp):
    1. using UnityEngine;
    2. using Unity.Burst;
    3. using Unity.Entities;
    4. using Unity.Mathematics;
    5.  
    6. public class PlayerAuthoring : MonoBehaviour
    7. {
    8.     class Baker : Baker<PlayerAuthoring>
    9.     {
    10.         public override void Bake(PlayerAuthoring authoring)
    11.         {
    12.             var entity = GetEntity(TransformUsageFlags.None);
    13.         }
    14.     }
    15. }
    16. public struct Player : IComponentData
    17. {
    18.  
    19. }
    Any method achieves it?
     
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    1,833
    I think you're overthinking or expecting too much from a bone-simple templating helper.

    #SCRIPTNAME# is fed by the filename you choose.

    The extra little struct you added is so short that I'd be embarrassed to add tooling to produce, but if you must, just run a little perl command or something which you can execute from your editor.
     
  3. SlumberPenguin

    SlumberPenguin

    Joined:
    Sep 20, 2020
    Posts:
    25
    yeah that's really short but used a lot, If it can be achieved, it can save a lot of time of repeat XD
     
  4. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    1,833
    Ok, $50/hr, minimum two hours.

    Oh, wait.

    Code (csharp):
    1. #!/bin/env perl -pi~
    2. $B = $1 if /class (\w+?)Authoring/;
    3. END { print "public struct $B : IComponentData\n{\n\n}\n" if defined $B; }
    If you're on Windows...

     
    Ryiah and SlumberPenguin like this.
  5. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,082
    A custom editor script to generate the template.
     
  6. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,900
    Here is my solution (MIT) and all, just open the Tools/Lurking Ninja/C# Templates Config menu and setup whatever templates you like. Put this in the Packages/ folder, obviously. You can even add shortcuts to any of your templates. (And I put this menu lower than the Create menu, so it's more usable in most cases)
     

    Attached Files:

    Ryiah likes this.
  7. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,900
    Kurt-Dekker likes this.