ページ「MonoGameで外部のXNBファイルを読み込む」と「PCエンジンスーパーグラフィックス」の間の差分

提供: MonoBook
(ページ間の差分)
ナビゲーションに移動 検索に移動
 
(ページの作成:「発売元:NECホームエレクトロニクス 発売:1989年12月8日 価格:39800円 商品番号:PI-TG4」)
 
1行目: 1行目:
[[MonoGame]]で作っている[[ゲーム]]に起動時データ更新を実装し、[[プログラム]]以外の画像や音楽などだけであれば自動更新するようにしたいわけだ。[[スマホ]]の[[ゲーム]]によくあるアレである。むしろ[[プログラム]]の細かい部分も[[NLua]]に投げるようにしてしまいたい。
+
発売元:NECホームエレクトロニクス
 
+
発売:1989年12月8日
そうなると外部から各種[[データ]]を読み込む必要が出てくる。
+
価格:39800円
しかし[[MonoGame]]で扱う各種[[データ]]の基本は[[プログラム]]に組み込まれた「Content」である。
+
商品番号:PI-TG4
 
 
== Xamarin.Android ==
 
[[MonoGame]]の[[ソースコード]]をざっと見た感じ、[[Xamarin.Android]]ではContentManagerクラスでアセット以外の[[ファイル]]へはアクセスできないようだ。ContentManagerクラスの[[ソースコード]]をみると幸いにもvirtualやらがチラホラ見受けられ継承してoverrideする前提の構造になっている。なので細かいことは考えずにoverrideしてしまえと考えた。
 
 
 
ついでに各種パラメータを保存した[[csv]]や[[json]]などのxnb以外の[[ファイル]]も同様の手順で読み込めるように、xnbパーサを経由しない裏口も用意すると捗った。
 
 
 
<source lang="csharp">
 
using System;
 
using System.IO;
 
using Microsoft.Xna.Framework;
 
using Microsoft.Xna.Framework.Content;
 
 
 
public class ExternalContentManager : Microsoft.Xna.Framework.Content.ContentManager
 
{
 
    public ExternalContentManager(IServiceProvider serviceProvider, string rootDirectory)
 
        : base(serviceProvider, rootDirectory)
 
    {
 
    }
 
 
 
    public ExternalContentManager(IServiceProvider serviceProvider)
 
        : base(serviceProvider)
 
    {
 
    }
 
 
 
    protected override Stream OpenStream(string assetName)
 
    {
 
        string assetPath;
 
        if (Path.GetExtension(assetName) != "xnb")
 
        {
 
            assetPath = assetName + ".xnb";
 
        }
 
        else
 
        {
 
            assetPath = assetName;
 
        }
 
        return OpenRead(assetPath);
 
    }
 
 
 
    public Stream OpenRead(string fileName)
 
    {
 
        Stream stream = null;
 
        string path;
 
 
 
        try
 
        {
 
            if (Path.IsPathRooted(fileName))
 
            {
 
                path = fileName;
 
            }
 
            else
 
            {
 
                path = Path.Combine(RootDirectory, fileName);
 
            }
 
 
 
            stream = File.OpenRead(path);               
 
 
 
            // Read the asset into memory in one go. This results in a ~50% reduction
 
            // in load times on Android due to slow Android asset streams.
 
            var memStream = new MemoryStream();
 
            stream.CopyTo(memStream);
 
            memStream.Seek(0, SeekOrigin.Begin);
 
            stream.Close();
 
            stream = memStream;
 
        }
 
        catch (FileNotFoundException fileNotFound)
 
        {
 
            throw new ContentLoadException("The content file was not found.", fileNotFound);
 
        }
 
        catch (DirectoryNotFoundException directoryNotFound)
 
        {
 
            throw new ContentLoadException("The directory was not found.", directoryNotFound);
 
        }
 
        catch (Exception exception)
 
        {
 
            throw new ContentLoadException("Opening stream error.", exception);
 
        }
 
        return stream;
 
    }
 
}
 
</source>
 
 
 
使い方は普通のContentManagerと大して変わらない。
 
<source>
 
// Androidであれば「/data/data/アプリID/files」あたりが返ってくるはず。
 
var path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
 
var cm = new ExternalContentManager(this.Content.ServiceProvider, path);
 
var font = cm.Load<SpriteFont>("IPA-Gothic");
 
</source>
 
 
 
== 関連項目 ==
 
* [[:Category:MonoGame|MonoGameカテゴリ]]
 
 
 
== 参考文献 ==
 
{{reflist}}
 
 
 
{{stub}}
 
[[category:MonoGame]]
 
[[category:Xamarin.Android]]
 

2019年3月9日 (土) 04:31時点における版

発売元:NECホームエレクトロニクス 発売:1989年12月8日 価格:39800円 商品番号:PI-TG4