Created
April 5, 2019 12:20
-
-
Save ifshuaishuai/8cab4973ea3a3ce0c0d8afbbb3dfa7f2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using UnityEngine; | |
using UnityEngine.UI; | |
using System.Collections; | |
using System.Collections.Generic; | |
public class ScrollIndexCallback1 : MonoBehaviour | |
{ | |
private static readonly int MainTex = Shader.PropertyToID("_MainTex"); | |
public Image image; | |
public Text text; | |
private Material _backupMaterial; | |
private Material _spawnMaterial; | |
private string _headName; | |
private string _imageURL; | |
private string _guid; | |
private static List<ScrollerData> _data = new List<ScrollerData>(10); | |
static ScrollIndexCallback1() | |
{ | |
_data.Add(new ScrollerData() { textName = "1", url = "http://localhost:8000/1.png"}); | |
_data.Add(new ScrollerData() { textName = "2", url = "http://localhost:8000/2.png"}); | |
_data.Add(new ScrollerData() { textName = "3", url = "http://localhost:8000/3.png"}); | |
_data.Add(new ScrollerData() { textName = "4", url = "http://localhost:8000/4.png"}); | |
_data.Add(new ScrollerData() { textName = "5", url = "http://localhost:8000/5.png"}); | |
_data.Add(new ScrollerData() { textName = "6", url = "http://localhost:8000/6.png"}); | |
_data.Add(new ScrollerData() { textName = "7", url = "http://localhost:8000/7.png"}); | |
_data.Add(new ScrollerData() { textName = "8", url = "http://localhost:8000/8.png"}); | |
_data.Add(new ScrollerData() { textName = "9", url = "http://localhost:8000/9.png"}); | |
_data.Add(new ScrollerData() { textName = "10", url = "http://localhost:8000/10.png"}); | |
} | |
void ScrollCellIndex (int idx) | |
{ | |
var data = _data[idx]; | |
SetData(data); | |
} | |
private void Awake() | |
{ | |
_guid = Guid.NewGuid().ToString(); | |
} | |
private void OnDisable() | |
{ | |
DestroyMaterial(); | |
OnPropertyChange(null, null); | |
} | |
private void OnPropertyChange(string newHeadName, string newImageURL) | |
{ | |
SetHeadName(newHeadName); | |
SetMaterialImage(newImageURL); | |
} | |
private void SetHeadName(string newHeadName) | |
{ | |
if (newHeadName != _headName) | |
{ | |
_headName = newHeadName; | |
text.text = _headName; | |
} | |
} | |
private void SetMaterialImage(string newImageURL) | |
{ | |
if (_imageURL == newImageURL) | |
return; | |
if (_imageURL != null) | |
{ | |
ResourceManager.Instance.DestroyWebTexture(_imageURL, _guid); | |
} | |
_imageURL = newImageURL; | |
if (_imageURL != null) | |
{ | |
ResourceManager.Instance.GetWebTexture(_imageURL, _guid) | |
.Then(texture => | |
{ | |
SpawnMaterial(); | |
image.material.SetTexture(MainTex, texture); | |
image.SetMaterialDirty(); | |
}) | |
.Catch(ex => | |
{ | |
/*Debug.LogException(ex);*/ | |
}); | |
} | |
} | |
private void SpawnMaterial() | |
{ | |
if (_spawnMaterial == null) | |
{ | |
var material = image.material; | |
_backupMaterial = material; | |
_spawnMaterial = new Material(material); | |
image.material = _spawnMaterial; | |
image.SetMaterialDirty(); | |
} | |
} | |
private void DestroyMaterial() | |
{ | |
if (_spawnMaterial != null) | |
{ | |
Destroy(_spawnMaterial); | |
_spawnMaterial = null; | |
image.material = _backupMaterial; | |
image.SetMaterialDirty(); | |
} | |
} | |
public void SetData(ScrollerData data) | |
{ | |
OnPropertyChange(data.textName, data.url); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment