- Home /
Answer by M-Hanssen · Apr 12, 2016 at 07:25 PM
A BLOB field in sql contains the binary data of an image. This binary data can be converted to a Texture2D in Unity and the conversion code is already inside the Unity API!
Take a look at this page for more guidelines: http://docs.unity3d.com/ScriptReference/Texture2D.LoadImage.html
Or this page to load raw texture data: http://docs.unity3d.com/ScriptReference/Texture2D.LoadRawTextureData.html
Just make sure you retreive the BLOB data from your database as a byte array!
Also make sure the image is in a supported format. Unity can only load .jpg and .png at runtime.
Thank you Hanssen!
I tried ExampleScript you suggested and it works on a plane but not on a UI Image (with $$anonymous$$esh Renderer on it), as you can see on screenshot.
Where I'm wrong?
Thank you
$$anonymous$$
Solved this step simply changing the sprite:
GetComponent().sprite = Sprite.Create(tex, new Rect(0, 0, 64, 64), new Vector2(0.5f, 0.5f));
What components must add to game object to above? above pic not clean. Thank you
Hi Ra$$anonymous$$,
the important thing are mesh renderer and TestImg.cs, containing the function GetImage() below.
Answer by Giusort · May 04, 2016 at 12:29 PM
EDIT
I solved this way, assuming the query returns just one image:
void GetImage() {
string connectionString = "URI=file:" + string.Format(@"Assets/StreamingAssets/{0}", "SQLiteDB.db");
IDbConnection dbcon = new SqliteConnection(connectionString);
IDbCommand dbcmd;
IDataReader reader;
var tex = new Texture2D(64,64);
dbcon.Open();
string sql = "SELECT image FROM dabname where ID='1'";
dbcmd = dbcon.CreateCommand();
dbcmd.CommandText = sql;
reader = dbcmd.ExecuteReader();
while (reader.Read()) {
byte[] img = (byte[])reader["image"];
tex.LoadImage(img);
// image 266x199
GetComponent<Image>().sprite = Sprite.Create(tex, new Rect(0, 0, 266, 199), new Vector2(0.5f, 0.5f));
}
dbcon.Close ();
reader.Dispose ();
}
Could you help me. Step by Step procedure . I 'm newbie in using unity
How do I display the image using Sprite.Create
Probably because the tex var is null or empty. Start checking the SQLite DB and the blob field, then the DB connection.
Do you have some some error on dbcon.Open() ?
There's no error on it. However the var tex is always null after I fetch an Image in SQLite.
Can you give me some examples oh how to load and save blob files in SQLIte.
the image shows a red question mark, even tho the byte array is not empry and i checked the boolean returned from tex.loadImage() returns true.
here's my code :
private void readQuestionsFromDB(){
string conn = "URI=file:" + Application.dataPath + "/quizdb.s3db"; //Path to database.
IDbConnection dbconn;
dbconn = (IDbConnection) new SqliteConnection(conn);
dbconn.Open(); //Open connection to the database.
IDbCommand dbcmd = dbconn.CreateCommand();
string sqlQuery = "SELECT id, statement, answer, image " + "FRO$$anonymous$$ questions";
dbcmd.CommandText = sqlQuery;
IDataReader reader = dbcmd.ExecuteReader();
while (reader.Read())
{
string statement = reader.GetString(1);
answerint = reader.GetInt32(2);
byte[] img = (byte[])reader["image"];
Question q = new Question(statement, answer, img);
questions.Add(q);
}
reader.Close();
reader = null;
dbcmd.Dispose();
dbcmd = null;
dbconn.Close();
dbconn = null;
}
and then i write this in the start method to display the array as an image :
readQuestionsFromDB();
statement.text = questions[0].statement;
Texture2D tex = new Texture2D(800,400); //image is 800/400
tex.LoadImage(questions[0].image);
image.GetComponent<Image>().sprite = Sprite.Create(tex, new Rect(0,0,tex.width,tex.height),new Vector2(0.5f, 0.5f));
please any help is appreciatied, thank you!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
SQLite and Lists in C# 1 Answer
Best practices for multiple databases 1 Answer
How to get a database with information in it to be usable in a build 0 Answers