Search Unity

Archetype and Instanciation

Discussion in 'Project Tiny' started by gamayun, Jun 20, 2019.

  1. gamayun

    gamayun

    Joined:
    Nov 20, 2012
    Posts:
    22
    Hi guys,

    I'm playing around with Unity Tiny CSharp after having done quite well with JS earlier. I have to admit I'm a bit lost now that I don't have EntityGoups, loading etc.

    Anyway I'm pretty sure that when we'll have a documentation we're going to have a wonderful Tiny engine!
    And the Unity Dev team, you are doing a great job!

    If someone have some time to post an example using Archetypes and instantiating entities, I'd be very grateful :)

    Cheers,

    Thierry
     
  2. etienne_unity

    etienne_unity

    Unity Technologies

    Joined:
    Aug 31, 2017
    Posts:
    54
    Hi @gamayun - you're not alone :) We'll add a menu navigation / scene loading example in our next release.
     
    gamayun likes this.
  3. AlexMasse

    AlexMasse

    Joined:
    Jun 29, 2014
    Posts:
    17
    Here's an example on how to load a MainMenu scene:
    1. Create a component called MyScenes with a SceneReference field called MainMenu
    2. Add this component to the configuration entity (The configuration entity is at the bottom of the hierarchy window)
    3. Drag your scene into the scene reference field
    4. In your code, load the scene like this:
      Code (CSharp):
      1. var myScenes = World.TinyEnvironment().GetConfigData<MyScenes>();
      2. SceneService.LoadSceneAsync(myScenes.MainMenu)
    5. Please note that scene loading is async so it might take a few frames for your scene to exist in the world. If you want to initialize something on your scene after loading it, you can add a component in your scene with a IsInitialized field on it. In your code, check if it's false, then do your initialization and set it to true.
     
    Last edited: Jun 20, 2019
    gamayun likes this.
  4. gamayun

    gamayun

    Joined:
    Nov 20, 2012
    Posts:
    22
    Hi Alex, thanks for the trick, I'll do that for now
     
    AlexMasse likes this.
  5. gamayun

    gamayun

    Joined:
    Nov 20, 2012
    Posts:
    22
    Hi again all,

    I finally managed to spawn my entities :) I needed 16 sprites, hexagones to start a game about a beehive. Don't mind about the ugly "tempo.sprrdr.sprite" I'm using to fill the sprite component, and also the game.tagHex which is just my component I'm starting to develop from now on (and I use a system on it to init the entities' coordonates to build my beehive).
    It works perfectly, at first (or probably second) frame.
    Here is the code for those who wondered like me:

    Code (CSharp):
    1.             myArch = EntityManager.CreateArchetype(new ComponentType[]
    2.             {
    3.                 typeof(Unity.Tiny.Core2D.Parent),
    4.                 typeof(Unity.Tiny.Core2D.Translation),
    5.                 typeof(Unity.Tiny.Core2D.Rotation),
    6.                 typeof(Unity.Tiny.Core2D.NonUniformScale),
    7.                 typeof(Unity.Tiny.Core2D.Sprite2DRenderer),
    8.                 typeof(Unity.Tiny.Core2D.LayerSorting),
    9.                 typeof(game.tagHex)
    10.              });
    11.             for (int i = 0; i != 16; i++) // 16 hexagons for my beehive
    12.             {
    13.                 Entity ec = EntityManager.CreateEntity(myArch);
    14.  
    15.                 Parent p = new Parent();
    16.                 p.Value = Entity.Null;
    17.                 Translation t = new Translation();
    18.                 t.Value = float3.zero;
    19.                 Rotation r = new Rotation();
    20.                 r.Value = quaternion.identity;
    21.                 NonUniformScale sca = new NonUniformScale();
    22.                 sca.Value = new float3(1f, 1f, 1f);
    23.                 Sprite2DRenderer s = new Sprite2DRenderer();
    24.                 s.color = new Unity.Tiny.Core2D.Color(1, 1, 1, 1);
    25.                 var tempo = EntityManager.GetComponentData<game.tagGameManager>(game.tagHex._EntityManager);
    26.                 s.sprite = tempo.sprrdr.sprite;
    27.                 LayerSorting lay = new LayerSorting { order = 1 };
    28.                 game.tagHex tHex = new game.tagHex();
    29.  
    30.                 EntityManager.SetComponentData(ec, t);
    31.                 EntityManager.SetComponentData(ec, r);
    32.                 EntityManager.SetComponentData(ec, sca);
    33.                 EntityManager.SetComponentData(ec, s);
    34.                 EntityManager.SetComponentData(ec, lay);
    35.                 EntityManager.SetComponentData(ec, tHex);
    36.  
    37.             }
    38.         }
    39.  
    40.  
     
  6. Reborn1214

    Reborn1214

    Joined:
    Jan 19, 2019
    Posts:
    15
    Code (CSharp):
    1.  var emg = cS.EntityManager;
    2.             var evn = cS.World.TinyEnvironment();
    3.             var configEntity = evn.GetEntityByName( "GameConfig" );
    4.             var bitmap = emg.GetComponentData<Text2DStyleBitmapFont>(configEntity);
    5.             var temp = emg.CreateEntity(ArchetypeFactory.TextArchetype);
    6.             emg.SetComponentData(temp,new Parent { Value = Entity.Null} );    
    7.             emg.SetComponentData( temp , NonUniformScale.Default );
    8.             emg.SetComponentData( temp , RectTransform.Default );
    9.             emg.SetComponentData( temp , new Text2DRenderer { style = temp, pivot = new float2(0.5f,0.5f),blending = BlendOp.Alpha} );
    10.             emg.SetComponentData( temp , bitmap );
    11.             emg.SetComponentData( temp , new Text2DStyle { color = Color.Default , size = 14 } );
    Have you ever tried adding Text2DStyleBitmapFont? When I set it,I get message "Entity [Unnamed Entity 26:1] has zero or negative size (0.0,50.0)! This is checked in DEVELOPMENT builds only!"
     
  7. Reborn1214

    Reborn1214

    Joined:
    Jan 19, 2019
    Posts:
    15
    oh,TextString must be set here... emg.SetBufferFromString<TextString>( temp , " " );
     
    gamayun likes this.
  8. gamayun

    gamayun

    Joined:
    Nov 20, 2012
    Posts:
    22
    I'm not used to that, thanks Reborn1234 for pointing it at me, I'll look into it, everything in Tiny ECS is kinda new to me :)