Script for loading and unloading objects?

Hi,

I was wondering if anyone has a script out there in the community that would effectively load and unload objects from memory as you get within and beyond a particular distance from them. Maybe even one that can be set for certain types of objects (eg. small distance 20-75m, medium distance 200-500m, large, 500-750, huge, 750-2000, etc).

Thanks in advance,

A

Might be of interest to you:

http://m2h.nl/files/resources_LODmanager.html
By M2H Game Studio

Hi Tomme,

Thanks for sharing. I assume from what I can make out, one would create 3 different meshes right? Attach the script to the mesh in the scene then fill in the Public Variables of the script with the appropriate info?

Let me know if I get an A or a F. :smile:

A

The principal of the script is to have three different LOD/Level of detail meshes/models.

Lets say you have a big castle model which is a drain on the performance of your game, so you develop three stages of this castle, the close, “detailed” one, which lets say has 5000 polygons, next you have the medium range model which could be 2500 polygons then finally you have the very distance 750 polygon model.

Now you will place these three mesh/model versions into the correct LOD variables:

public var lodMesh0 : Mesh;
public var lodMesh1 : Mesh;
public var lodMesh2 : Mesh;

Then you need to set the distance variables, the script checks the distance by using:

var distanceFromObject : float = Vector3.Distance(thisTransform.position, Camera.main.transform.position);

You just need to set the distance you want your models to be displayed at, here:

public var distanceLOD1 : float;
public var distanceLOD2 : float;

If your gameObject’s distance is under “distanceLOD1” then the script will display the “LOD0” mesh/model, your high detail model, next if your gameObject’s distance is between “distanceLOD1” and “distanceLOD2” then it will display “LOD1” or your medium detail mesh/model, then finally if your gameObject’s distance is larger then “distanceLOD2” it will display the “LOD2” mesh/model.

Much more on LOD/Level of detail here: Level of detail - Wikipedia
I also believe Unity is developing a built in LOD manager, well I read about it somewhere :slight_smile:

Tomme

Thanks Tomme,

I thought that was how it works. What I was wondering is would I attach this script to each object I place in the scene?

Thanks in advance,

A

PS. Yes, that would be awesome if Unity would add an LOD manager in as a standard.

I believe you attach it each object you use, obviously when using duplicate objects you will just set it once in a prefab. However I have not actually used this script, I only know of it :slight_smile:

Thanks for the info Tomme.

I will try this out. :slight_smile:

A

i think occlusion culling takes care of that :slight_smile: it will not render the object that are not in the camera’s fov.

Hi ar0nax,

Thanks for the input. Now, would OC be standard even in the Indie license (non Pro)? I assume that it would just depend on the settings you put in the Camera info right?

A

not sure, it might be a Pro only feature, to check it out go to Window menu in Unity and see if it’s there.

Actually it is there but if I select it, nothing happens…or is it just to remind me it is? :smile:

A

OC doesn’t affect what is held in memory. Are you trying to improve performance or memory? These are completely separate. For example, an inactive game object with a large texture is still in memory, but it is not affecting performance because it is not being processed.

From what I gather neither occlusion culling or LOD will help you in saving memory. As Rafes mentioned they will help in rendering performance.

In the case of LOD you are rendering smaller textures for objects that are farther away, or not rendering anything if they are past your last LOD distance. You could setup a system that will load and unload textures that are not in use, but the cost of loading/unloading from memory as you change LODs would cost you much more in performance than you would gain in memory usage.

In the case of OC objects that are within your camera view but are obstructed by other objects are not rendered. This also helps in rendering performance. OC must be setup and baked onto your scene and I don’t think it comes with the free version of Unity.

Hope this helps.