通过简单的 ResourceManager 管理 XNA 中的资源,WPXNA(二)
平方已經開發了一些 Windows Phone 上的一些游戲,算不上什么技術大牛。在這里分享一下經驗,僅為了和各位朋友交流經驗。平方會逐步將自己編寫的類上傳到托管項目中,沒有什么好名字,就叫 WPXNA 吧,最后請高手繞道而行吧,以免浪費時間。(為了突出重點和減少篇幅,有些示例代碼可能不夠嚴謹。)
資源的類型
在一些簡單的 2D 游戲設計中,我們通常用到的資源是一些字體,圖像和聲音。所以這里就不涉及視頻和模型這些內容了,要制作視頻和 3D 模型往往需要花費很多時間。
這里,平方通過枚舉類型 ResourceType 來區分不同的資源:
internal enum ResourceType {Image,Font,Sound,Music, }在上面的代碼中,Image 表示圖像資源,Font 表示字體資源。Sound 和 Music 都表示聲音資源,不同的是 Music 通過 MediaPlayer 類播放(一般是 mp3 文件),而 Sound 一般指一些小的 wav 文件。
標識資源
平方創建了一個 Resource 結構來標識一個資源,他有三個公開的字段,Name 是資源的名稱,這個名稱不能和其他資源重復。Type 是資源的類型,也就是上面提到的 ResourceType。Path 是資源的訪問路徑,也就是資源文件在項目中的位置,如下圖所示:
internal struct Resource {internal readonly string Name;internal readonly string Path;internal readonly ResourceType Type;internal Resource ( string name, ResourceType type, string path ){if ( string.IsNullOrEmpty ( name ) || string.IsNullOrEmpty ( path ) )throw new ArgumentNullException ( "name, path", "name, path can't be null" );this.Name = name;this.Type = type;this.Path = path;}}因為,只有正確的載入資源,游戲才能正常運行,所以在 Resource 類的構造函數中,平方對參數作了一些檢查。
管理資源
然后,我們可以通過 ResourceManager 來管理資源。
在構造函數中,我們接受一個 Resource 結構數組,他們也就是需要管理的資源。
internal readonly IList<Resource> Resources;internal ResourceManager ( IList<Resource> resources ) { this.Resources = null == resources ? new Resource[] { } : resources; }當然,ResourceManager 本身并不能夠載入資源,所以我們需要使用 ContentManager 類:
private ContentManager contentManager; internal World World;private readonly Dictionary<string, Texture2D> textures = new Dictionary<string, Texture2D> ( ); private readonly Dictionary<string, SpriteFont> fonts = new Dictionary<string, SpriteFont> ( ); private readonly Dictionary<string, SoundEffectInstance> sounds = new Dictionary<string, SoundEffectInstance> ( ); private readonly Dictionary<string, Song> music = new Dictionary<string, Song> ( );public void LoadContent ( ) {if ( null == this.contentManager )this.contentManager = new ContentManager ( this.World.Services, contentDirectory );try{foreach ( Resource resource in this.Resources )switch ( resource.Type ){case ResourceType.Image:this.textures.Add ( resource.Name, this.contentManager.Load<Texture2D> ( resolution + resource.Path ) );break;case ResourceType.Font:this.fonts.Add ( resource.Name, this.contentManager.Load<SpriteFont> ( resource.Path ) );break;case ResourceType.Sound:this.sounds.Add ( resource.Name, this.contentManager.Load<SoundEffect> ( resource.Path ).CreateInstance ( ) );break;case ResourceType.Music:this.music.Add ( resource.Name, this.contentManager.Load<Song> ( resource.Path ) );break;}}catch { }}調用 ResourceManager 的 LoadContent 方法來載入資源,在 LoadContent 方法中,我們會創建一個新的 ContentManager 對象,并通過他的 Load 方法載入我們事先接受的資源。
要創建 ContentManager,我們還需要設置 ResourceManager 的 World 字段(你也可以將他修改為屬性)。所以,在調用 LoadContent 之前,你需要確保 World 字段不為空。
當你覺得這些資源已經沒有用了的時候,調用 UnloadContent 方法來釋放資源:
public void UnloadContent ( ) {foreach ( Texture2D texture in this.textures.Values )texture.Dispose ( );foreach ( SoundEffectInstance sound in this.sounds.Values )sound.Dispose ( );foreach ( Song song in this.music.Values )song.Dispose ( );this.textures.Clear ( );this.fonts.Clear ( );this.sounds.Clear ( );this.music.Clear ( );if ( !this.Resources.IsReadOnly )this.Resources.Clear ( );if ( null != this.contentManager )this.contentManager.Unload ( );}在上面的代碼中,我們先對所有的資源調用了 Dispose 方法,然后調用了 ContentManager 的 Unload 方法。
使用資源
最后,我們在 World 類中具體的使用一下 ResourceManager 類。
private readonly ResourceManager resourceManager;public World ( Color backgroundColor ): base ( ) {// ...this.resourceManager = new ResourceManager ( new Resource[] {new Resource ( "bird", ResourceType.Image, @"image\bird" ),new Resource ( "click", ResourceType.Sound, @"sound\click" )} );this.resourceManager.World = this;}在 World 的構造函數中,我們創建了一個 ResourceManager,并指出我們需要一個圖片和一個波形文件。
protected override void OnNavigatedTo ( NavigationEventArgs e ) {// ...this.resourceManager.LoadContent ( );base.OnNavigatedTo ( e ); }當頁面載入之后,我們調用 LoadContent 方法來載入圖片和聲音。之后,我們在 OnUpdate 和 OnDraw 方法中使用他們。
private void OnUpdate ( object sender, GameTimerEventArgs e ) {this.resourceManager.GetSound ( "click" ).Play ( ); }private void OnDraw ( object sender, GameTimerEventArgs e ) {this.GraphicsDevice.Clear ( this.BackgroundColor );this.spiritBatch.Begin ( );this.spiritBatch.Draw ( this.resourceManager.GetTexture ( "bird" ), new Vector2 ( 20, 20 ), Color.White );this.spiritBatch.End ( ); }通過 ResourceManager 的 GetTexture 和 GetSound 方法可以獲取指定的圖片和聲音,只需要傳遞資源的名稱即可。
最后,你還需要在一個適合的位置調用 ResourceManager 的 UnloadContent 方法。
本期視頻 http://v.youku.com/v_show/id_XNTYwOTA2ODgw.html
項目地址 http://wp-xna.googlecode.com/
更多內容 WPXNA
平方開發的游戲 http://zoyobar.lofter.com/
QQ 群 213685539
歡迎平方在其他位置發布的同一文章:http://www.wpgame.info/post/decc4_6329f7
轉載于:https://www.cnblogs.com/zoyobar/archive/2013/05/23/wpxna2.html
總結
以上是生活随笔為你收集整理的通过简单的 ResourceManager 管理 XNA 中的资源,WPXNA(二)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2500元价位的双5G手机 手机价位20
- 下一篇: 鞋店名,有创意的鞋店名字511个