代码来自烟雨迷离半世殇Unity作品展示:坦克大战(基于GameFramework)_设计师养成记-CSDN博客游戏名称:Battle City游戏素材:SIKI学院,爱给网开发平台:Unity 2018.3.1f1游戏平台:WinX游戏版本:1.0游戏用途:仅供学习交流使用,若用作商业,后果自负。游戏类型:经典坦克大战,微创新游戏介绍:加入商城模块,任务模块,存读档功能,技能背包系统,Buff系统源码下载:https://blog.csdn.net/qq_1502054...https://blog.csdn.net/qq_15020543/article/details/86760793
非常棒!推荐大家去看
游戏过程中需要加载的图片全部为SpritesAsset继承ScriptableObject。
namespace GameMain.Scripts.CostumAssets
{
[Serializable]
public class SpriteItem
{
/// <summary>
/// 精灵名称
/// </summary>
public string m_SpriteName;
/// <summary>
/// 精灵
/// </summary>
public Sprite Sprite;
}
}
namespace GameMain.Scripts.CostumAssets
{
public class SpritesAsset : ScriptableObject
{
/// <summary>
/// 精灵数组
/// </summary>
[SerializeField] public List<SpriteItem> m_SpritesAssets = new List<SpriteItem>();
}
}
游戏开始后如何加载资源?
GameEntry.Resource.LoadAsset(AssetUtility.GetSpriteAsset(), OnLoadSpriteAssetSuccess);
其中,AssetUtility.GetSpriteAsset()位于工具类中,获得asset
建立工具类,方便资源的加载
namespace GameMain.Scripts.Utility
{
public static class AssetUtility
{
public static string GetDataTableAsset(string assetName, LoadType loadType)
{
return GameFramework.Utility.Text.Format("Assets/GameMain/DataTables/{0}.{1}", assetName,
loadType == LoadType.Text ? "txt" : "bytes");
}
public static string GetSceneAsset(string assetName)
{
return GameFramework.Utility.Text.Format("Assets/GameMain/Scenes/{0}.unity", assetName);
}
public static string GetSoundAsset(string assetName)
{
return GameFramework.Utility.Text.Format("Assets/GameMain/Sounds/{0}.wav", assetName);
}
public static string GetEntityAsset(string assetName)
{
return GameFramework.Utility.Text.Format("Assets/GameMain/Entities/{0}.prefab", assetName);
}
public static string GetUIFormAsset(string assetName)
{
return GameFramework.Utility.Text.Format("Assets/GameMain/UI/UIForms/{0}.prefab", assetName);
}
public static string GetSpriteAsset()
{
return "Assets/GameMain/CostumAssets/SpritesAsset.asset";
}
}
}
public LoadAssetCallbacks
OnLoadSpriteAssetSuccess = new LoadAssetCallbacks(loadSpritsAssetCallback);
OnLoadSpriteAssetSuccess则是一个回调,在加载成功后回调,回调内容为:
private static void loadSpritsAssetCallback(string assetname, object asset, float duration, object userdata)
{
m_SpritesAsset = asset as SpritesAsset;
}
SpritesAsset 类型的参数记录了这个回调返回的结果
public static SpritesAsset m_SpritesAsset { get; set; }
最后是资源在游戏中的使用:
transform.GetComponent<SpriteRenderer>().sprite = GameManager.m_SpritesAsset.m_SpritesAssets[6].Sprite;
根据之前定义的结构进行读取即可!非常方便
public class SpriteItem
{
/// <summary>
/// 精灵名称
/// </summary>
public string m_SpriteName;
/// <summary>
/// 精灵
/// </summary>
public Sprite Sprite;
}
|