在Unity中显示Parse.com存储的JPG

时间:2015-11-16 20:44:16

标签: c# parse-platform unity3d

我正在研发一款将移动相机快照编码为.JPG的应用程序,并将这些图像存储在Parse中。

应用程序的另一个页面需要显示存储对象的列表,并显示每个单元格的相应图像(作为缩略图)。

我的问题是,即使Unity没有抛出任何错误,图像文件也不会在网格渲染器的平面上显示为缩略图。

以下代码:

using UnityEngine;
using UnityEngine.UI;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using Parse;

public class RetrieveAllObjects : MonoBehaviour
{
    public RectTransform InfoCell;
    public Text IdText;
    public Text DateText;
    public float Vec3x;
    public float Vec3y;
    public List<string>AllObjectIds = new List<string> ();
    public List<string>CreatedAtDates = new List<string> ();
    public GameObject AnchorPos;
    int num;
    string objectId;
    DateTime? createdAt;
    IEnumerable<ParseObject> results;

    // Use this for initialization
    void Start ()
    {

        var query = ParseObject.GetQuery ("Snapshot")
            .OrderByDescending("createdAt");

        query.FindAsync ().ContinueWith (u =>
        {
            results = u.Result;
            foreach (var result in results) {
                objectId = result.ObjectId;
                createdAt = result.CreatedAt;

                // print ("Object ID: " + objectId + "\n" + "Date Created: " + createdAt);

                AllObjectIds.Add (objectId);
                CreatedAtDates.Add (createdAt.ToString());
            }

            foreach (string id in AllObjectIds) {
                // CreateInfoCell ();
                // print (id);
            }
            foreach (string date in CreatedAtDates) {
                // CreateInfoCell ();
                // print (date);
            }
            return AllObjectIds;
            return CreatedAtDates;
        });

        StartCoroutine (UpdateUI ());
    }

    IEnumerator UpdateUI () {
        yield return new WaitForSeconds(1);
        var query = ParseObject.GetQuery ("Snapshot");
        Task task = query.FindAsync ();
        while(!task.IsCompleted) yield return null;
        // Do all your continue with stuff here
        foreach (string id in AllObjectIds) {
            CreateInfoCell (id);
        }
        AnchorPos.SetActive (false);
    }

    void CreateInfoCell (string id)
    {
            // print ("CELL CREATED\n");
            RectTransform newCell = Instantiate (InfoCell);
            newCell.name = id + " - " + CreatedAtDates[num];
            GameObject PutIdText = GameObject.Find(newCell.name + "/ObjectIdText");
            PutIdText.GetComponent<Text> ().text = id;
            GameObject PutDateText = GameObject.Find(newCell.name + "/DateCreatedText");
            PutDateText.GetComponent<Text> ().text = CreatedAtDates[num];

        StartCoroutine (DisplayThumbnail(newCell, id));

            // IdText.text = id;
            // DateText.text = CreatedAtDates[num];
            num++;
            newCell.transform.SetParent (this.gameObject.transform);
            newCell.transform.localScale = new Vector3 (1, 1, 1);
        newCell.transform.position = new Vector3 (AnchorPos.transform.position.x, AnchorPos.transform.position.y - Vec3y, AnchorPos.transform.position.z);
            Vec3y = Vec3y + 100;
    }

    IEnumerator DisplayThumbnail (RectTransform newCell, string id)
    {


        ParseQuery<ParseObject> query = ParseObject.GetQuery ("Snapshot").WhereEqualTo("ObjectId", id);

        var queryTask = query.GetAsync (id);
        while (!queryTask.IsCompleted)
            yield return null;

        ParseObject obj = queryTask.Result;
        yield return new WaitForSeconds(1);
        var imageFile = obj.Get<ParseFile> ("Snapshot");
        var imageRequest = new WWW (imageFile.Url.ToString ());
        yield return imageRequest;

        GameObject PutImg = GameObject.Find(newCell.name + "/Img");

        print (newCell.name + "/Img\n" + PutImg.name + " - " + imageRequest);
        PutImg.GetComponent<Renderer> ().material.mainTexture = imageRequest.texture;
    }

    // Update is called once per frame
    void Update ()
    {

    }
}

0 个答案:

没有答案