Search Unity

Loading something once?

Discussion in 'Scripting' started by gk104, Aug 22, 2014.

  1. gk104

    gk104

    Joined:
    Apr 25, 2014
    Posts:
    57
    Hey folks
    i was wondering if there is a thing in unity that can load something once, for example, i want to add a texture

    when the main menu is loaded and then to destroy it (pretty easy tho), but then if i load the second level, and going backwards to the main menu, that texture won't be loaded again, is that possible?

    maybe it's kinda related to OOP or something i dunno..
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    DontDestroyOnLoad would be a good way to go here. Call it on your menu objects and just disable them when you enter the game scene. Then you can load a blank scene and re-enable the menu objects to return to the main menu. Should be pretty seamless.
     
    gk104 likes this.
  3. Kirlim

    Kirlim

    Joined:
    Aug 6, 2012
    Posts:
    126
    Just adding, because you said loading textures.

    If I am correct, any textures and materials that you create by yourself in code (even the ones created when changing renderer.material, I think), it is your duty to manually unload them as well. Failing to do so will result in memory leak since those won't be under gc surveillance. It'd be nice if someone could confirm this right or wrong, though.

    Final note, Unity and OOP do not tag along so well since Unity is projected towards Component Based Systems. I'd suggest to research about this in deep for a few days - it will save you a lot of problems caused by code design later. Not saying to not use OOP, but to keep components orientation in mind as you do stuff.
     
    gk104 likes this.
  4. gk104

    gk104

    Joined:
    Apr 25, 2014
    Posts:
    57
    Hey there,
    thanks for the info about unloading textures, didn't know that. but how do you unload textures?
    i didn't quite get the idea, maybe something like that?

    Code (CSharp):
    1. if(Application.loadedLevel != 0)
    2. {
    3. // for example, current level is 0
    4. Texture tex= null;
    5. }
    and also, is there any program to check memory leak in pc or android?
     
  5. Kirlim

    Kirlim

    Joined:
    Aug 6, 2012
    Posts:
    126
    Remember to only unload manually created textures. Ones set by inspector, references or the likes are under Unity care, and will be unloaded as they are supposed to. I believe that you need to use this method:
    http://docs.unity3d.com/ScriptReference/Object.Destroy.html

    For specifically textures, it is more like you will be using the Resources set of methods. In that case, there is a proper Resources unloading method. For simplicity, use references with inspector and prefabs, and follow StarManta suggestion. It will be seamless, flexible and will avoid most of the problems you could run into by going low level.

    Software to check for memory leaks... I don't know. Probably not.
     
    gk104 likes this.
  6. gk104

    gk104

    Joined:
    Apr 25, 2014
    Posts:
    57
    Sorry for disturbing you, can you give me an example for manually created texture(a piece of code or something)?
     
  7. Kirlim

    Kirlim

    Joined:
    Aug 6, 2012
    Posts:
    126
    I believe that when you use something like Texture t = new Texture(). Usually you will use this to clone textures so you can modify the clones. Another way is 'fetching' a texture from the WWW class - it will instantiate a new texture for you in 'each' fetch. I believe that the WWW created textures are also not under Unity gc care.

    Memory leak wise, WWW class and any other thing that implements IDisposable needs to be manually unloaded by the programmer. And assets loaded by Resources methods in which you lose all references also become memory leaks afaik.

    These are "advanced" stuff that you should be worrying only if you really need them. What exactly are you trying to do? Chances are that you are trying to use a complex solution to a simpler problem.
     
  8. gk104

    gk104

    Joined:
    Apr 25, 2014
    Posts:
    57
    Thank you very much, helped alot :)

    Im just building an simple cubes game for android, and because of the GUI in unity that changes within different screen resolutions, i decided to use one resolution for all devices with: Screen.SetResolution

    so problem is when i load the app(not level, only app), it's kinda making the screen weird because of the command above. so i thought of making texture when that command runs and then to destroy it when screen resolution changed.
     
  9. Kirlim

    Kirlim

    Joined:
    Aug 6, 2012
    Posts:
    126
    Each mobile device has its own aspect ratio and resolution. Specially on android, there are many, many many possible combinations (try making something compatible with ALL galaxy y, for example).

    Unity GUI system has its pros and cons (theres the new GUI system coming with 4.6, it might solve this problem for you). If you need something related to screen size, you can use planes with textures instead of the GUI. They will be on scene size. Theres one draw back: on small screens (even if they are a 5cm tall 4k resolution), the textures will be small, whereas on huge tatami-like tablets, the textures will be huge. With different aspect ratios, items to the sides of the screen might be cut or be way inside the screen. You will be *forced* to make code to reposition these items. Be aware that, since they wont be pixel perfect, visual artifacts will become common - your artwork will need to be resistant to this.

    Anyway, GUI and UI feels like 80% of the job for mobile games. You'll get used to it. If possible, avoid using SetResolution on mobile devices.

    Common trick: create a new, orthogonal camera that shows only a GUI layer (create one if needed). Make it show on top of the gameplay camera. Calculate the width of the GUI camera on code, during runtime (different devices, different aspect ratios). The math is simple. Make gameplay camera to not render GUI layer, and put all you UI stuff on the GUI layer. Finally, put some script on these UI objects that corrects their position according to the width of the GUI camera (i.e always 2 world units from the left). This correction can be made on OnStart or OnEnable methods. Then test, test, test, test with different screen resolutions. Search for visual artifacts, change textures import settings, etc. Beautifying a game is a task on its own.
     
    gk104 likes this.
  10. gk104

    gk104

    Joined:
    Apr 25, 2014
    Posts:
    57
    thanks alot for the info!!
    i have done some research online and found out GUI.matrix in unity, i think it solved my problem, i removed the SetResolution and currently using GUI matrix and it seems like its working, tested on several resolutions and it keeps the same scale, which is great, last thing to check is on bigger screens like tablets and such...

    and yeah, the GUI and UI is most of the work in smartphones and it's kinda annoying.
    i'll check out your trick later, and currently im looking forward for the new GUI in 4.6v, i hope that its really fix the problem with GUI scales and resolutions.