Unity 需要实现场景跳转,然会返回到原场景的功能

原场景中只有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找到,然后再去找内部要调用的组件。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值