I am pretty new in programming with C#. At the moment I am trying to make a little Augmented Reality App including object detection by using Unity. Right now have two regonized stations. With my next step I’d like to display for each station the two sensor datas which are pushed into a sql database. Thats only because I thought its the easiest way. No I managed to read some datas from that database by using WWW.
Is there a way how I could load the all the time (like each frame in the update function)?
Because if I do it like the following way, it just downloads it at the beginning and that takes quite a while. So it doesnt seem to be possible to do that multiple times a second. Another way would only display the new values if one value changes. But I really dont know how to do that. I am not asking for a complete code, just some tipps how I could manage that.
Thank you very much!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DataLoader : MonoBehaviour {
public string[] Maschinen;
// Use this for initialization
IEnumerator Start () {
WWW itemData = new WWW ("http://localhost/UnityARDB/itemData.php");
yield return itemData;
string itemDataString = itemData.text;
print (itemDataString);
Maschinen = itemDataString.Split(';');
print (GetDataValue (Maschinen [0], "Wert:"));
}
string GetDataValue(string data, string index) {
string value = data.Substring (data.IndexOf (index) + index.Length);
if(value.Contains("|"))value = value.Remove (value.IndexOf ("|"));
return value;
}
// Update is called once per frame
void Update () {
}
}
I think that while using a SQL web call should be able to run multiple times a second, that is a terrible terrible idea just because it’ll put a huge strain on your server per user. What is actually going to change about these things on that timescale? I’m not really clear on what data you’re actually retrieving from the server.
If you do want to do requests like this, you can write a loop that basically sends out a request every time the previous request comes back:
IEnumerator Start() {
while (true) {
WWW itemData = new WWW ("http://localhost/UnityARDB/itemData.php");
yield return itemData;
//process your result
}
}
Or you can have it kick off a request at a set interval:
IEnumerator Start() {
while (true) {
StartCoroutine(RetrieveData());
yield return new WaitForSeconds(0.25f);
}
}[
IEnumerator RetrieveData() {
WWW itemData = new WWW ("http://localhost/UnityARDB/itemData.php");
yield return itemData;
//process your result
//keep in mind results might return in unexpected order
}
All that said, I still think that anything that involves multiple calls to a SQL database per second per user is probably a bad idea.
Thanks for your answer.
Because you ask what’s going to change:
I have two machines with each a temperature sensor. Also the machine is selecting different programmes which shall be displayed (just the programme name) within my app. So after the temperature can change all the time and the programme changes depending on the item. So therefore what’s going to change: the time stamp, maybe a counter, the temperature and the used programme.
I understand that multiple calls a second don’t make much sense. Do you have an idea how i could do that differently? Because I’m building the ar app through unity, i later have to make sure it’s running on a iOS device. Is there a way that the server/database pushes some datas when changed onto a device? That seems to make more sense to me.
Add IObservable and IObserver like IEnumerator and Enumerator except it is for ‘push’ events instead of ‘pull’
here is some code that uses observable with google realtime database
but I think this might only work (with luck) on the latest mono beta of unity? idk?
Now I have a general concern… As I am using Vuforia for Obeject Regonition in Unity to then at the end build an iOS App.
Dou you or anyone else think that my case can work that way? Because i only want to display the information from the database upon something got detected I couldn’t figure out if its possible to work at the end on an iOS device.
For now I made it work in unity to get that necessary datas and display them when something is detected, but on the device it doesn’t work. I think the way the database connection is handled in my unity script just doesn’t work on iOS. It just doesnt show any data from the database. The server “localhost” obviously can’t be accessed from the device, but should it normally work like that?
I know its a crossplatform problem, but maybe some has an idea?