-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAvatar.cs
111 lines (102 loc) · 3.56 KB
/
Avatar.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
namespace Cisco.Spark
{
/// <summary>
/// Represents a <see cref="Person"/>s display picture on Spark.
/// </summary>
public class Avatar
{
/// <summary>
/// URL of the Person's avatar.
/// </summary>
public Uri Uri { get; set; }
/// <summary>
/// The UnityEngine Texture of the person's avatar.
/// </summary>
public Texture Texture { get; set; }
/// <summary>
/// True if the texture has already been downloaded.
/// </summary>
public bool Downloaded = false;
/// <summary>
/// True if already being downloaded.
/// </summary>
bool Locked = false;
/// <summary>
/// Callbacks that are queued to fire.
/// </summary>
List<Action<bool>> WaitingCallbacks;
/// <summary>
/// Builds an Avatar object from an image url.
/// </summary>
/// <param name="uri">Display picture URL.</param>
internal Avatar(Uri uri)
{
Uri = uri;
}
/// <summary>
/// Downloads the Person's Avatar as a Texture from the Url.
/// </summary>
/// <param name="success">True if the download succeeded.</param>
/// <param name="force">Optional: Force the texture to be redownloaded.</param>
public IEnumerator Download(Action<SparkMessage> error, Action<bool> success, bool force = false)
{
if (Downloaded && !force)
{
success(true);
}
else
{
if (Locked)
{
if (WaitingCallbacks == null) WaitingCallbacks = new List<Action<bool>>();
WaitingCallbacks.Add(success);
}
else
{
Locked = true;
#if UNITY_5_4 || UNITY_5_5
var www = UnityWebRequest.GetTexture(Uri.AbsoluteUri);
yield return www.Send();
if (www.isError)
#else
var www = UnityWebRequestTexture.GetTexture(Uri.AbsoluteUri);
yield return www.SendWebRequest();
if (www.isNetworkError)
#endif
{
Debug.LogError("Failed to Download Avatar: " + www.error);
error(new SparkMessage(www));
}
else
{
var tex = ((DownloadHandlerTexture)www.downloadHandler).texture;
if (tex)
{
// Complete.
Texture = tex;
Downloaded = true;
success(true);
Locked = false;
// Notify waiting callbacks, if any.
if (WaitingCallbacks != null)
{
foreach (var callback in WaitingCallbacks) callback(true);
WaitingCallbacks = null;
}
}
else
{
Debug.LogError(www.downloadHandler.text + " (" + www.responseCode + ")");
error(new SparkMessage(www));
}
}
}
}
}
}
}