Search Unity

Templates, snippets and Extensions for ECS

Discussion in 'Entity Component System' started by Deadcow_, Jun 23, 2018.

  1. Deadcow_

    Deadcow_

    Joined:
    Mar 13, 2014
    Posts:
    135
    Unity ECS is great, but it's too verbose. I constantly find myself typing really similar patches of code here or there.

    I made almost 50 resharper templates for Unity over the years, a few surrounding templates too. And homemade tiny library with bunch of syntactic sugarish extensions methods to speed up my workflow.

    I wander if you guys have any thoughts on how to speed up / make easier to write ECS code with such tools?
     
  2. JPrzemieniecki

    JPrzemieniecki

    Joined:
    Feb 7, 2013
    Posts:
    33
    I only have a Create/ECS System context menu item for generating the basic boilerplate you want in pretty much all ComponentSystems:

    Code (CSharp):
    1. #if UNITY_EDITOR
    2. using System.IO;
    3. using UnityEngine;
    4. using UnityEditor;
    5.  
    6. public static class ECSGenerateSystem
    7. {
    8.     static string _template =
    9.     "using Unity.Entities;\n" +
    10.     "\n" +
    11.     "class ##SystemName## : ComponentSystem\n" +
    12.     "{\n" +
    13.     "    struct Data\n" +
    14.     "    {\n" +
    15.     "        public int Length;\n" +
    16.     "    }\n" +
    17.     "    [Inject] Data _data;\n" +
    18.     "    \n" +
    19.     "    protected override void OnUpdate()\n" +
    20.     "    {\n" +
    21.     "        for (int i = 0; i < _data.Length; i++)\n" +
    22.     "        {\n" +
    23.     "        }\n" +
    24.     "    }\n" +
    25.     "}\n";
    26.  
    27.     class DoCreateSystem : UnityEditor.ProjectWindowCallback.EndNameEditAction
    28.     {
    29.         public override void Action(int instanceId, string pathName, string resourceFile)
    30.         {
    31.             string baseFile = Path.GetFileNameWithoutExtension(pathName);
    32.             string fullPath = Path.GetFullPath(pathName);
    33.  
    34.             var text = _template.Replace("##SystemName##", baseFile);
    35.             File.WriteAllText(fullPath, text);
    36.  
    37.             AssetDatabase.ImportAsset(pathName);
    38.             ProjectWindowUtil.ShowCreatedAsset(
    39.                 AssetDatabase.LoadAssetAtPath(pathName, typeof(Object)));
    40.         }
    41.     }
    42.  
    43.     [MenuItem("Assets/Create/ECS System", false, 0)]
    44.     public static void Create()
    45.     {
    46.         ProjectWindowUtil.StartNameEditingIfProjectWindowExists(
    47.             0,
    48.             ScriptableObject.CreateInstance<DoCreateSystem>(),
    49.             "NewSystem.cs",
    50.             EditorGUIUtility.IconContent("cs Script Icon").image as Texture2D,
    51.             "");
    52.     }
    53. }
    54.  
    55. #endif

    I've been toying with some extension methods on ComponentSystem to e.g. skip writing PostUpdateCommands all the time, but I'm not sure it's worth it. To be honest, while getting used to it I don't think ECS is _that_ much more verbose in comparison to my usual style.
     
    Last edited: Jun 23, 2018
    hippocoder likes this.
  3. Necromantic

    Necromantic

    Joined:
    Feb 11, 2013
    Posts:
    116
    There have been a few threads on this topic already with a few snippets posted.
    The ECS is still kind of in an early stage and I think these things will come over time.
    For me it's currently more important that the actual core functionality is there.
     
    JPrzemieniecki and FROS7 like this.