Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.ServiceModel;
- using UnityStoreLibrary;
- public class UpdateScene : MonoBehaviour {
- public GameObject paymentTrigger;
- void Start(){
- InvokeRepeating("UpdateMyScene", 1f, Strings.updateInterval);
- paymentTrigger.collider.enabled = true;
- }
- private void UpdateMyScene(){
- UnityStoreClient client = new UnityStoreClient(new BasicHttpBinding(), new EndpointAddress(Strings.webserviceUrl));
- Item[] items = client.GetUnity3dItems(null);
- for(int i=0;i<items.Length;i++){
- GameObject curent = GameObject.Find(items[i].name);
- if (curent != null){
- ItemFieldValue[] values = items[i].item_field_values;
- for(int j=0;j<values.Length;j++){
- GameObject o = GameObject.Find(curent.name + "/" + values[j].field_details.name);
- if (o != null){
- if (values[j].field_details.type_id.Equals(Strings.colorType))
- UpdateColor (values[j], o);
- if (values[j].field_details.type_id.Equals(Strings.textureType))
- if (!o.renderer.material.mainTexture.name.Equals(values[j].value.Substring(0,values[j].value.Length-4)))
- StartCoroutine(UpdateTexture(values[j], o));
- }
- }
- }
- }
- }
- void SaveTextureToFile(Texture2D texture, string fileName)
- {
- byte[] bytes=texture.EncodeToPNG();
- System.IO.FileStream file = new System.IO.FileStream(Strings.defaultUserDirectory+fileName, System.IO.FileMode.CreateNew);
- System.IO.BinaryWriter binary= new System.IO.BinaryWriter(file);
- binary.Write(bytes);
- file.Close();
- }
- void UpdateColor (ItemFieldValue ifv, GameObject o) {
- string[] c = ifv.value.Split(',');
- Color color;
- float r = float.Parse(c[1])/255;
- float g = float.Parse(c[2])/255;
- float b = float.Parse(c[3])/255;
- float a;
- if (o.renderer.material.shader.name.Equals("Transparent/Diffuse"))
- a = float.Parse(c[0])/255/2;
- else
- a = float.Parse(c[0])/255;
- color = new Color(r,g,b,a);
- if (o.renderer.material.color != color)
- o.renderer.material.color = color;
- }
- IEnumerator UpdateTexture(ItemFieldValue ifv, GameObject o) {
- string url = "";
- if (!System.IO.File.Exists(Strings.defaultUserDirectory + ifv.value))
- url = Strings.texturesHost + ifv.value;
- else
- url = "file://" + Strings.defaultUserDirectory + ifv.value;
- WWW www = new WWW (url);
- yield return www;
- if (!System.IO.File.Exists(Strings.defaultUserDirectory + ifv.value))
- SaveTextureToFile(www.texture, ifv.value);
- o.renderer.material.mainTexture = www.texture;
- yield return null;
- }
- }
- /* The yield return 0; statement tells Unity that it has finished executing
- * for this frame and allows it to execute other code. The next frame it will
- * pick up where it left off (in this instance in the middle of the loop).
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement