画像を多く使うアプリを作っています。
画像をたくさん使うので参照方法をResourceフォルダにすると
重たくなるので何か良い方法はないかと探していて
PackageManagerにAddressableというのがレビュー版にあったので
早速使ってみました。
手順)
1、PackageManagerのレビュー版でAddressableをインストール
2、Windows>AssetManagement>AddressabledをクリックしてCreatをクリック
3、Imagesフォルダに入っているファイルを指定してインスペクタ上にAddressというチェックがあるので
それをチェック。
下記のコードで参照できました。
Addressables.LoadAsset<Sprite>("Assets/Images/hogehoge.png").Completed += op => _image.sprite = op.Result;
ちなみに階層はこんな感じです。
UnityのAddressableのマニュアルを見ているとAddressables.LoadAssetは非同期なので読み込み完了時
コールバックをする必要があるとのこと。
早速下記コードを書き足しました。
LoadTest.cs
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.AddressableAssets; using UnityEngine.ResourceManagement; public class LoadTest : MonoBehaviour { [SerializeField]private Image _image = null; // Start is called before the first frame update void Start() { Addressables.LoadAsset<Sprite>("Assets/Images/hogehoge.png").Completed += onLoadDone; } void onLoadDone(IAsyncOperation<Sprite> op) { _image = op.Result; } }
すると下記のエラーがでます。
Assets/Scripts/LoadTest.cs(21,21): error CS0246: The type or namespace name 'IAsyncOperation<>' could not be found (are you missing a using directive or an assembly reference?)
IAsyncOperation<>のnamespaceが見つからないとのこと。
しかしマニュアルを見るとIAsyncOperation<>のnamespaceはUnityEngine.ResourceManagementで良いはずなのですが
どうしてでしょうか?最初スペルが違うかと思ったのですが合っています。よろしくお願いします。
環境)
PC: mac
Unity2018.3.14f1
言語:C#
回答2件
あなたの回答
tips
プレビュー