Load existing GameObjects into a 2D array - c#

I'm making a tile based board game (on a grid). In going about making the gameboard, the easy thing would be to instantiate all of the tiles in code, but then they only appear in play mode. I want to be able to edit the tiles in the editor, but I still want my tiles to be stored in a 2D array in my controller class. If I build the board in the editor, how might I go about loading them in at runtime?

Keep The Tiles in the Resources Folder Unity. At Run-time Load from the Resources and instantiate Tiles To Your Grid Positions in the Scene.
GameObject[] Tiles= (Resources.LoadAll<GameObject> ("Tiles"));

Related

Text on Sprites in Unity

I'm trying to make an application that has sprites going down the screen with numbers in them. The Text(Script) component only works for 3D objects. I've tried using Text Mesh and making that a child of the sprites but I need them to be instantiated as a Prefab. So when I load the sprite in as a prefab it loses it's relationship to the Text Mesh. Does anyone know of a solution that worked for them?
There are many ways to do it depending upon your requirement. I will show you two of the most basic.
1- Use 2D Sprite and TextMesh.
2- Use Canvas Image and Text Objects.
P.S First one, that is 2D Sprite can be Instantiated directly in the hierarchy but for 2nd(Canvas Image) you need to instantiate it inside the canvas obj means as a child of canvas object.

Unity - How to combine sprites into one

I want to make RPG game using Unity2d with the tile feature to draw the game map.
I created a new class inherited from UnityEngine.Tilemaps.Tile and overrided void GetTileData.
In void GetTileData I determine the sprite to show for each tile according to the tile's neighbors.
See the image below. The source image is input from the inspector. The input is only one image like this below. I don't want to make massive input images caus that totally mess up.
But then I have a problem. When in the game I have to extract certain blocks from the source image and combine them into a new sprite to show onto the map as a tile sprite.
Just want to know, if I have the 4 rectangles known, and want to combine them into a sprite like the image above, how can I do that?
There is no easy way to do what you want in code. Simply use a tilemap with smaller base tiles.

Unity layers not working as I want

So I'm very new to Unity and creating my first 2D game, It will have a player, monsters, platforms and a static background image.
So What I've done is set a 2D sprite as a background image which is on the default layer. I also got a character from the asset store that I just pulled in to the game and set to User layer 8 (player).
The platforms randomly spawn on the map and was not visible through the background at first but when I set the sprite sortingorder to 1 they were visible.
Now for the monsters. They are also from the asset store but inserted into the game via C# code. If I have a the background on screen they are still not visible even if i set the GameObject.layer = 8 for them(to same layer as player). Why? What Is the problem here.
Thanks in advance.
Unity has two types of layers, the ones you're changing are not the ones that determine drawing order, you need to change the SortingLayer and OrderInLayer parameters in the SpriteRenderer component.
https://unity3d.com/learn/tutorials/topics/2d-game-creation/sorting-layers
If you're only using sprites then you can keep all of them in the same SortingLayer and change the OrderInLayer of the background to something like -100.
If you're using 3d models, youre gonna need to manually set the positions of the objects closer or away from the camera (and keep all the sprites on the default SortingLayer).

Unity 3D: Load sprites from a folder

Is it possible in Unity 3D using C# to create an array to load sprites randomly from a folder rather than from a sprite sheet? If it is, what code do I use to refer to the folder? From what I can find, sprites are usually coded to load using random.range with an array using a sprite sheet instead of actually accessing the folder. The only thing even similar to this that I have been able to find is here:
http://docs.unity3d.com/ScriptReference/Resources.html
but as you can see with this you can only load from a folder titled "Resources" in the "Assets" folder, and possibly I'm mistaken but it would also seem that this can only be done with a game object. (?)
You are looking at the right docs.
A sprite is a GameObject, just a more specific one meant to be used in 2d games. So you would create your sprites and make prefab of them. Those prefabs go in the Resources folder and here is the code:
GameObject [] objs = (GameObject[])Resources.LoadAll("SpriteFolder");
GameObject randomSprite = objs[Random.Range(0, objs.Length)];
Here is what I ended up using:
Sprite[] enemySprites = Resources.LoadAll("Sprites/Enemies");

import scene in xna

I have a scene that contains mutiple objects. How can i import it in xna and mentain each objects position? right now i export the scene in .fbx and load it in a model like this :
cube.model = contentManager.Load<Model>("cub");
but the objects don't retain their position and are all gathered in one point.
I need a method to import all the objects as individual objects but to retain the objects position in the scene?
(i.e. i need to import the scene so that i may manipulate the objects and retain their position in the scene so that i shouldn't reposition all the objects by myself)
Each object's scene position is in the fbx. The way to get it and implement it is to create a matrix array to hold every object's transform relative to the scene origin, then utilize the appropriate transforms when setting the effect.World for each object during draw time.
//class variables
Matrix[] objectTransforms;
//LoadContent section
cube.model = contentManager.Load<Model>("cub");
objectTransforms = new Matrix[cube.model.Bones.Count];
cube.model.CopyAbsoluteTransformsTo(objectTransforms);// the magic is done here
//draw method
foreach(ModelMesh mm in cube.model.Meshes)
{
foreach (BasicEffect bfx in mm.Effects)
{
bfx.World = objectTransforms[mm.ParentBone.Index] * whateverLocalTransformYouWant;
//draw here
}
}
Without utilizing 'objectTransforms' all objects will be drawn with their local origins located at the world origin, which sounds like what you are experiencing.
I personally made a level editor for most of the things in my game, but I do have a large scene in blender that links to other models. That way I can arrange all the level pieces in blender. When I export each individual piece, it does retain is global position and import correctly in the game. I would say that the issue resides in your fbx exporter. There should be an option to preserve location.
What modelling program do you use? If you use blender, there is an fbx exporter set made specifically for XNA that you should use.

Categories

Resources