Unity:商业游戏底层资源加载框架学习 (一)

本文是学习Siki学院Ocean老师的《Unity商业游戏底层资源加载框架(Unity2018.1)》视频课程的学习笔记和总结

程序集

Unity中自定义程序集,需要进行依赖设定,设置完成之后,点击Apply保存

资源加载方式

 

  1. 直接挂载到组件上
  2. 通过Resources.Load()的方式加载:要加载的资源必须放置在Resources文件夹下面
  3. 通过AssetBundle方式加载:绝大部分公司都是使用这种方式加载
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEditor;
    
    public class BundleEditor
    {
        /// <summary>
        /// 打包AssetBundle包
        /// </summary>
        [MenuItem("Tools/Build Bundle")]
        public static void Build()
        {
            BuildPipeline.BuildAssetBundles(Application.streamingAssetsPath, BuildAssetBundleOptions.ChunkBasedCompression, EditorUserBuildSettings.activeBuildTarget);
            AssetDatabase.Refresh();
        }
    }
    
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class ResourceTest : MonoBehaviour
    {
        private void Start()
        {
            //使用AssetBundle加载资源,并实例化
            AssetBundle bundle = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/attack");
            GameObject prefab = bundle.LoadAsset<GameObject>("attack");
            GameObject.Instantiate(prefab);
        }
    
    }
  4. 通过AssetDataBase.LoadAtPtah()方式加载  

XML序列化和反序列化

using System.Collections.Generic;
using System.Xml.Serialization;

[System.Serializable]
public class TestSerialize
{
    [XmlAttribute("Id")]
    public int Id { get; set; }
    [XmlAttribute("Name")]
    public string Name { get; set; }
    [XmlElement("List")]
    public List<int> List = new List<int>();
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Xml.Serialization;

public class Test: MonoBehaviour
{
    private void Start()
    {
        //SerializeTest();
        DeseralizeTest();
    }


    private void SerializeTest()
    {
        TestSerialize testSerialize = new TestSerialize()
        {
            Id = 1,
            Name = "测试",
            List = new List<int>() { 2, 3 }
        };
        XmlSerialize(testSerialize);
    }

    private void DeseralizeTest()
    {
        TestSerialize testSerialize = XmlDeserialize();
        Debug.LogFormat("Id:{0},Name:{1}", testSerialize.Id, testSerialize.Name);
        foreach (var item in testSerialize.List)
        {
            Debug.Log(item);
        }
    }

    /// <summary>
    /// 序列化
    /// </summary>
    /// <param name="testSerialize"></param>
    private void XmlSerialize(TestSerialize testSerialize)
    {
        FileStream fileStream = null;
        StreamWriter sw = null;
        try
        {
            fileStream = new FileStream(Application.dataPath + "/test.xml", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
            sw = new StreamWriter(fileStream, System.Text.Encoding.UTF8);
            XmlSerializer xs = new XmlSerializer(testSerialize.GetType());
            xs.Serialize(sw, testSerialize);
        }
        catch (System.Exception e)
        {
            Debug.LogError(e);
        }
        finally
        {
            if (sw != null)
                sw.Close();
            if (fileStream != null)
                fileStream.Close();
        }
    }

    /// <summary>
    /// 反序列化
    /// </summary>
    /// <returns></returns>
    private TestSerialize XmlDeserialize()
    {
        FileStream fs = null;
        try
        {
            fs = new FileStream(Application.dataPath + "/test.xml", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
            XmlSerializer xs = new XmlSerializer(typeof(TestSerialize));
            TestSerialize testSerialize = xs.Deserialize(fs) as TestSerialize;
            return testSerialize;
        }
        catch (System.Exception e)
        {
            Debug.LogError(e);
        }
        finally
        {
            if (fs != null)
                fs.Close();
        }
        return null;
    }
}

二进制序列化和反序列化

using System.Collections.Generic;

[System.Serializable]
public class TestSerialize
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<int> List = new List<int>();
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

public class Test: MonoBehaviour
{
    private void Start()
    {
        //BinarySerializeTest();
        BinaryDeserializeTest();
    }


    
    #region Binary
    private void BinarySerializeTest()
    {
        TestSerialize testSerialize = new TestSerialize()
        {
            Id = 10010,
            Name = "BinarySerialize测试",
            List = new List<int>() { 100, 150 }
        };
        BinarySeralize(testSerialize);
    }

    /// <summary>
    /// 二进制序列化
    /// </summary>
    /// <param name="testSerialize"></param>
    private void BinarySeralize(TestSerialize testSerialize)
    {
        FileStream fs = null;
        try
        {
            fs = new FileStream(Application.dataPath + "/test.bytes", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(fs, testSerialize);
        }
        catch (System.Exception e)
        {
            Debug.LogError(e);
        }
        finally
        {
            if (fs != null)
                fs.Close();
        }
    }

    private void BinaryDeserializeTest()
    {
        TestSerialize testSerialize = BinaryDeseralize();
        Debug.LogFormat("Id:{0},Name:{1}", testSerialize.Id, testSerialize.Name);
        foreach (var item in testSerialize.List)
        {
            Debug.Log(item);
        }
    }

    /// <summary>
    /// 二进制反序列化
    /// </summary>
    /// <returns></returns>
    private TestSerialize BinaryDeseralize()
    {
        FileStream fs = null;
        try
        {
            fs = new FileStream(Application.dataPath + "/test.bytes", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
            BinaryFormatter bf = new BinaryFormatter();
            TestSerialize testSerialize = bf.Deserialize(fs) as TestSerialize;
            return testSerialize;
        }
        catch (System.Exception e)
        {
            Debug.LogError(e);
        }
        finally
        {
            if (fs != null)
                fs.Close();
        }
        return null;
    }
    #endregion
}

Assets序列化

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

[CreateAssetMenu(fileName = "TestAssets",menuName ="CreateAssets",order = 0)]
public class AssetsSerialize : ScriptableObject
{
    public int Id;
    public string Name;
    public List<string> TestList;
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ResourceTest : MonoBehaviour
{
    private void Start()
    {
        ReadTestAssets();
    }

    #region Assets
    private void ReadTestAssets()
    {
        AssetsSerialize assetsSerialize = UnityEditor.AssetDatabase.LoadAssetAtPath<AssetsSerialize>("Assets/Scripts/TestAssets.asset");
        Debug.Log(assetsSerialize.Id);
        Debug.Log(assetsSerialize.Name);
        foreach (var item in assetsSerialize.TestList)
        {
            Debug.Log(item);
        }
    }
    #endregion

}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值