Jump to content

About loading and unloading gameObjects in multi terrain scene


Wuyan
 Share

Go to solution Solved by Peter,

Recommended Posts

1. How to load and unload some game objects (such as buildings) in a multi-terrain scene. I've tested them as children of the terrain and they can be loaded and unloaded with the terrain. Is this the correct way? Or is there any other way?

 

2. My first time using a multi-terrain world. If I want to save and read some data of spawn GameObject while unloading and loading. For example, trees are felled, berries are collected, etc. what should I do? Can you provide some ideas or solutions? Thank you so much.

Edited by Wuyan
Link to comment
Share on other sites

  • Solution

Translated Question:
 

Quote

1. How to load and unload some game objects (such as buildings) in a multi-terrain scene. I've tested them as children of the terrain and they can be loaded and unloaded with the terrain. Is this the correct way? Or is there any other way?

 

2. My first time using a multi-terrain world. If I want to save and read some data of spawn GameObject while unloading and loading. For example, trees are felled, berries are collected, etc. what should I do? Can you provide some ideas or solutions? Thank you so much.

1. Yes, this is exactly how you would do it. By adding them as children it is ensured that the objects are being loaded together with the terrain, and also put into the cache correctly.

2. Terrain loading works by loading and unloading small unity scene files - those scene files cannot be saved in a standalone build, so if you would e.g. track the amount of apples on a tree directly on the object, the object would always be reset when the scene unloads and loads again. If you have only a few objects you could put them all in the main scene and keep them in memory even if the terrain is being unloaded, but this is not really a good solution if you have many objects like trees you need to track, or if the objects are affected by gravity, etc.

What you would need to do instead is to build structures where you save the state of the object in a manager object in the main scene.

For example if you have rocks that can be collected, you could have a "RockInstance" script on every rock. This script could generate an ID to identify the rock, this could be just a number for example or a GUID. 

Then you could have a "RockManager" object that allows the RockInstances to read and write the current state to it. When the player picks up the Rock with the ID ABC123 the RockInstance calls a function in the RockManager that logs that the ID ABC123 has the state "taken" and then e.g. hides the mesh renderer for the rock.

When the terrain is unloaded and then later loaded again, the RockInstance could check in the RockManager at startup if it has any data saved for its instance - if it finds out that it is taken already, it can hide the mesh renderer and put itself in the state where it cannot be collected anymore.

Translation of the answer:

1. 是的,这正是你要做的。通过将它们添加为子对象,可以确保对象与地形一起加载,并正确放入缓存中。

2.地形加载通过加载和卸载小的统一场景文件来工作 - 这些场景文件不能保存在独立的构建中,所以如果你愿意,例如直接在对象上跟踪树上苹果的数量,当场景卸载并再次加载时,对象总是会被重置。如果您只有几个对象,您可以将它们全部放在主场景中,即使正在卸载地形,也可以将它们保存在内存中,但如果您需要跟踪许多对象(例如树木),这并不是一个很好的解决方案,或者如果物体受到重力等的影响。
相反,您需要做的是构建结构,在其中将对象的状态保存在主场景的管理器对象中。
例如,如果你有可以收集的岩石,你可以在每块岩石上都有一个“RockInstance”脚本。该脚本可以生成一个 ID 来识别岩石,这可能只是一个数字或 GUID。
然后你可以有一个“RockManager”对象,它允许 RockInstances 读取和写入当前状态。当玩家拿起 ID 为 ABC123 的 Rock 时,RockInstance 会调用 RockManager 中的一个函数,该函数记录 ID ABC123 的状态为“taken”,然后例如隐藏岩石的网格渲染器。
当地形被卸载然后再次加载时,RockInstance 可以在启动时检查 RockManager 是否为其实例保存了任何数据 - 如果它发现它已经被占用,它可以隐藏网格渲染器并将自己放入无法再收集的状态。

 

  • Like 1
Link to comment
Share on other sites

17 hours ago, Peter said:

1. Yes, this is exactly how you would do it. By adding them as children it is ensured that the objects are being loaded together with the terrain, and also put into the cache correctly.

2. Terrain loading works by loading and unloading small unity scene files - those scene files cannot be saved in a standalone build, so if you would e.g. track the amount of apples on a tree directly on the object, the object would always be reset when the scene unloads and loads again. If you have only a few objects you could put them all in the main scene and keep them in memory even if the terrain is being unloaded, but this is not really a good solution if you have many objects like trees you need to track, or if the objects are affected by gravity, etc.

What you would need to do instead is to build structures where you save the state of the object in a manager object in the main scene.

For example if you have rocks that can be collected, you could have a "RockInstance" script on every rock. This script could generate an ID to identify the rock, this could be just a number for example or a GUID. 

Then you could have a "RockManager" object that allows the RockInstances to read and write the current state to it. When the player picks up the Rock with the ID ABC123 the RockInstance calls a function in the RockManager that logs that the ID ABC123 has the state "taken" and then e.g. hides the mesh renderer for the rock.

When the terrain is unloaded and then later loaded again, the RockInstance could check in the RockManager at startup if it has any data saved for its instance - if it finds out that it is taken already, it can hide the mesh renderer and put itself in the state where it cannot be collected anymore.

 

Hi Peter, Thank you so much!  Looks like a good idea. I‘ll try it.

Edited by Wuyan
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Tell a friend

    Love Unity 3D World & Game Creation Tools - Canopy - Procedural Worlds? Tell a friend!
  • Interesting Articles

    1. 8

      Errors from Gaia 2021 after updating to Unity 2021.3.2 LTS

    2. 7

      Problems with latest Gaia Update

    3. 7

      Problems with latest Gaia Update

    4. 0

      Ambient sounds issues

    5. 7

      Scene Optimizer

×
×
  • Create New...