using System.Collections; using System.Collections.Generic; using UnityEngine; using System; public class FunctionSync_MaterialTexture : OneValueSyncObject { Renderer renderer; Dictionary matDic = new Dictionary(); private void Start() { Init(); } public void Init() { if (!hasInit) { renderer = GetComponent(); InitDynamic("MaterialTex_" + gameObject.name, CallBack, ValueType.Int); } } /// /// 设置材质 /// /// Resource文件下加载路径 public void SetMaterial(int num) { if(num<0) { return; } else if(num>0) { } myint = num; SendSync(); if (num == 0) { //关闭 renderer.material.mainTexture = null; } else { if (!matDic.ContainsKey(num)) { //StartCoroutine(GetTexture(num, MediaPanel.instance.dicPicture[num].url, a => //{ // if (a) // { // renderer.material.mainTexture = matDic[num]; // } // else // { // Debug.LogError("下载图片失败:" + num); // } //})); } else { renderer.material.mainTexture = matDic[num]; } } } /// /// 下载图片 /// /// IEnumerator GetTexture(int num, string url1, Action callback) { WWW www1 = new WWW(url1); yield return www1; if (www1.isDone) { if (!string.IsNullOrEmpty(www1.error)) { Debug.LogError(www1.error); MessagePanel.ShowMessage("获取图片失败:" + www1.error, GameObject.Find("Canvas").transform, a => { }); callback.Invoke(false); } else { Debug.Log("下载图片成功:" + num); matDic.Add(num, www1.texture); callback.Invoke(true); } } } /// /// 回调 /// /// public void CallBack(string id, bool isEnterRoom) { if (myint == 0) { renderer.material.mainTexture = null; } else if (!matDic.ContainsKey(myint)) { //StartCoroutine(GetTexture(myint, MediaPanel.instance.dicPicture[myint].url, a => //{ // if (a) // { // renderer.material.mainTexture = matDic[myint]; // } // else // { // Debug.LogError("下载图片失败:" + myint); // } //})); } else { renderer.material.mainTexture = matDic[myint]; } } }