IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 游戏开发 -> Unity官方案例-Addressables-Sample-笔记(4)-Sprite Land(Sprite加载) -> 正文阅读

[游戏开发]Unity官方案例-Addressables-Sample-笔记(4)-Sprite Land(Sprite加载)

记录一下Addressables-Sample项目中的脚本和学习的笔记,主要是熟悉API及其使用方式.方便日后工作查询.

项目地址:

github: Addressables-Sample

Unity官方教程

Bilibili-Unity官方教程 Addressables【中文字幕】

4. Sprite Land

本示例中目前演示了三种sprite访问方法。

场景说明

Scenes/SampleScene

  • 首先是直接对单个sprite进行AssetReference。因为这个精灵是单个条目,所以我们可以直接引用资源并获取精灵。这是最简单的例子。

  • 第二种方法是从sprite表中访问sprite。这是导致撞车的原因,但现在应该修复。在这里,我们将sprite表资产作为IList类型加载。这告诉addressables加载所有作为sprite的子对象,并将它们作为列表返回。

  • 第三种是从地图册中访问精灵。在这种情况下,必须使用addressables加载sprite atlas,然后使用普通atlasapi从中加载给定的sprite。本例还显示了扩展AssetReference以提供可寻址项不附带的类型化引用(本例中为AssetReference)。

脚本说明

Scripts/SpriteControlTest.cs

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor.AddressableAssets.GUI;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.SceneManagement;
using UnityEngine.U2D;
using UnityEngine.UI;

public class SpriteControlTest : MonoBehaviour
{
    public AssetReferenceSprite singleSpriteReference;
    
    public AssetReference spriteSheetReference;
    
    
    public AssetReferenceAtlas spriteAtlasReference;
    public AssetReference spriteSubAssetReference;
    public AssetReference atlasSubAssetReference;
    
    
    public List<SpriteRenderer> spritesToChange;

    public Button button;
    public Text buttonText;

    int m_ClickCount = 0;
    public void OnButtonClick()
    {
        button.interactable = false;
        m_ClickCount++;
        switch (m_ClickCount)
        {
            case 1:
                singleSpriteReference.LoadAssetAsync<Sprite>().Completed += SingleDone;
                break;
            case 2:
                spriteSheetReference.LoadAssetAsync<IList<Sprite>>().Completed += SheetDone;
                break;
            case 3:
                spriteAtlasReference.LoadAssetAsync().Completed += AtlasDone;
                break;
            case 4:
                spriteSubAssetReference.LoadAssetAsync<Sprite>().Completed += SheetSubDone;
                break;
            case 5:
                atlasSubAssetReference.LoadAssetAsync<Sprite>().Completed += AtlasSubDone;
                break;
            case 6:
                Addressables.LoadAssetAsync<Sprite>("sheet[sprite_sheet_4]").Completed += SheetNameSubDone;
                break;
            case 7:
                Addressables.LoadAssetAsync<Sprite>("Atlas[u7]").Completed += AtlasNameSubDone;
                break;
        }
    }
    
    

    void SingleDone(AsyncOperationHandle<Sprite> op)
    {
        if (op.Result == null)
        {
            Debug.LogError("no sprites here.");
            return;
        }
        
        spritesToChange[0].sprite = op.Result;
        
        button.interactable = true;
        buttonText.text = "Change with sheet list";
    }

    void SheetDone(AsyncOperationHandle<IList<Sprite>> op)
    {
        if (op.Result == null)
        {
            Debug.LogError("no sheets here.");
            return;
        }

        spritesToChange[1].sprite = op.Result[5];
        
        button.interactable = true;
        buttonText.text = "Change with atlas";
    }

    void AtlasDone(AsyncOperationHandle<SpriteAtlas> op)
    {
        if (op.Result == null)
        {
            Debug.LogError("no atlases here.");
            return;
        }

        spritesToChange[2].sprite = op.Result.GetSprite("u3");
        
        button.interactable = true;
        buttonText.text = "Change with sprite sub-object ref";
    }

    void SheetSubDone(AsyncOperationHandle<Sprite> op)
    {
        if (op.Result == null)
        {
            Debug.LogError("no sprite in sheet here.");
            return;
        }

        spritesToChange[3].sprite = op.Result;
        
        button.interactable = true;
        buttonText.text = "Change with atlas sub-object ref";
    }

    void AtlasSubDone(AsyncOperationHandle<Sprite> op)
    {
        if (op.Result == null)
        {
            Debug.LogError("no sprite in atlas here.");
            return;
        }

        spritesToChange[4].sprite = op.Result;
        
        button.interactable = true;
        buttonText.text = "Change with sprite[name]";
    }

    void SheetNameSubDone(AsyncOperationHandle<Sprite> op)
    {
        if (op.Result == null)
        {
            Debug.LogError("no sprite in sheet here.");
            return;
        }

        spritesToChange[5].sprite = op.Result;
        
        button.interactable = true;
        buttonText.text = "Change with atlas[name]";
    }

    void AtlasNameSubDone(AsyncOperationHandle<Sprite> op)
    {
        if (op.Result == null)
        {
            Debug.LogError("no sprite in atlas here.");
            return;
        }

        spritesToChange[6].sprite = op.Result;
        
        buttonText.text = "The End";
    }

    void Start()
    {
        Addressables.InitializeAsync();
    }

}

[Serializable]
public class AssetReferenceAtlas : AssetReferenceT<SpriteAtlas>
{
    public AssetReferenceAtlas(string guid) : base(guid) { }
}

  • AssetReferenceAtlas 继承至AssetReferenceT,是自定义SpriteAtlas类型的AssetReference

  • 加载Sprite,

    • singleSpriteReference.LoadAssetAsync()
  • 加载SubSprite

    • spriteSheetReference.LoadAssetAsync<IList>
  • 加载图集SpriteAtlas

    • spriteAtlasReference.LoadAssetAsync()
  • 加载SubSprite里面的单张Sprite

    • spriteSubAssetReference.LoadAssetAsync()
  • 加载SpriteAtlas里面的单张Sprite

    • atlasSubAssetReference.LoadAssetAsync()
  • 按可寻址路径加载Sprite里面的单张Sprite

    • Addressables.LoadAssetAsync(“sheet[sprite_sheet_4]”)
  • 按可寻址路径加载SpriteAtlas里面的单张Sprite

    • Addressables.LoadAssetAsync(“Atlas[u7]”)
  游戏开发 最新文章
6、英飞凌-AURIX-TC3XX: PWM实验之使用 GT
泛型自动装箱
CubeMax添加Rtthread操作系统 组件STM32F10
python多线程编程:如何优雅地关闭线程
数据类型隐式转换导致的阻塞
WebAPi实现多文件上传,并附带参数
from origin ‘null‘ has been blocked by
UE4 蓝图调用C++函数(附带项目工程)
Unity学习笔记(一)结构体的简单理解与应用
【Memory As a Programming Concept in C a
上一篇文章      下一篇文章      查看所有文章
加:2021-07-29 11:59:42  更:2021-07-29 11:59:50 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/20 19:09:14-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码