Unity3d进阶学习(8)-- 自定义资源配置文件、序列化

一、资源配置文件、序列化的介绍

资源配置文件:在游戏开发中,经常会用到一些配置文件保存一些数据,然后项目运行中读取这些配置文件中的数据在游戏中使用。比如 XML、JSON、Protobuf、Excel、TXT等等。

序列化:就是将数据的状态信息转换为可以存储或传输的形式的过程。比如将游戏里面的数据写进文件里, 包括角色的生命, 攻击, 防御等等. 这样当游戏需要调整时 (比如平衡性问题), 就只需要对文件进行改写, 而不需要将游戏进行重新编译. 玩家在更新的时候也只需要下载几个被改写的文件即可.

1.变量 –> 2.序列化格式数据 –> 3.保存为文件到硬盘 –> 4.加载文件 –> 5.读取序列化格式数据 –> 6.变量
1到2就是序列化,5到6就是反序列化

二、unity的自定义配置文件

接下来说下使用可序列化类保存配置,介绍一个Unity的配置文件(.asset)。
优点:当我们需要将游戏资源里的贴图(Texture)、游戏对象(Gameobject)等预设体保存到配置文件时,这时我们就可以使用该配置文件,但是当关联的预设体丢失时,需要重新将预设体关联起来。

三、通过案例深入理解

下面做一个小案例
1. 玩家通过鼠标点击屏幕创建出不同的对象(玩家通过配置文件自定义),创建出来的对象通过资源池进行管理
2. 每个类都有详细介绍,可以根据需求直接使用

创建序列化类资源池类【GameObjectPool】– 存储游戏资源
1. 该类声明为 [Serializable]
2. 需要序列化的变量将其设置为public或者设置其属性为[SerializeField]

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

[Serializable]
public class GameObjectPool {
    [SerializeField]
    private string m_name;
    [SerializeField]
    private GameObject m_perfab;
    [SerializeField]
    private int m_maxAmount;

    public string name
    {
        get
        {
            return m_name;
        }
    }
    public GameObject gameobject
    {
        get
        {
            return m_perfab;
        }
    }
    public int maxAmount
    {
        get
        {
            return m_maxAmount;
        }
    }

    //
    [NonSerialized]
    private List<GameObject> m_gameObjList = new List<GameObject>();
    public List<GameObject> gameObjList
    {
        get
        {
            if(m_gameObjList == null)
            {
                m_gameObjList = new List<GameObject>();
            }
            return m_gameObjList;
        }
    }

    //如果要优化,可以在创建一个list用来存放Active为false的物体,需要的时候直接获取就不用遍历了
    public GameObject GetObject()
    {
        foreach(GameObject obj in m_gameObjList)
        {
            if (!obj.activeInHierarchy)
            {
                obj.SetActive(true);
                return obj;
            }
        }
        if(m_gameObjList.Count >= m_maxAmount)
        {
            GameObject.Destroy(m_gameObjList[0]);
            m_gameObjList.RemoveAt(0);
        }

        GameObject prefab = GameObject.Instantiate(m_perfab, new Vector3(0, 3, 0), Quaternion.identity);
        m_gameObjList.Add(prefab);
        return prefab;
    }
}

存储所有资源池的类【GameObjectPoolList】 – 创建该类为配置文件
1. 类必须继承 ScriptableObject

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 存储所有的资源池  继承ScriptableObject变成可自定义的资源配置文件
/// </summary>
public class GameObjectPoolList: ScriptableObject
{
    [SerializeField]
    private List<GameObjectPool> m_poolList = new List<GameObjectPool>();
    public List<GameObjectPool> poolList
    {
        get
        {
            if(m_poolList == null)
            {
                m_poolList = new List<GameObjectPool>();
            }
            return m_poolList;
        }
    }
}

管理资源池的类【PoolManager】 – 外界通过访问这个类来访问资源池

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PoolManager {

    private static PoolManager m_instance = null;
    public static PoolManager Instance
    {
        get
        {
            if(m_instance == null)
            {
                m_instance = new PoolManager();
            }
            return m_instance;
        }
    }

    //文件路径
    private const string m_pathPrefix = "Assets/FrameWork/Resources/";
    private const string m_fileName = "Gameobjectpool";
    private const string m_pathPostfix = ".asset";
    public static string PoolConfigPath
    {
        get
        {
            return m_pathPrefix + m_fileName + m_pathPostfix;
        }
    }
    //字典  根据name查找对应的pool
    private Dictionary<string, GameObjectPool> m_poolDic;

    //构造函数
    PoolManager()
    {
        GameObjectPoolList poolList = Resources.Load<GameObjectPoolList>(m_fileName);//获取文件类

        m_poolDic = new Dictionary<string, GameObjectPool>();
        foreach(GameObjectPool pool in poolList.poolList)
        {
            m_poolDic.Add(pool.name, pool);
        }
    }

    public void Init()
    {
        //do nothing
    }

    public GameObject GetObjectFromPool(string poolName)
    {
        GameObjectPool pool;
        if(m_poolDic.TryGetValue(poolName, out pool))
        {
            return pool.GetObject();
        }
        else
        {
            Debug.LogWarning("poolName: " + poolName + "  is not exit");
            return null;
        }
    }
}

创建配置文件的类 【PoolManagerEditor】– 利用编译器方法创建 GameObjectPoolList 为配置文件
1. 工程下新建一个文件夹 Editor,在该文件夹下创建下面的类
2. 菜单栏中找 Manager –> Create GameObjectPoolConfig 点击运行就可以 PoolManager.PoolConfigPath 的路径下看到生成的配置文件

public class PoolManagerEditor
{
    //将这个方法做成一个菜单
    [MenuItem("Manager/Create GameObjectPoolConfig")]
    static void CreatePoolList()
    {
        //将对象实例化
        GameObjectPoolList poolList = ScriptableObject.CreateInstance<GameObjectPoolList>();

        //创建类为可编辑的配置文件
        AssetDatabase.CreateAsset(poolList, PoolManager.PoolConfigPath);
        AssetDatabase.SaveAssets();
    }
}

四、测试

1. 通过配置文件,自行配置资源
这里写图片描述

2. 代码测试

private void Update()
{
   if (Input.GetMouseButtonDown(0))
   {
      GameObject obj = PoolManager.Instance.GetObjectFromPool("Sphere");
   }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值