UnityVR--ResourceManager--Resource Management

Table of contents

Introduction

Several ways to load resources

Resource loading manager Resload.cs

Application example of ResLoad class


Introduction

  A resource management tool set is recorded here, providing some methods to load some Objects and Prefabs directly from the Assets folder into the scene.

Several ways to load resources

  In projects we often need to use something ready to use, which may be a prefab format, or any format, such as materials, documents or even scenes. In the previous article, the following methods 1 and 2 were mainly used to obtain:

  1. Set public variables and specify corresponding resources in the panel

  This method has been used a lot before, just make a variable public in the script, and then drag the corresponding object into the panel. like:  

public GameObject Bullet;//加入一个子弹

 

  Pros : Intuitive

  Disadvantage : Once the script is modified or remounted, it is easy to forget the setting

  2. Find in the scene

  That is, load the resource into the scene from the beginning, and use GameObject.Find to find it in the script. For example, in the robot arm scene 2 , the script finds the child node of the robot arm according to the path:

  Advantages : convenient, no need to do too many settings

  Disadvantages : GameObject.Find or transform.Find, etc., will traverse all nodes (including all child nodes) on the field during runtime, which consumes resources.

  3. Load from the Assets folder

 

   Place the resources under the "Assets/Resources" folder, and the name of this folder cannot be changed. When needed, use Unity's API "Resources.Load" to load. The definition is as follows, where the parameter path is based on the Resources folder as the parent directory:

        public static Object Load(string path)

        public static T Load<T>(string path) where T : Object

Resource loading manager Resload.cs

  There are many useful resource loading methods in Unity's Resources class. On its basis, we can create a resource management file ResLoad.cs according to the needs of our own projects, so that we can easily call various types of files in the project. The following ResLoad.cs defines 3 methods for loading resources, including generic methods, so that any type of file can be loaded, and other methods may be added later as needed.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//从Assets加载资源的方法集合
public class ResLoad : SingleMono<ResLoad>  //继承Mono的单例
{
    public GameObject LoadPrefab(string resName)
    {//通过资源名添加预制体,资源需要放在Resources根目录
        GameObject go = Resources.Load<GameObject>(resName);
        if(go==null)
        {//判断是否加载成功
            Debug.LogError(resName+"ResLoad:Resources加载路径失败");
            return null;
        }
        return Instantiate(go);  //如果加载成功,则实例化它
        //因为ResLoad类是继承了SingleMono<ResLoad>的单例,
        //这个单例是继承MonoBehaviour的,因此直接可以使用Instantiate方法实例化
        //否则就需要用GameObject来调用
        //return GameObject.Instantiate(go);
    }

    public T LoadAsset<T>(string pathName,string resName) where T:Object
    {//通过路径加载资源,当然也可以先判空一下
        return Resources.Load<T>(pathName + "/" + resName);
    }

    public T LoadAsset<T>(string resName)
        where T :Object
    {//通过资源名加载,比如这个资源直接放在根目录下
        return Resources.Load<T>(resName);
    }
}

Application example of ResLoad class

 The ResLoad class inherits SingleMono, so there is no need to create a new object every time when using it, and the only instance in the project can be created or accessed by using the Instance field. The following is an example call:

  1. In the toolset Tools.cs, load the material file named "line" in the Resources folder:

line.material=Resload.Instance.LoadAsset<Material>("line"); 

  2. When the protagonist uses a bullet, press the Space button to load the prefab named "Bullet" in the Resources folder:

Bullet = Resload.Instance.LoadPrefab("Bullet");

Guess you like

Origin blog.csdn.net/tangjieitc/article/details/130846946