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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Feedback Add default implementation to ISystem.OnCreate and ISystem.OnDestroy

Discussion in 'Entity Component System' started by CodeMonkeyYT, Nov 6, 2022.

  1. CodeMonkeyYT

    CodeMonkeyYT

    Joined:
    Dec 22, 2014
    Posts:
    121
    SystemBase only requires you to implement OnUpdate whereas ISystem requires you to implement OnUpdate, OnCreate and OnDestroy

    Makes sense for OnUpdate to be required but most Systems probably don't need anything in OnCreate or OnDestroy so it would be a nice improvement to just add empty default implementations for those on the interface.
     
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,294
    That will not work though, as ISystem is restrained to the struct type.

    Can't use virtual keyword inside a struct.
    Struct is a value type, and thus sealed and cannot be derived from / inherited.

    As an alternative, splitting ISystem into multiple interfaces would work.
    But that would be way more tedious (and would have more overhead to assembly scanning) than to press Alt+Enter and generate missing methods automatically.

    Best solution here is to use a file template.
    E.g. https://support.unity.com/hc/en-us/articles/210223733-How-to-customize-Unity-script-templates

    1. Add an Assets/ScriptTemplates folder (or use a default Unity's templates folder which comes with Unity installation to add it for all projects).

    2. Create a text file with a name "04-MenuName-FileName.cs".txt (without quotes) inside it.

    Where 04 is an index of menu item inside Create menu;
    MenuName - name of the item;
    FileName - name of the generated script;

    3. Add something like this inside template:
    Code (CSharp):
    1. using Unity.Entities;
    2.  
    3. public partial struct #SCRIPTNAME# : ISystem {
    4.     public void OnCreate(ref SystemState state) { }
    5.     public void OnDestroy(ref SystemState state) { }
    6.  
    7.     public void OnUpdate(ref SystemState state) { }
    8. }
    4. Reboot Unity to pick up menu item.

    And you're set, use an item from the menu to generate ISystem with all required methods automatically. Also handy if you want to customize and add something of an extra.

    Note that there's already an ECS submenu inside Create menu that automatically generates a system template.
     
    Last edited: Nov 6, 2022
  3. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,647
    It would be nice if they were implemented as default interface methods (which I believe is what the OP was suggesting?)
    Code (CSharp):
    1. public interface ISystem
    2. {
    3.     void OnCreate(ref SystemState state) { }
    4.     void OnDestroy(ref SystemState state) { }
    5.     void OnUpdate(ref SystemState state);
    6. }
    This seems to work in 2022.2 as far as I can tell
     
    bb8_1, xVergilx and CodeMonkeyYT like this.
  4. CodeMonkeyYT

    CodeMonkeyYT

    Joined:
    Dec 22, 2014
    Posts:
    121
    Yup that's exactly what I meant, just add an empty default interface implementation.
    Perhaps the issue is Unity doesn't want to force people to use C# 8+?
     
    bb8_1 likes this.
  5. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,647
    I do find with the introduction of SystemAPI a lot of my OnCreate/OnDestroy are empty so I am fully supportive of this.
     
    WAYNGames, bb8_1 and FONTOoMas like this.