Unity Addressable

Unity important directory

insert image description here

Several important directories in the project

Assets Store resources, code, configuration
Library After most of the resources are imported into the Assets directory, they will be converted into files recognized by Unity, and the converted files will be stored in this directory
Logs log file
Packages third-party plug-ins
ProjectSettings Store various project settings
UserSettings User preferences, such as how the editor is laid out

When you want to migrate a project or copy the project to others, you only need to back up the three directories of Assets, Packages and ProjectSettings

Addressable

Addressable is an addressable resource management system based on Asset Bundle.
Advantages:

  1. Automatically manage AB package packaging, publishing, and loading
  2. It is more convenient to load local and remote resources
  3. Analyze resource dependencies
  4. Easier memory management

resource grouping

Install Addressable through Package Manager, and then click Groups
insert image description here
to create Addressables Settings for the first use. After creation, an AddressableAssetsData folder will be created in the Assets directory to save related configurations.
insert image description here
These configurations use ScriptableObject

The Addressables Groups interface is used for resource planning. Right-click on the blank space to create different groups.
insert image description here
insert image description here
Every time a new group is created, a corresponding configuration file will be generated by default to record the packaging and loading settings. When packaging, a group will be labeled as one or more ABs Bag

Drag the resource directly into a group, and it becomes an addressable resource. You
insert image description here
can also check Addressable in the Inspector. By
insert image description here
default, the path will be used as the Addressable Name (or Address), which is the unique identifier of the resource. You can also manual modification
insert image description here

Notice:

  1. C# code cannot be used as an addressable resource
  2. If the resources under the Resources folder become addressable resources, they will be moved into the Resources_ moved folder.
    Reason: The resources under the Resources folder will eventually be packaged out. If it becomes an addressable resource, it means that you want to manage it through Addressables,
    so it is unnecessary Load and package through Resources, so it will be automatically migrated to avoid repeated packaging

Labels on the right add tags to resources, such as low-definition version, high-definition version, level 1, level 2, and then you can only load resources of a certain label

Right click on the resource option
insert image description here

Move Addressables to Group Move resources to the specified Group
Move Addressables to New Group Create a new Group with the same settings as the current Group, move resources to this Group
Rmove Addressables Remove resource, become unaddressable
Simplify Addressable Names Simplifies addressable resource names, removes paths and extensions from names, and simplifies and shortens names
Copy Address to Clipboard copy address to clipboard
Change Address change name
Create New Group Create a new Group

Right click on the group option
insert image description here

Remove Group(s) Remove the group, and all resources in the group will return to unaddressable resources
Simplify Addressable Names simplified name
Rmove Addressables Remove resource, become unaddressable
Set as Default Set as the default group, when you directly check Addressable in the resource, it will automatically join this group
Inspect Group Setting Quickly select associated group-related profiles
Rename double naming
Create New Group create new group

Profile configuration overview related

insert image description here
insert image description here
Manage Profiles: Manage configuration files
Configure packaging platform, local and remote packaging, load path information

Tools tool related

insert image description here

Inspect System Settings Check system settings
Check for content Update Restrictions Check Content Update Limits
Window Open the Addressables related window
Groups View Show Sprite and Subobject Addressable: Display the sprites and subobjects of addressable objects. Generally, you can check this option when you want to see the content in the atlas resource
Group Hierarchy with Dashes: Group Hierarchy with Dashes

How to load resources in Play Mode Script editing mode

insert image description here

Use Asset Database (fastest) Use the AssetDatabase related interface to load resources directly, without packaging ab package, use it in the development stage
Simulate Groups (advanced) It is used in the test phase, and the ab package will not be packaged. A time delay is added to the code to simulate the download situation
Use Existing Build To be used in the release phase, you need to pack the ab package and configure the remote download path

Build (build packaging related)

insert image description here

New Build Build resources (equivalent to packaging resource groupings), providing a default packaging script
Update a Previour Build update previous version
Clean Build Empty the previous build resources

load resources

Loading via resource identifier class

using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;

public class Test : MonoBehaviour
{
    
    
    //通用资源标识类可以用来加载任意类型资源
    public AssetReference assetReference;
    //只能加载图集资源
    public AssetReferenceAtlasedSprite asReference;
    //只能加载prefab资源
    public AssetReferenceGameObject prefabReference;
    //加载精灵图片或图集中的某个图片
    public AssetReferenceSprite spriteReference;
    //只能加载贴图资源
    public AssetReferenceTexture textureReference;
    //指定资源类型
    public AssetReferenceT<AudioClip> audioReference;
    public AssetReferenceT <RuntimeAnimatorController> controller;
    public AssetReferenceT<TextAsset> textReference;

    private void Start()
    {
    
    
        AsyncOperationHandle<GameObject> handle = assetReference.LoadAssetAsync<GameObject>();
        handle.Completed += OnLoadCompleted;
    }

    private void OnLoadCompleted(AsyncOperationHandle<GameObject> handle)
    {
    
    
        if (handle.Status == AsyncOperationStatus.Succeeded)
        {
    
    
            //handle.Result就是加载的对象
            GameObject go = Instantiate(handle.Result);
        }
    }
}

insert image description here
Click the circle on the right to select the specified resource, and drag the non-addressable resource to add it to the default Group

Loading and unloading by AddressableName

The APIs related to Addressable loading are asynchronous, and the synchronous method has been deprecated.
Asynchronous loading and unloading, resources can be local or remote server

private AsyncOperationHandle<GameObject> _asyncOperationRes;
private AsyncOperationHandle<GameObject> _asyncOperationInst;

private void Load()
{
    
    
    //加载资源
    Addressables.LoadAssetAsync<GameObject>("AddressableName").Completed += (handle) =>
    {
    
    
        if (handle.Status == AsyncOperationStatus.Succeeded)
        {
    
    
            _asyncOperationRes = handle;
            // 加载实例
            Addressables.InstantiateAsync(handle.Result).Completed += (inst) =>
            {
    
    
                _asyncOperationInst = inst;
            };
        }
    };
}

private void Unload()
{
    
    
    // 释放实例
    Addressables.ReleaseInstance(_asyncOperationInst);

    // 释放资源
    Addressables.Release(_asyncOperationRes);
}

Load and unload scenes

using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.ResourceManagement.ResourceProviders;
using UnityEngine.SceneManagement;

public class Test2 : MonoBehaviour
{
    
    
    SceneInstance _loadedScene;//场景句柄
    
    public void LoadSceneAsync()
    {
    
    
        Addressables.LoadSceneAsync("SceneName", LoadSceneMode.Additive).Completed += (handle) =>
        {
    
    
            if (handle.Status == AsyncOperationStatus.Succeeded)
            {
    
    
                _loadedScene = handle.Result;
                //do something
            }
        };
    }

    public void UnloadSceneAsync()
    {
    
    
        Addressables.UnloadSceneAsync(_loadedScene).Completed += (handle) =>
        {
    
    
            if (handle.Status == AsyncOperationStatus.Succeeded)
            {
    
    
                _loadedScene = new SceneInstance();
                //do something
            }
        };
    }
}

analysis resources

insert image description here
The analysis window helps us analyze the resource redundancy in the project. It is divided into Fixable Rules and Unfixable Rules. Click a rule, and then click Analyze Selected Rules to analyze it. For fixable rules, for example, A references C, and B also references C, clicking Fix Selected Rules will create a new Group and put C into it.

reference

Unity Advanced Addressable

Guess you like

Origin blog.csdn.net/sinat_34014668/article/details/131029724