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

Question ecs beginner question

Discussion in 'Entity Component System' started by pretender, Jan 16, 2023.

  1. pretender

    pretender

    Joined:
    Mar 6, 2010
    Posts:
    862
    normally i want some data to be singleton data, here is how i tried this
    Code (CSharp):
    1. SystemAPI.SetSingleton(new GameData { Debug = false });
    but
    InvalidOperationException: No suitable code replacement generated, this is either due to generators failing, or lack of support in your current context

    GameData

    Code (CSharp):
    1. using Unity.Entities;
    2.  
    3. namespace Game
    4. {
    5.     public struct GameData : IComponentData
    6.     {
    7.         public bool Debug;
    8.     }
    9. }
    10.  
    what i am doing wrong?
     
  2. Spy-Master

    Spy-Master

    Joined:
    Aug 4, 2022
    Posts:
    291
    SystemAPI only works in specific contexts.

    Seeing the error message you have means that the invocation of the SystemAPI method isn't in a valid place. As an alternative to using SystemAPI, you can equivalently call the appropriate APIs explicitly.

    For singleton components specifically, you can see a list of some of relevant APIs here.
     
  3. pretender

    pretender

    Joined:
    Mar 6, 2010
    Posts:
    862
    yes, i managed to figure this simple thing out, in MonoBehaviour you do it like this:

    Code (CSharp):
    1.  
    2. if (Input.GetKeyDown(KeyCode.B))
    3. {
    4.     var query = _world.EntityManager.CreateEntityQuery(typeof(GameData));
    5.     var data = query.GetSingleton<GameData>();
    6.     data.Debug = !data.Debug;
    7.     query.SetSingleton(data);
    8.  
    9.     Debug.Log("GameData.Debug: " + data.Debug);
    10. }
    11.  
    and in SystemBase like this:

    Code (CSharp):
    1.  
    2. var gameData = SystemAPI.GetSingleton<GameData>();
    3. gameData.Debug = !gameData.Debug;
    4. SystemAPI.SetSingleton(gameData);
    5.  
    now i wanted to place a prefab in GameData like this:
    Code (CSharp):
    1. public struct GameData : IComponentData
    2.     {
    3.         public bool Debug;
    4.         public Entity Prefab;
    5.     }
    and to make authoring component like this:

    Code (CSharp):
    1. public class GameDataAuthoring : MonoBehaviour
    2.     {
    3.         public Entity Prefab;
    4.     }
    5.    
    6.     public class BakeGameData : Baker<GameDataAuthoring>
    7.     {
    8.         public override void Bake(GameDataAuthoring authoring)
    9.         {
    10.             AddComponent(new GameData
    11.             {
    12.                 Debug = false,
    13.                 Prefab = authoring.Prefab
    14.             });
    15.         }
    16.     }
    but it doesn't appear in the inspector, so i guess it has to be game object, but how to convert it then to entity? I made a prefab out of the game object that was in the subscene which i use for automatic conversion but it doesn't work. how to have prefab in the game data that i can place in the inspector and then access to spawn in the game?
     
  4. apaxnid

    apaxnid

    Joined:
    Nov 18, 2012
    Posts:
    34
  5. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,993
    https://github.com/Unity-Technologi...lloCube/Assets/4. Prefabs/SpawnerAuthoring.cs