/
NguiTextureBindingFromUrl.cs
105 lines (85 loc) · 2.39 KB
/
NguiTextureBindingFromUrl.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
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
[System.Serializable]
[AddComponentMenu("NGUI/NData/Texture Binding From Url")]
public class NguiTextureBindingFromUrl : NguiBinding
{
// Base on TextBinding
private readonly Dictionary<Type, EZData.Property> _properties = new Dictionary<Type, EZData.Property>();
private UITexture _uiTexture;
public string textureUrl = null;
public int hack_x = 0;
public int hack_y = 0;
protected override void Unbind()
{
base.Unbind();
foreach(var p in _properties)
{
p.Value.OnChange -= OnChange;
}
_properties.Clear();
}
protected override void Bind()
{
base.Bind();
FillTextProperties(_properties, Path);
foreach(var p in _properties)
{
p.Value.OnChange += OnChange;
}
}
protected override void OnChange()
{
base.OnChange();
textureUrl = (string)GetTextValue(_properties);
}
public override void Start()
{
base.Start();
StartLoadingTexture();
}
private void StartLoadingTexture ()
{
if (!string.IsNullOrEmpty(textureUrl))
{
_uiTexture = gameObject.GetComponent<UITexture>();
if (!_uiTexture) {
_uiTexture = gameObject.AddComponent<UITexture>();
//Material mat = new Material(Shader.Find("Unlit/Transparent Colored (AlphaClip)"));
Material mat = new Material(Shader.Find("Unlit/Transparent Colored"));
_uiTexture.material = mat;
}
_uiTexture.MakePixelPerfect();
this.LoadTexture(textureUrl, (texture) => {
_uiTexture.mainTexture = texture;
_uiTexture.MakePixelPerfect();
if ((hack_x>0) && (hack_y>0))
{
gameObject.transform.localScale = new Vector3 (hack_x, hack_y, gameObject.transform.localScale.z);
}
/* Scale images if not retina */
/*
if (!AppGUISettings.IS_RETINA)
gameObject.transform.localScale = new Vector3 (gameObject.transform.localScale.x/2, gameObject.transform.localScale.y/2, gameObject.transform.localScale.z);
*/
});
}
}
private IEnumerator LoadTexture (WWW www, Action<Texture2D> callback)
{
yield return www;
if (string.IsNullOrEmpty(www.error)) {
Texture2D tex = new Texture2D(4, 4, TextureFormat.RGB24, false);
www.LoadImageIntoTexture(tex);
callback(tex);
} else {
callback(null);
}
}
private void LoadTexture (string url, Action<Texture2D> callback)
{
StartCoroutine (LoadTexture (new WWW (url), callback));
}
}