-1
\$\begingroup\$

We could say that we are beginners, We can consult and show our data (text) from Mysql but the images don´t show in Unity. Need some help! We hace tried almost everything! We can someone help us.

in C# we have...

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Net.NetworkInformation;
using UnityEditor.Sprites;
public class Detalles : MonoBehaviour {

string url = "https://cdn.designrulz.com/wp-content/uploads/2012/03/unique-house-garden-at-Luxury-and-Stylish-Glass-House-desgnrulz-3.jpg";
Texture2D img;


public string[] items;

public int num ;
public Image imagen;
public Text precio;
public Text tipo;
public Text superficie;
public Text recamaras;
public Text baños;
public Text cocheras;
public Text ciudad;
public Text ubicacion;

//
IEnumerator Start(){
    WWW itemsData = new WWW ("http://localhost/Casas/ItemsData2.php");
    yield return itemsData;
    string itemsDataString = itemsData.text;
    //print (itemsDataString);
    items = itemsDataString.Split (';');
    //num = items.Length - 1;
    //for( int i = 0; i < num; i++ ){
    //  }
    precio.text = " $ "+ GetDataValue (items[num],"Precio");
    tipo.text = " " + GetDataValue (items [num], "Tipo");
    superficie.text = " "+ GetDataValue (items[num],"Superficie")+" m2";
    recamaras.text = " " + GetDataValue (items [num], "Recamaras")+ " Recamaras";
    baños.text = " " + GetDataValue (items [num], "Sanitarios")+" Baños";
    cocheras.text = " " + GetDataValue (items [num], "Cocheras")+" Cocheras";
    ciudad.text = " " + GetDataValue (items [num], "Ciudad");
    ubicacion.text = " " + GetDataValue (items [num], "Ubicacion");
    StartCoroutine (LoadImg ());
}

IEnumerator LoadImg(){
    yield return 0;
    WWW imgLink = new WWW (url);
    yield return imgLink;
    img = imgLink.texture;
}

void Update(){
    imagen.sprite =  Sprite.Create (img, new Rect(0,0,690,345),new Vector2(0.5f, 0.5f));
}

string GetDataValue(string data, string index){
    string value = data.Substring (data.IndexOf (index) + index.Length);
    value = value.Remove (value.IndexOf ("|"));
    return value;

In PHP we have...

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbName = "casas";

$conn = new mysqli($servername, $username, $password, $dbName);


if(!$conn){
    die("Conection failed".mysqli_connect_error());
}

$sql = "SELECT id,titulo,foto,tipo,precio,superficie,sanitarios,recamaras,cocheras,colonia,ciudad,ubicacion FROM casas ORDER BY id ";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result)>0) {
    while ($row = mysqli_fetch_assoc($result)) {
        echo "Id".$row['id'] ."|Titulo".$row['titulo']."|Tipo".$row['tipo']."|Precio".$row['precio'] ."|Superficie".$row['superficie']."|Sanitarios".$row['sanitarios'] ."|Recamaras".$row['recamaras']."|Cocheras".$row['cocheras'] ."|Colonia".$row['colonia']."|Ciudad".$row['ciudad'] ."|Ubicacion".$row['ubicacion']."|".";";
    }
}
?>
\$\endgroup\$
0
\$\begingroup\$

To start with, you should move the line

imagen.sprite =  Sprite.Create (img, new Rect(0,0,690,345),new Vector2(0.5f, 0.5f));

from Update() to the end of LoadImg() as you don't want to be creating a new sprite every time the game updates.

Your code looks mostly ok otherwise. Here's a working code sample which loads an image from a URL. If it still doesn't work, comparing the two and modifying yours to match should fix the issue. https://answers.unity.com/questions/1175862/loading-a-sprite-from-url-c.html

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.