LoginSignup
6
8

More than 5 years have passed since last update.

UnityでWWWを使って画像・テキスト・動画・fbxファイルを読み込む

Last updated at Posted at 2017-07-06

前提

環境

Unity 5.6.1f1 Personal

比較

[Unity] ドラッグ&ドロップ一切使わずスクリプトのみで画像・動画・テキスト・Prefab取り込み(http://qiita.com/gshirato/items/b823a0a4ea7baff102f2#_reference-c1cb36405534174098b4)

StreamReaderResources.Load()をつかわないためコードがシンプルに
動画を取り込む際にはVideoPlayerのurlからの取り込みを活用できる。

コード

global
string appPath = "http://machin/";

共通Tip

Pathに半角スペースが入っていることを想定。この場合リクエストエラー(400:Bad Request)を返すので、半角スペースを%20にあらかじめ置換。

string path = "http://machin/Example 1/ex.json";
path = path.Replace(" ", "%20"); 
//"http://machin/Example%201/ex.json"が返される。

参考:パーセントコーディング(Wikipedia)(https://ja.wikipedia.org/wiki/%E3%83%91%E3%83%BC%E3%82%BB%E3%83%B3%E3%83%88%E3%82%A8%E3%83%B3%E3%82%B3%E3%83%BC%E3%83%87%E3%82%A3%E3%83%B3%E3%82%B0)

テキストファイル Json/js

JSONファイルをウェブ上で取得することができなかったため(web.configの問題=>解決)内容全く同じなままJavaScript形式で保存。(一応動く)
以下はjsonファイルを読み込むコード。

json
string url = appPath + "json/machin.json";
JSONObject jsonObject;

WWW www = new WWW(url);
yield return www;

if(www.error == null)
{
    if(www.isDone)
    {
        string textfile = www.text;
        jsonObject = new JSONObject(textfile);
    }
}
else
{
    print(www.error);
}


3Dテキスト化

3DText
public textPrefab;//(事前に用意)

GameObject textGo = Instantiate(textPrefab);
TextMesh text3d = textGo.GetCompenent<TextMesh>();

text3d.text = www.text;

画像

photos
string url = appPath + "photos/machin.png";
GameObject image = GameObject.CreatePrimitive(PrimitiveType.Plane);
WWW www = new WWW(url);
yield return www;

Renderer rend = image.GetComponent<Renderer>();
Texture2D tex2d = new Texture2D(1, 1);

tex2d = www.texture;

rend.material.mainTexture = tex2d;

動画

VideoPlayerで超簡単。
過去記事:Unity 5.6からの動画再生機能 VideoPlayerで動画再生(http://qiita.com/gshirato/items/1500f61b408e7af69453)

3Dモデル

更新:2017/7/13
AssetBundlesを使って.fbxモデルをネット上から回収(http://qiita.com/gshirato/items/b58dd8545dc215e41b34)

HoloLens開発記事一覧

http://qiita.com/gshirato/items/6c026f2c67dc332f829b から

6
8
0

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
6
8