- Home /
How do I create dictionary with 3D Objects?
I'm currently trying to convert this 2D code (sprite) to 3D (3D game objects), but I don't know how. Can anyone tell me how to convert these things? This is the code
void InitializeSpriteDictionary() {
spriteDictionary = new Dictionary<char, Sprite> ();
spriteDictionary.Add (' ', none);
spriteDictionary.Add ('r', blackRook);
spriteDictionary.Add ('n', blackKnight);
spriteDictionary.Add ('b', blackBishop);
spriteDictionary.Add ('q', blackQueen);
spriteDictionary.Add ('k', blackKing);
spriteDictionary.Add ('p', blackPawn);
spriteDictionary.Add ('R', whiteRook);
spriteDictionary.Add ('N', whiteKnight);
spriteDictionary.Add ('B', whiteBishop);
spriteDictionary.Add ('Q', whiteQueen);
spriteDictionary.Add ('K', whiteKing);
spriteDictionary.Add ('P', whitePawn);
}
As far as Unity is concerned, "Sprites" are 3d objects, too, meaning they exist in 3d space. The renderer ($$anonymous$$eshRenderer, SpriterRenderer) defines how the object is rendered. So, either the objects have a SpriteRenderer or $$anonymous$$eshRenderer (or Skinned$$anonymous$$eshrenderer). You can't automatically convert from one to the other, it would require removing and adding components and creating new materials.
Answer by Glurth · Feb 01, 2017 at 07:24 PM
Regarding the dictionary code, I suspect you will simply want to replace "Sprite" (a flat image) with "Mesh" (a collection of 3D verticies, that makes up a set of triangles, and info about them). Obviously, you will also need to change all those sprite references, like "whiteRook", into Mesh references. e.g.
(dictionary creation)
meshDictionary = new Dictionary<char, Mesh> ();
(mesh reference declaration)
public Mesh whiteRook;
A Mesh, just like a sprite, can be stored as "Assets" in your project folder. Usually they are created in another program, and imported.
https://docs.unity3d.com/ScriptReference/Mesh.html
Edit: I think Cherno's comments are referring to the next step, changing the scene objects to render Meshes, rather than sprites, and I agree.