I'm a student using the latest version of Microsoft XNA for learning purposes.
I need to load a whole lot of texture from the Content folder (or more specifically a folder called Tiles inside the Content folder) and I really don't want to type the following code 100 times:
texture = Content.Load<Texture2D>("texture");
I realize I could change the names of the files and run through a loop to load them, but this seems inefficient for finding specific tiles if there's a way I could keep the names. I am just looking for a way to load all of these that saves development time.
Also, the files are in .BMP form, if that matters.
Keep in mind that every texture needs to be loaded at least once.
So using
texture = Content.Load<Texture2D>("texture");
is fine to repeat for different textures, as long as you don't repeat it for the same texture.
I've learned is to load all textures in the LoadContent() of game1.cs, that method is only called once at the moment the game starts. So they're all ready to be used for the rest of the game.
There's also a Lazy Loading practice, where the texture is only loaded for when the object appears. But you should be aware that the texture loading won't repeat itself for each object.
Related
I want to change some game assets like backgrounds, images and buttons for another ones for an event and I want to let the players switch between the UI skins wherever they want.
Tested creating some GUI Skin but I think thats not what I want.
Right now, before build the game, I manually copy and paste new assets and replace old ones so the files have the same name and Unity changes every image in the game that uses that sprite. When the event finish I do the same with the old images. Bu I want to change that with just a button.
I know I could do the cahnge one by one but, there is any way to do this change withouth having to set all image sprites one by one? maybe changing the "baseSprite" name to "baseSpriteTemp" and the "eventSprite" name to "baseSprite"?
Any idea?
GUISkin is what you would use with the Immediate Mode GUI system, i.e. drawing in script OnGUI functions.
If you are talking about in game assets for the UI and other items, you have options:
Roll your own theming system
Use AssetBundle Variants
Use Addressable assets
I have used asset bundle variants in the past, and they were pretty easy to setup.
Addressables are a newer feature, and as such I have had little experience, but it looks like it can work for what you are looking for.
I want to make a game in which the player can take a picture of the view in one of the camera GameObjects whilst the game is running and then save that image and view it later in the same play session, much like taking a screenshot using a camera item in other games and then viewing it in a gallery. I was wondering if there was currently anyway of doing this within Unity as it will be a central mechanic in my game.
There are multiple ways to do this.
The simplest is Application.CaptureScreenshot, which will create an image file. To explore the screenshots, use a file explorer package, or implement your own.
The other solution is to use a RenderTexture and to export the data to a List. You can then create an UI to explore the List. The problem is that each texture will be uncompressed and kept in memory, which can become dangerously heavy after only a few screenshots. Only do this if you don't have access to files or if you only keep the last few screenshots.
I have a very very Big/Huge scene to load and I can't load it at once.
What I have tried so far is I divided the scene into multiple scenes and loading it into async manner as my character controller move. I am moving my character controller and accordingly loading (its near)scenes by matching its position. But it is not smooth. . .
How do I load big scene smoothly? what will be the strategy? Is this right strategy which I am doing or there is any other feasible and easy to use solution available.?
Try separate your scene into small chunks. When you move your character - just load new chunks and unload old. For example, this strategy uses minecraft.
You can't decrease loading time to zero milliseconds, but loading will be smooth. You can use chunks about 5x5x5 meters. And load from manager with coroutines.
Don't forget to use object pool pattern (because Instantiate it very costly) :)
UPD:
For example, this way use in plugins:
Uniblocks Voxel Terrain
MapMagic World Generator
Also, this plugins uses open world generation from 2d/3d noises (Perlin noise, etc.)
I've been using Monogame for a awhile now and I was just wondering what is the best way to load my content? Lets say i have an intervals system that constantly creates objects on the screen, so should I load the object's sprite in the game class and put the sprite variable in the constructor or should I put the content variable in the object constructor and load the sprite from within the object?
Btw by best way I mean I try to keep the framerate and use less memory, thx in advance!
Best way for frame rate is to load it before game loop starts. But the best way for less memory is right when you need it and dispose of it after your done. Not necessarily mutually inclusive... pick your poison.
You have 3 different solutions, and each will fit a different type of game.
Load everything in LoadContent
This will cause a longer initial startup time, but after everything is loaded, you wont have to wait for anything anymore. However keep in mind that this is only suited for small games such as Tetris, Arcanoid or Chess. Games that generally doesn't have a lot of content. Keep in mind that the more content you load, the more memory it will consume.
Load everything needed for the current scene
This is what most games does, since you wont load data for scenes that you never use, and you wont load data for scenes you're not yet accessing.
Load everything needed for the current scene (Extended)
Like the previous answer, but with a small twist. If possible, load content while displaying other content. For instance, as soon as the player finished the last step for completing a scene, initiate the loading of the next scene.
Arcanoid example: As soon as the ball hits the last block (or if you're brave, even as soon as you can calculate that the ball WILL hit the final block), initiate the loading of the content for the next scene. Let this load while the ball flies towards the final block, and also while displaying the score for the current scene (Time, deaths, bonuses, etc.)
And should the player actually close the dialogues before the scene has been loaded, show another loading scene while the data finishes loading. That way, there might only be 1-2 seconds of loading time instead of 10.
Remember to perform all this loading in a background thread.
I've run into a slight problem. When I restart my game (by running my own initialize method), I can successfully reload all my objects/variables. But the initial SoundEffectInstance I used continues to play while a new instance is created. This results in multiple instances of the same music being played simultaneously.
What I'm trying to achieve is a way to get rid of the first instance when making a new one.
instance.Dispose(); can't be used as it means I can't re-initialize the sound.
I tried using a list to solve this (removing instances before restarting game), but it seems I can't CreateInstance() from within the list, restricting me from initializing the sound to begin with.
Arrays don't seem to offer a solution either. Any ideas?
First off, you probably shouldn't use SoundEffectInstance for music - this results in the entire song being loaded into RAM uncompressed - which eats up a ton of space relative to streaming an MP3/WMA from disk using Music.
If you want to dispose all content then you should unload the ContentManager that has all of your content. Then you can recreate the content - it will take some time to re-load it from disk but you will have a clean set of content if that's what you're after. Keep in mind that a SoundEffectInstance contains unmanaged resources, so the only way to "remove" it is to dispose it or unload its ContentManager.
If you don't want to do that then you should be preserving references to your content and then you can manually do things like stop:
http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.audio.soundeffectinstance.stop.aspx
You should use mediaplayer.play() to play a song instead of SoundEffectInstance and then
you can just have a if statement that you can say if X happens then mediaplayer.play()
else
mediaplayer.stop(); or
mediaplayer.pause()