unity多场景加载与GameObject实例管理

使用LoadSceneMode.Additive,可以同时加载多个场景。但是并不是所有可以见元素都是可以直接调用的。如图,加载了scene02,激活后显示为粗体。
这里写图片描述

一些常用对象的说明:

  • Directional Light 不对另一个场景中的物体有作用,需要在独立的场景中单独创建。
  • EventSystem 只需要在一个场景中存在。
  • Camera 可以看见另一个场景的物体。
  • GameObject 只能操作当前ActiveScene中的物体,如果要把一个物体移动到另一个scene中,使用SceneManager.MoveGameObjectToScene(GameObject, Scene)方法。
  • GameObject.Find() 可以找到所有场景中的物体。

使用一个例子理解

IEnumerator LoadSC()
{
    #region  处于scene01

    GameObject go0 = Instantiate(prefab);
    go0.name = SceneManager.GetActiveScene().name + "_cube";
    //go.transform.SetParent(this.transform);

    yield return SceneManager.LoadSceneAsync("scene02", LoadSceneMode.Additive);
    Debug.Log("[log1] >> " + SceneManager.GetActiveScene().name); //scene01

    Scene _scene02 = SceneManager.GetSceneByName("scene02");
    yield return new WaitUntil(() => _scene02.isLoaded);
    Debug.Log(SceneManager.sceneCount + " / " + _scene02.isLoaded);

    SceneManager.MoveGameObjectToScene(go0, _scene02); //go必须在scene1的root下

    #endregion

    #region  处于scene02

    // 处于scene02
    SceneManager.SetActiveScene(_scene02);
    Debug.Log("[log2] >> " + SceneManager.GetActiveScene().name);

    GameObject go1 = Instantiate(prefab); //会创建到当前Active的场景
    go1.name = SceneManager.GetActiveScene().name + "_cube";

    #endregion
}
好的,我理解了您的问题。首先,我们需要准备好背包的预制体,并将其打包成AssetBundle。在Unity中,可以通过以下步骤来完成: 1. 创建一个空的场景,并在场景中添加背包预制体。 2. 在菜单栏中选择 "AssetBundles" -> "Build AssetBundles"。 3. 在弹出的窗口中,选择要打包的平台和输出路径,然后点击 "Build" 按钮。 打包完成后,我们需要在代码中加载AssetBundle并实例化背包预制体。可以参考以下代码: ```csharp using UnityEngine; using System.Collections; public class LoadAssetBundle : MonoBehaviour { public string assetBundleUrl; public string assetName; IEnumerator Start () { // 加载AssetBundle using (WWW www = new WWW(assetBundleUrl)) { yield return www; AssetBundle bundle = www.assetBundle; // 实例化背包预制体 GameObject prefab = bundle.LoadAsset<GameObject>(assetName); Instantiate(prefab); // 卸载AssetBundle bundle.Unload(false); } } } ``` 在上述代码中,`assetBundleUrl` 是AssetBundle的路径,`assetName` 是要加载的预制体名称。加载完成后,我们需要通过 `Instantiate` 方法创建背包的实例。 接下来,我们需要编写一个 `UImanager` 脚本来控制背包的打开和关闭。可以参考以下代码: ```csharp using UnityEngine; using UnityEngine.UI; public class UImanager : MonoBehaviour { public GameObject bag; public void ToggleBag () { bag.SetActive(!bag.activeSelf); } } ``` 在上述代码中,`bag` 是背包的实例,`ToggleBag` 方法用于切换背包的状态。在UI中,我们可以通过添加一个按钮,并将其绑定到 `ToggleBag` 方法来实现打开和关闭背包的功能。 希望以上内容能够对您有所帮助。如有任何问题,请随时与我联系。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值