Search Unity

loading levels ?

Discussion in 'Scripting' started by patricia, Aug 25, 2007.

  1. patricia

    patricia

    Joined:
    Aug 10, 2007
    Posts:
    73
    I would like to know if it's possible to load a new mesh, like if it was a level.

    For example, let's say i have a racing game , and i want to load different tracks.
    Can i do something like
    mesh1.loadNewtrack( "track12.fbx" )

    Or do i need to make 10 unity files for 10 tracks ? ( the reason i ask is for the eventually of road packs that the user could install himself )

    Thank you
    patricia.
     
  2. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    Lemme see if I can answer these...

    There are probably a couple ways of doing this, the easiest is to use use different "scenes" for each of the levels. Then you'd use something like
    Code (csharp):
    1. Application.LoadLevel ("Daytona");
    to load each of the new scenes / race tracks.

    I think I follow where you're going with this. If you're asking if there's some way of loading an external asset "bundle" (meshes + textures + other parts) from a Unity standalone, the answer is -no-. This has been asked for a couple times but isn't currently possible with Unity. You can write your own code to import assets and there's some example scripts in the Wiki to get you started. You can also call some types of assets through the WWW class from a Unity webplayer. Otherwise, if you want an asset to be usable in your game, you have to put it into your assets folder in the editor before building your player.
     
  3. patricia

    patricia

    Joined:
    Aug 10, 2007
    Posts:
    73
    Thanks for the reply,

    if i have my 10 mesh in my unity project, can i replace an existing one easily ?
    I find it a bit strange to redo a unity scene for each level. ( seems too much work to me :) )

    Patricia.
     
  4. Foxxis

    Foxxis

    Joined:
    Jun 27, 2006
    Posts:
    1,108
    You should easily be able to switch one track for another and resave the scene as a new one. Obviously, how hard that is going to be is depending on how intelligently you have set up your scene to begin with.
     
  5. pete

    pete

    Joined:
    Jul 21, 2005
    Posts:
    1,647
    scenes are actually easy to manage. build one then delete the track and add the next track, save as next scene. then you can tweak things like your start point etc but you'll keep any global managers. otherwise you could do some sort of destroy/instantiate script but scenes give you visual feedback as you build. save them in your assets folder and it's a simple double-click to switch scenes when editing.

    another thing you can do if your game isn't too complex, is place tracks outside the camera far clip plane. then just change the player position to the next start point. like the destroy/instantiate method, you'd need to build in some sort of transition or it would just pop.

    loading external assets... yep we all want that!

    [edit: a little misleading... global managers... some of these you'll only want in your start level then use dontdestroyonload to make them persistent.]
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You can load external assets; it requires progamming an importer, however.

    In the case of using different meshes, switching scenes seems like overkill if all you want to do is change the mesh. For example, a function to change the mesh on the current object could be something like:

    Code (csharp):
    1. var myMeshes : Mesh[];
    2. function ChangeMesh(whichMesh : int) {
    3.     if (whichMesh >= myMeshes.Length || whichMesh < 0) {return false;}
    4.     var mf : MeshFilter = gameObject.GetComponent(MeshFilter);
    5.     mf.sharedMesh = myMeshes[whichMesh];
    6.     return true;
    7. }
    In the inspector, you'd set myMeshes to be the number of different meshes you want to use, and then drag your various meshes into the slots there. In the script, you'd then call

    Code (csharp):
    1. ChangeMesh(5);
    to change the mesh on the object to the 6th mesh in the array. (Plus there's a bit of error checking, so you could do "if (!ChangeMesh(5)) {print ("Invalid mesh number!");}" or something.)

    Note the use of sharedMesh rather than mesh...that's important, because you don't want to be messing with the actual meshes in your assets. It's possible to change them through code, which is probably not what you want.

    --Eric