Search Unity

Using Shape2D

Discussion in 'Project Tiny' started by Tomiha_be, Apr 1, 2019.

  1. Tomiha_be

    Tomiha_be

    Joined:
    Jan 14, 2016
    Posts:
    9
    Got a few questions about the Shape2D component.
    1. Can we use it? (I know we it can't be added through the editor, and it's not visible in the editor. But should it work when added through code? Or is it deprecated in some way?)
    2. Does the Sprite2DRenderer automatically pick up a Shape2D? Or do we need to add the Shape2DRenderer for that? (already tried both, none of them renders my shape)

    This is the code I use to add and modify my Shape2D to an entity. (Don't mind the index buffer at the moment, wanted to make sure that the winding order wasn't the problem) [It's only executed once]

    Code (JavaScript):
    1.                 this.world.forEach([ut.Entity, game.Trail], (entity, trail) =>
    2.                 {
    3.                     if(!this.world.hasComponent(entity, ut.Core2D.Shape2D))
    4.                     {
    5.                         this.world.addComponent(entity, ut.Core2D.Shape2D);
    6.                         console.log("Shape appended!")
    7.                     }
    8.  
    9.                     let vertices: Vector2[] = [
    10.                         new Vector2(-1,0),
    11.                         new Vector2(0,1),
    12.                         new Vector2(1,0)
    13.                     ]
    14.  
    15.                     let indices: number[] = [
    16.                         2,1,0,
    17.                         0,1,2
    18.                     ]
    19.  
    20.                     this.world.usingComponentData(entity, [ut.Core2D.Shape2D], (shape)=>{
    21.                         shape.vertices = vertices;
    22.                         shape.indices = indices;
    23.  
    24.                         console.log("Shape modified!")
    25.                     })
    26.                 });
    There should be a Triangle visible normaly. Am I missing something?
    (The entity has a Sprite2DRenderer attached, already tried adding a Shape2DRenderer but still no triangle)

    Kind Regards
     
  2. sniffle63

    sniffle63

    Joined:
    Aug 31, 2013
    Posts:
    157
    i saw someone make a fake 3D game using Shape2D with unity tiny. So it is possible




    brave_2019-04-02_12-52-01.png
     
  3. JJJohan

    JJJohan

    Joined:
    Mar 18, 2016
    Posts:
    204
    I've put the project source code up on GitHub, I hope it's of some use to you :).

    This is probably the most relevant file (I add shape2D and shape2DRenderer components with a set of vertices and indices near the bottom of the file):
    https://github.com/JJJohan/TinyCubeRunner/blob/master/Assets/Game/Scripts/CubeSpawnSystem.ts

    The reason I do it for each cube face separately in my example is because the colors are not per vertex but this might not be relevant for you.
     
    sniffle63 likes this.
  4. sniffle63

    sniffle63

    Joined:
    Aug 31, 2013
    Posts:
    157
    Thanks alot! Super useful
     
  5. Tomiha_be

    Tomiha_be

    Joined:
    Jan 14, 2016
    Posts:
    9
    Thanks for the sample code.

    But apparently, the Shape2D module wasn't activated in my project settings... (although I thought it was o_O). After activating the module, the shapes were rendered (I could've slapped myself). Lost a lot of time figuring this out. No error when using objects from a module that is not included. :(
    I was losing my mind over this... :p

    Anyway, thanks for the suggestions! :D
     
    sniffle63 likes this.