我认为它不起作用的主要原因是您可能Camera
在新场景中还有另一个。
内部OnMouseDrag
依赖于物理系统Collider
,使用来自Camera
. 现在,如果您切换场景,我猜想一个相机会被禁用,因此您的拖动会被打断。
也使用LoadScene
代替LoadSceneAsync
会导致明显的滞后,也可能与问题有关。
我有一个可能更复杂的解决方案,但这是我通常做的:
1. 拥有一个全局场景“MainScene”
这个场景包含诸如 MainCamera、全局光照、全局管理器组件之类的东西,无论如何都不应该被破坏。
2.使用附加异步场景加载
您说您不希望您的用户在场景切换时不注意,所以我还是建议您使用SceneManager.LoadSceneAsync
。
然后为了不卸载前面提到MainScene
的你传递可选参数 LoadSceneMode.Additive
。这使得新场景被加载到已经存在的场景之外。然后稍后您只需通过卸载先前添加加载的场景来交换它们。
我为此创建了一个非常简单的static
管理器:
public static class MySceneManager
{
// store build index of last loaded scene
// in order to unload it later
private static int lastLoadedScene = -1;
public static void LoadScene(int index, MonoBehaviour caller)
{
caller.StartCoroutine(loadNextScene(index));
}
// we need this to be a Coroutine (see link below)
// in order to correctly set the SceneManager.SetActiveScene(newScene);
// after the scene has finished loading. So the Coroutine is required
// in order to wait with it until the reight moment
private static IEnumerator loadNextScene(int index)
{
// start loading the new scene async and additive
var _async = SceneManager.LoadSceneAsync(index, LoadSceneMode.Additive);
// optionally prevent the scene from being loaded instantly but e.g.
// display a loading progress
// (in your case not but for general purpose I added it)
_async.allowSceneActivation = false;
while (_async.progress < 0.9f)
{
// e.g. show progress of loading
// yield in a Coroutine means
// "pause" the execution here, render this frame
// and continue from here in the next frame
yield return null;
}
_async.allowSceneActivation = true;
// loads the remaining 10%
// (meaning it runs all the Awake and OnEnable etc methods)
while (!_async.isDone)
{
yield return null;
}
// at this moment the new Scene is supposed to be fully loaded
// Get the new scene
var newScene = SceneManager.GetSceneByBuildIndex(index);
// would return false if something went wrong during loading the scene
if (!newScene.IsValid()) yield break;
// Set the new scene active
// we need this later in order to place objects back into the correct scene
// if we do not want them to be DontDestroyOnLoad anymore
// (see explanation in SetDontDestroyOnLoad)
SceneManager.SetActiveScene(newScene);
// Unload the last loaded scene
if (lastLoadedScene >= 0) SceneManager.UnloadSceneAsync(lastLoadedScene);
// update the stored index
lastLoadedScene = index;
}
}
这MySceneManager
是一个static class
所以它不附加到任何游戏对象或场景,而只是“生活”在Assets
. 您现在可以使用从任何地方调用它
MySceneManager.LoadScene(someIndex, theMonoBehaviourCallingIt);
类型的第二个参数MonoBehaviour
(基本上是您的脚本)是必需的,因为必须有人负责运行IEnumerator
协程,而协程本身无法完成static class
。
3. DontDestroyOnLoad
目前,您正在将您随时拖动的任何 GameObject 添加到DontDestroyOnLoad
. 但是您永远不会撤消此操作,因此您同时触摸的任何东西都将从那一刻开始……永远。
我宁愿使用例如
public static class GameObjectExtensions
{
public static void SetDontDestroyOnLoad(this GameObject gameObject, bool value)
{
if (value)
{
// Note in general if DontDestroyOnLoad is called on a child object
// the call basically bubbles up until the root object in the Scene
// and makes this entire root tree DontDestroyOnLoad
// so you might consider if you call this on a child object to first do
//gameObject.transform.SetParent(null);
UnityEngine.Object.DontDestroyOnLoad(gameObject);
}
else
{
// add a new temporal GameObject to the active scene
// therefore we needed to make sure before to set the
// SceneManager.activeScene correctly
var newGO = new GameObject();
// This moves the gameObject out of the DontdestroyOnLoad Scene
// back into the currently active scene
gameObject.transform.SetParent(newGO.transform, true);
// remove its parent and set it back to the root in the
// scene hierachy
gameObject.transform.SetParent(null, true);
// remove the temporal newGO GameObject
UnityEngine.Object.Destroy(newGO);
}
}
}
这是一个扩展方法,允许您简单地调用
someGameObject.SetDontDestroyOnLoad(boolvalue);
在任何 GameObject 参考上。
然后我将您的脚本更改为
public class MoveBall : MonoBehaviour
{
public static Vector2 mousePos = new Vector2();
// On mouse down enable DontDestroyOnLoad
private void OnMouseDown()
{
gameObject.SetDontDestroyOnLoad(true);
}
// Do your dragging part here
private void OnMouseDrag()
{
// NOTE: Your script didn't work for me
// in ScreenToWorldPoint you have to pass in a Vector3
// where the Z value equals the distance to the
// camera/display plane
mousePos = Camera.main.ScreenToWorldPoint(new Vector3(
Input.mousePosition.x,
Input.mousePosition.y,
transform.position.z)));
transform.position = mousePos;
}
// On mouse up disable DontDestroyOnLoad
private void OnMouseUp()
{
gameObject.SetDontDestroyOnLoad(false);
}
}
在你的StarCollision
脚本中你只需要交换
SceneManager.LoadScene(1);
和
MySceneManager.LoadScene(2, this);
演示
对于一个小演示,我使用两个简单的脚本“伪造”它
主场景中的这个
public class LoadFirstscene : MonoBehaviour
{
// Start is called before the first frame update
private void Start()
{
MySceneManager.LoadScene(1, this);
}
}
而这个在其他场景中
public class LoadNextScene : MonoBehaviour
{
[SerializeField] private int nexSceneIndex;
private void Update()
{
if (!Input.GetKeyDown(KeyCode.Space)) return;
MySceneManager.LoadScene(nexSceneIndex, this);
}
}
并有 3 个场景:
主要:如上所述包含
测试:包含
- 一个
MoveBall
“球体”
- 这
LoadNextScene
test2:包含
- 一个移动球“立方体”
- 这
LoadNextScene
使用与构建设置匹配的索引(确保Main
始终位于0
;))
我现在可以在键之间切换test
和test2
使用Space。
如果我同时拖动其中一个对象,我可以将它带到下一个场景中(但一次只有一个)。我什至可以将它再次带回第一个场景,以便拥有两个我可以玩的球体对象;)