Advertisement
rhose87

Untitled

Jun 20th, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.79 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.ServiceModel;
  4. using UnityStoreLibrary;
  5.  
  6. public class UpdateScene : MonoBehaviour {
  7.    
  8.     public GameObject paymentTrigger;
  9.    
  10.     void Start(){
  11.         InvokeRepeating("UpdateMyScene", 1f, Strings.updateInterval);
  12.         paymentTrigger.collider.enabled = true;
  13.     }
  14.    
  15.     private void UpdateMyScene(){
  16.         UnityStoreClient client = new UnityStoreClient(new BasicHttpBinding(), new EndpointAddress(Strings.webserviceUrl));
  17.         Item[] items = client.GetUnity3dItems(null);
  18.         for(int i=0;i<items.Length;i++){
  19.             GameObject curent = GameObject.Find(items[i].name);
  20.             if (curent != null){
  21.                 ItemFieldValue[] values = items[i].item_field_values;              
  22.                 for(int j=0;j<values.Length;j++){
  23.                     GameObject o = GameObject.Find(curent.name + "/" + values[j].field_details.name);
  24.                     if (o != null){                    
  25.                         if (values[j].field_details.type_id.Equals(Strings.colorType))
  26.                             UpdateColor (values[j], o);                    
  27.                         if (values[j].field_details.type_id.Equals(Strings.textureType))
  28.                             if (!o.renderer.material.mainTexture.name.Equals(values[j].value.Substring(0,values[j].value.Length-4)))
  29.                                 StartCoroutine(UpdateTexture(values[j], o));
  30.                     }
  31.                 }
  32.             }
  33.         }
  34.     }
  35.    
  36.     void SaveTextureToFile(Texture2D texture, string fileName)
  37.      {
  38.         byte[] bytes=texture.EncodeToPNG();
  39.         System.IO.FileStream file = new System.IO.FileStream(Strings.defaultUserDirectory+fileName, System.IO.FileMode.CreateNew);
  40.         System.IO.BinaryWriter binary= new System.IO.BinaryWriter(file);
  41.         binary.Write(bytes);
  42.         file.Close();
  43.      }
  44.  
  45.     void UpdateColor (ItemFieldValue ifv, GameObject o) {
  46.         string[] c = ifv.value.Split(',');
  47.         Color color;
  48.         float r = float.Parse(c[1])/255;
  49.         float g = float.Parse(c[2])/255;
  50.         float b = float.Parse(c[3])/255;
  51.         float a;
  52.         if (o.renderer.material.shader.name.Equals("Transparent/Diffuse"))
  53.             a = float.Parse(c[0])/255/2;
  54.         else
  55.             a = float.Parse(c[0])/255;
  56.         color = new Color(r,g,b,a);
  57.         if (o.renderer.material.color != color)
  58.             o.renderer.material.color = color;
  59.     }
  60.  
  61.     IEnumerator UpdateTexture(ItemFieldValue ifv, GameObject o) {
  62.         string url = "";
  63.         if (!System.IO.File.Exists(Strings.defaultUserDirectory + ifv.value))
  64.             url = Strings.texturesHost + ifv.value;
  65.         else
  66.             url = "file://" + Strings.defaultUserDirectory + ifv.value;
  67.         WWW www = new WWW (url);
  68.         yield return www;
  69.         if (!System.IO.File.Exists(Strings.defaultUserDirectory + ifv.value))
  70.             SaveTextureToFile(www.texture, ifv.value);
  71.         o.renderer.material.mainTexture = www.texture;
  72.         yield return null;     
  73.     }
  74. }
  75.  
  76. /* The yield return 0; statement tells Unity that it has finished executing
  77.  * for this frame and allows it to execute other code. The next frame it will
  78.  * pick up where it left off (in this instance in the middle of the loop).
  79.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement