PhysicsWorld.SceneLoadMode
Options for what happens to bodies and worlds when a scene loads in Single mode, or when you open a scene in the Editor.
読み終わるまでの所要時間 2 分最終更新 17時間前
Definition
- Type: Enum
- Namespace: Unity.U2D.Physics
- Assembly: UnityEngine.PhysicsCore2DModule
public enum PhysicsWorld.SceneLoadMode
Remarks
A Single mode load closes every open scene, which destroys the GameObjects in them. Bodies and worlds are not GameObjects, so this setting decides which of them are destroyed at the same time.
Whatever the setting, a body or world is never destroyed when its owner is a GameObject, or a component on a GameObject, that the load keeps. The load keeps a GameObject that you moved to the DontDestroyOnLoad scene with Object.DontDestroyOnLoad, or one that has HideFlags.DontSave set. Any other owner does not keep its bodies and worlds, including an asset such as a ScriptableObject, even though the load does not unload the asset. You set a body's owner with PhysicsBody.SetOwner and a world's owner with PhysicsWorld.SetOwner. A world is also kept while it holds a body whose owner survives.
A body keeps its shapes and chains. A joint is destroyed when either of the two bodies it connects is destroyed. A world that is kept, including the default world, keeps any settings your scripts changed, such as its gravity.
This setting has no effect on an additive scene load, on unloading a scene, or on entering and exiting Play mode in the Editor. Additive loads and unloads never destroy bodies or worlds. Entering and exiting Play mode destroys every body and world and creates the default world again from the project settings. The one exception is entering Play mode with both Reload Domain and Reload Scene disabled, which keeps everything.
Set it in the project settings with PhysicsCoreSettings2D.sceneLoadMode, and read it at runtime with PhysicsWorld.sceneLoadMode.
Examples
// Keep a body when the next scene loads, by giving it an owner that survives the load.using UnityEngine;using Unity.U2D.Physics;public class KeepBodyAcrossScenes : MonoBehaviour{ PhysicsBody m_Body; int m_OwnerKey; void Start() { // Move this GameObject to the DontDestroyOnLoad scene so it survives a Single mode load. DontDestroyOnLoad(gameObject); // Create a body owned by this component, so the body is kept when a scene loads. m_Body = PhysicsWorld.defaultWorld.CreateBody(); m_OwnerKey = m_Body.SetOwner(this); } void OnDestroy() { // An owned body can only be destroyed with the owner key that SetOwner returned. if (m_Body.isValid) m_Body.Destroy(m_OwnerKey); }}