LoginSignup
11
10

More than 5 years have passed since last update.

[Unity] テキストファイルをスクリプトでロードして中身を表示する方法

Last updated at Posted at 2014-12-18

テキストファイルをスクリプトでロードして中身を表示する方法です.
Resources.Load(path) というメソッドを使います.

1. Resources フォルダを作ってテキストファイルを作る

Unity では "Resources" というフォルダを作ると,
その中のアセットをプログラムからアクセスすることができます.

下記の画像のように "Resources" というフォルダを作って
sample_text.txt というファイルを作りましょう.

1.png

ちなみに中身は "Hello, world!" と書いてあるだけの
シンプルなファイルです.

2.png

2. スクリプトでロードして中身を表示する

適当な Script を作って Awake あたりで下記のコードを書いてください.

var textAsset = Resources.Load ("sample_text") as TextAsset;
Debug.Log (textAsset.text);

Resources.Load(path) の返り値は Object型なので
TextAsset にキャストしています.

3. 実行する

Unity で実行してみてください.
Console パネルに結果が表示されるのがわかるかと思います.

4.png

以上, 備忘録でした.

11
10
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
10