当前位置:   article > 正文

YooAsset资源管理_yooasset加载场景

yooasset加载场景

了解

阅读YooAsset文档,快速入门

测试环境

  1. Unity:Unity2021.3.6f1c1
  2. YooAsset:1.4.11

示例

准备加载资源

包名:Test Package
包内有一个Cube预制体和一个场景
启用Enable Addressable
在这里插入图片描述

加载资源

using System;
using System.Collections;
using UnityEngine;
using YooAsset;
public class YooAssetTest : MonoBehaviour
{
    public EPlayMode playMode;//设置加载YooAsset模式
    string packageName;//包名
    ResourcePackage package;

    private void Awake()
    {
        packageName = "Test Package";//编辑器中创建的YooAsset包名

        YooAssets.Initialize();

        package = YooAssets.CreatePackage(packageName);

        YooAssets.SetDefaultPackage(package);

        StartCoroutine(InitializeYooAsset(playMode, () =>
        {
            //异步加载资源 加载Cube
            /* var handle = YooAssets.LoadAssetAsync<GameObject>("Cube");
             handle.Completed += (value) =>
             {
                 var prefab = value.AssetObject as GameObject;
                 Instantiate(prefab, Vector3.zero, Quaternion.identity);
             };*/

            //异步加载场景
            var handle = YooAssets.LoadSceneAsync("New Scene");
            handle.Completed += (value) =>
            {
                Debug.Log("加载新的场景");
            };
        }));
    }

    private IEnumerator InitializeYooAsset(EPlayMode playMode, Action callBack = null)
    {
        InitializationOperation initializationOperation = null;

        if (playMode == EPlayMode.EditorSimulateMode)//编辑器中运行 无需构建
        {
            var createParameters = new EditorSimulateModeParameters();
            createParameters.SimulateManifestFilePath = EditorSimulateModeHelper.SimulateBuild(packageName);
            initializationOperation = package.InitializeAsync(createParameters);
        }
        else if (playMode == EPlayMode.OfflinePlayMode)//离线模式 需要构建
        {
            var createParameters = new OfflinePlayModeParameters();
            initializationOperation = package.InitializeAsync(createParameters);
        }
        else
        {
            Debug.Log("暂无联机模式");
            yield break;
        }

        yield return initializationOperation;

        if (package.InitializeStatus == EOperationStatus.Succeed)
        {
            Debug.Log("包初始化成功");

            callBack?.Invoke();
        }
        else
        {
            Debug.LogWarning($"{initializationOperation.Error}");
        }
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/86191
推荐阅读
相关标签
  

闽ICP备14008679号