Unity question - material.mainTexture = www.texture

A place for tutorials on how to get the most out of Flash

Unity question - material.mainTexture = www.texture

Postby OwnerOfSuccuby » Wed Dec 21, 2016 12:31 pm

Hello. Get some problems with Unity.

I load captcha image from localhost and it loads - i can see it in Sprites renderer in materials.

But it do not effect sprite image at all.

How to load image to sprite ? If i get a texture - try different ways - nothing works. What did i make wrong ? :mrgreen:

Code: Select All Code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class KaptchaGetScript : MonoBehaviour {

   // Use this for initialization
   private string url = "http://localhost/Kaptcha/captcha.php";
   //private string url = "http://localhost/Kaptcha/bg_capcha.png";
   private Renderer KAPTCHA;
   private Sprite sprt;
   private WWW www;
   private bool READY = false;

   IEnumerator Start () {
   //void Start() {
      //KAPTCHA = GameObject.Find ("KAPTCHA").GetComponent("Renderer") as Renderer;
      www = new WWW(url);
      yield return www;
      //KAPTCHA = new Renderer ();
      Debug.Log(www.texture);
      //Renderer renderer = GetComponent<Renderer>();
      //KAPTCHA.material.mainTexture = www.texture;
      //renderer.material.mainTexture = www.texture;
      KAPTCHA = GameObject.Find ("KAPTCHA").GetComponent<Renderer>();
      KAPTCHA.material.mainTexture = www.texture;
      //KAPTCHA.sprite = Sprite.Create((Texture2D)tex, new Rect(0,0, tex.width,tex.height), Vector2.zero);
      //sprt = GameObject.Find ("KAPTCHA").GetComponent<SpriteRenderer>().sprite;
      //GameObject.Find("111").GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>("Sprites/222");
   //   sprt = Sprite.Create(www.texture, new Rect(0.0f,0.0f,www.texture.width,www.texture.height), new Vector2(0.5f,0.5f), 100.0f);
      //sprt = Sprite.Create(www.texture, new Rect(0.0f,0.0f,www.texture.width,www.texture.height), new Vector2(0.5f,0.5f), 100.0f);
      //KAPTCHA.Applay ();
      www.Dispose();

//      www.LoadImageIntoTexture(KAPTCHA.material.mainTexture as Texture2D);
   }
   
   // Update is called once per frame
   void Update () {
   /*
      if (READY == false) {
         if (www.isDone) {
            KAPTCHA.material.mainTexture = www.texture;
            READY = true;
         }
      }
      */
   }

}
User avatar
OwnerOfSuccuby
 
Joined: Fri Jun 11, 2010 9:33 pm

Re: Unity question - material.mainTexture = www.texture

Postby OwnerOfSuccuby » Wed Dec 21, 2016 1:28 pm

Find a answer thank you ;) :mrgreen: Sorry for topic but may be will be interesting to some body :mrgreen:

Code: Select All Code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class KaptchaGetScript : MonoBehaviour {

   // Use this for initialization
   private string url = "http://localhost/Kaptcha/captcha.php";
   //private string url = "http://localhost/Kaptcha/bg_capcha.png";
   //private Renderer KAPTCHA;
   //private Sprite sprt;
   private WWW www;
   private bool READY = false;
   private UnityEngine.UI.Image go;

   IEnumerator Start () {
   //void Start() {
      //KAPTCHA = GameObject.Find ("KAPTCHA").GetComponent("Renderer") as Renderer;
      www = new WWW(url);
      yield return www;

      go = GameObject.Find ("KAPTCHA").GetComponent<UnityEngine.UI.Image>();

      go.sprite = Sprite.Create(http://www.texture, new Rect(0,0, http://www.texture.width,www.texture.height), Vector2.zero);
      http://www.Dispose();
   }
   
   // Update is called once per frame
   void Update () {
   /*
      if (READY == false) {
         if (http://www.isDone) {
            KAPTCHA.material.mainTexture = http://www.texture;
            READY = true;
         }
      }
      */
   }

}
User avatar
OwnerOfSuccuby
 
Joined: Fri Jun 11, 2010 9:33 pm


Return to Tutorials



Who is online

Users browsing this forum: No registered users