-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathTextureSetComponent.NetWork.Await.cs
61 lines (54 loc) · 2.07 KB
/
TextureSetComponent.NetWork.Await.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
using System;
using ET;
using UGFExtensions.Await;
using UnityEngine;
namespace UGFExtensions.Texture
{
public partial class TextureSetComponent
{
/// <summary>
/// 通过网络设置图片
/// </summary>
/// <param name="setTexture2dObject">需要设置图片的对象</param>
/// <param name="saveFilePath">保存网络图片到本地的路径</param>
public async void SetTextureByNetworkAsync(ISetTexture2dObject setTexture2dObject,string saveFilePath = null, ETCancellationToken etCancellationToken = null)
{
int serialId = -1;
Texture2D texture = null;
if (m_TexturePool.CanSpawn(setTexture2dObject.Texture2dFilePath))
{
texture = (Texture2D)m_TexturePool.Spawn(setTexture2dObject.Texture2dFilePath).Target;
}
else
{
serialId = m_SerialId++;
void Cancel()
{
CancelSetTexture(serialId);
}
try
{
etCancellationToken?.Add(Cancel);
var data = await m_WebRequestComponent.AddWebRequestAsync(setTexture2dObject.Texture2dFilePath);
if (!data.IsError)
{
texture = new Texture2D(0, 0, TextureFormat.RGBA32, false);
texture.LoadImage(data.Bytes);
if (!string.IsNullOrEmpty(saveFilePath))
{
SaveTexture(saveFilePath, data.Bytes);
}
m_TexturePool.Register(
TextureItemObject.Create(setTexture2dObject.Texture2dFilePath, texture,
TextureLoad.FromNet), true);
}
}
finally
{
etCancellationToken?.Remove(Cancel);
}
}
SetTexture(setTexture2dObject, texture,serialId);
}
}
}