原场景中只有UI(UGUI),加载的3D场景是从AB包中加载的,使用的是原场景不删除的办法
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class TEST : MonoBehaviour {
public Button btn;
public void Start()
{
btn.onClick.AddListener(() => Load());
}
public void Load()
{
AsyncOperation ass=SceneManager.LoadSceneAsync("02",LoadSceneMode.Additive);
}
}
代码比较简单 主要是了解
SceneManager.LoadSceneAsync(异步加载)的参数含义:
《1》public static AsyncOperation LoadSceneAsync(int sceneBuildIndex, LoadSceneMode mode);
int sceneBuildIndex:是在scenes in build中场景的下标(一般不建议使用该方法)
LoadSceneMode mode:LoadScenesMode 是个枚举 有两种
// 摘要:
// Used when loading a scene in a player.
public enum LoadSceneMode
{
// 摘要:
// Closes all current loaded scenes and loads a scene.
Single = 0,
//
// 摘要:
// Adds the scene to the current loaded scenes.
Additive = 1,
}
single:关闭所有当前加载的场景并加载场景。
Additive :将场景添加到当前加载的场景中
但是加载的场景删除后,原来场景的UI的一些UI组件不能获取使用,
解决办法:
Transform transform = GameObject.Find("UIPanelTest01(Clone)").transform;
deviceText = transform.Find("DeviceName/Text").GetComponent<Text>();
deviceText.text = name;
deviceName = transform.Find("DeviceName").GetComponent<RectTransform>();
deviceName.position = Input.mousePosition;
先通Find找到,然后再去找内部要调用的组件。