How to read data from CKAsset.

Ask a question here. It may wind up in the FAQ on the plugin homepage.
elritualk
Posts: 2
Joined: Fri Jun 25, 2021 10:13 pm

How to read data from CKAsset.

Post by elritualk »

Hi,

I want to save, load, and read a CKAsset to iCloud.

- Save. Ok.
First, I save the CKAsset to iCloud.

var record = new CKRecord(recordType, new CKRecordID(recordID));
record.SetAsset(new CKAsset(NSURL.FileURLWithPath(Application.persistentDataPath + "/savedgame.save")), "savedfile");
And then I do a database.SaveRecord. I got not error, so upload is ok.

- Load. Ok.
Now, I load the CKAsset from iCloud.

I do database.FetchRecordWithID. I got the record, and then I do:
CKAsset asset = record.AssetForKey("savedfile");

- Read. How?
But, now, how to access to the content of that file?

Debug.Log(asset.FileURL); gives me an output: NSURL 0x10799049024
Debug.Log(asset.ToString()); gives me an output: CKAsset 0x4877096912

Thanks.
Esteban.

elritualk
Posts: 2
Joined: Fri Jun 25, 2021 10:13 pm

Re: How to read data from CKAsset.

Post by elritualk »

Nevermind. Got it working.

I just use the asset.FileURL.AbsoluteString and then load that url with UnityWebRequest:

CKAsset asset = record.AssetForKey("savedfile");
StartCoroutine(GetFile(asset.FileURL.AbsoluteString));

private IEnumerator GetFile(string path)
{
UnityWebRequest req = UnityWebRequest.Get(path);

yield return req.Send();

if(req.isNetworkError)
{
Debug.Log("Error.");
}
else
{
Debug.Log(req.downloadHandler.text);
}
}

Thanks.

Post Reply