So in school I've been given assignment to create a music streamer app for androids. I decided to use Unity because it's very easy to create GUI and because C# hasn't been a problem for me so far. After creating basic GUI for Login / Register I am now completely lost. I've got no clue as to how to actually connect database with appropriate table to unity. (I could use either school's server or the control panel to a server provider which I am paying for, because it has a lot of DB options such as: phpMyAdmin, MySQL, Remote MySQL?, PostgreSQL, or phpPgAdmin)
#Connecting Unity to database (and more)
1 messages · Page 1 of 1 (latest)
Here's a picture of all databases I can use
Just to be clear: I just need tips on how to make registration and log in or where can I find tutorials for it
Hey Jandes! Are you going to use the database to store the music for streaming? Or is that for something else?
And is using a database a requirement for your project, or would you be open to other options? I can think of some simpler ways to stream music than a database.
It is not a requirement, the professor just told me it has to be a music streaming app and I wanted to use Database for music streaming as well, but mainly I need it for log in and register (I think). If you have any other suggestion for any or both of them I would be glad to hear it.
@stone tundra One option you could consider is Unity Cloud Save:
https://docs.unity.com/ugs/en-us/manual/cloud-save/manual/tutorials/unity-sdk#End_to_end_initialization_and_authentication_flow
The Unity Authenticaiton SDK can be used to sign people in and you can use Cloud Save (and Cloud Save Files) to store data.
If you want it to be searchable, depending on your needs, you can also define what fields you want to filtering on and run queries from Cloud Code: https://docs.unity.com/cloud-code
@sacred hearth 's solution will let you store metadata about the streaming, like a list of available songs and where to download them
Once you have a specific track you want to download, use https://docs.unity3d.com/2017.3/Documentation/ScriptReference/WWW.GetAudioClipCompressed.html
This will get you an AudioClip that you can assign to an https://docs.unity3d.com/ScriptReference/AudioSource.html
I don't want to put you off using a database if that's something you want to do! (Although Cloud Save might be simpler/easier!)
Using a database with Cloud Code is also a great option. The advantage with using Cloud Code to talk to the database is that it will automatically handle authentication for you and allow you to securely read/write data only for that player.
(I was way overthinking the problem and trying to figure out how to do live audio transcoding in Unity, before I remembered GetAudioClipCompressed 😅 )
The acual music will have to be stored somewhere on open server, available for download without signin. You'll need a direct link to the file to give to GetAudioClipCompressed() not a page with a download button (so Google Drive wont work)
Do you know how to host files like that?
Can't say I do. I mostly programmed in CLion compiler or made a few small offline games in Unity, and I learned postgreSQL a bit . Haven't done much of anything else yet.
You've got a couple options for the file hosting:
-
A CDN (Content Distribution Network) like Amazon AWS or Digital Ocean Spaces (my preference) https://docs.digitalocean.com/products/spaces/how-to/add-and-remove-files/ - This is basically just a place where you upload files and get direct links to them. I think most will have a free tier for small projects
-
Make your own server by installing nginx and hosting static files: https://docs.nginx.com/nginx/admin-guide/web-server/serving-static-content/
DigitalOcean
Upload files from your local computer to make them available in a Spaces bucket, download files from a bucket to your local computer, and delete files from a bucket to stop serving them.
I have a server and a website of my own, that I pay for once a year, isn't it possible to use that?
Oh yeah that should work! As long as you can get a direct link to the file
Yeah I am pretty sure I can do that. Thanks, I am going to read the documentation you and Iain sent me earlier. Can we keep this thread open just in case I will require any additional help in the future?
So for cloud save I could use save SaveAsync method for saving one registration, log in would be LoadAsync and then liked songs would be in SaveAsync again? Something like that?
Yeah the thread will stay open! I won't have as much time to respond after the event but I'll try
Yes, although Cloud Save is only necessary if you want to have users with accounts that sync between devices
If you just want simple local saves, you can use PlayerPrefs https://docs.unity3d.com/ScriptReference/PlayerPrefs.html
Well yeah another assignment is making a port from android to PC, I just thought it would be cool if the songs a person has liked saved to their accounts
But using only PlayerPrefs does seem easier
You can use PlayFab, its very easy to store simple user data and authentication (your basic user name/email/password). PlayFab is super simple for creating/authenticating users - you SHOULD use that and not a database if the database isn't a requirement.. However you could still use a DB for searching/listing out the available streams.. streaming is a whole other problem if "streaming" is actually what is a requirement on your project.. because using the www.audiosource to download an audio file is not streaming...
If it's as simple as you having a fixed list of streams/audio files to play, you can create that list in PlayFab Title Data (which any user has read access to)
But certainly you could also use a simple SQLite local file, if you wanted an actual litte static database you can read from that has the list of streams to listen to.
However, if you just want some static data/list of things you could also just use Scriptable Objects.. and you add a public List<YourScriptable> Songs; onto a class/component that is like a StreamManager or MusicManager named class and just loop through that list to list out the streams using your own UI
so, those are some of the options.
For an example of how to play an audio stream in Unity, here is a video although its 3 years old but should still work:
https://www.youtube.com/watch?v=2T2fiUUETAw
I’m wondering how can I play an mp3 audio file from URL in Unity. So I tried to search around and I found the solution.
Please find the source code from my blog link here: https://thirteenov.ciihuy.com/how-to-play-an-mp3-audio-file-from-url-in-unity/
the documentation on it is here:
again, I have to recommend using PlayFab for your login/password/email account creation with the ability to store user data.. it's incredibly fast and easy you can have it working in a few minutes. It even has a password recovery system.. all you do is create the typical user interface (which it sounds like you already did) and call the PlayFab API, which it's literally just a few lines of code for each thing.. logging in, creating the account, etc
using PlayFab will also give you cross platform, when you build the PC version it will just work, nothing to change. I believe the example they give for the login/create account API call has #IF defs for android, apple, and PC right there
If not, I can send you my few lines of example code and you will see just how easy playfab is to use
if (AppSettings.IsRegistered)
{
print($"Using email and password to autlogin: {AppSettings.EmailAddress}");
var requestEmail = new LoginWithEmailAddressRequest { Email = AppSettings.EmailAddress, Password = AppSettings.Password };
PlayFabClientAPI.LoginWithEmailAddress(requestEmail, OnAutoLoginSuccess, OnAutoLoginFailure);
return;
}
else
{
#if UNITY_STANDALONE
var requestCustomId = new LoginWithCustomIDRequest { CustomId = SystemInfo.deviceUniqueIdentifier, CreateAccount = true };
PlayFabClientAPI.LoginWithCustomID(requestCustomId, OnAutoLoginSuccess, OnAutoLoginFailure);
return;
#endif
#if UNITY_ANDROID
var requestAndroid = new LoginWithAndroidDeviceIDRequest { AndroidDeviceId = SystemInfo.deviceUniqueIdentifier, CreateAccount = true };
PlayFabClientAPI.LoginWithAndroidDeviceID(requestAndroid, OnAutoLoginSuccess, OnAutoLoginFailure);
return;
#endif
#if UNITY_IOS
var requestIOS = new LoginWithIOSDeviceIDRequest { DeviceId = SystemInfo.deviceUniqueIdentifier, CreateAccount = true };
PlayFabClientAPI.LoginWithIOSDeviceID(requestIOS, OnAutoLoginSuccess, OnAutoLoginFailure);
return;
#endif
}
AppSettings is my own class that makes it easy to store the username/pwd in playerprefs.. and IsRegistered just means they already logged in/already created an account previously. The ELSE is when they haven't created an account and always runs, so you always have a behind the scenes playfab login even the first time before you prompt them to create an account.
there is another line of code/API call where you add an email and password to an existing account returned via those silent/background logins in the ELSE portions shown above
so in effect, the first time someone runs the app they are logged in even without providing a username or password.. via ther unique device ID. and then later you add an email/password.
it's super simple, definitely recommended way to go for user auth
if you need something where users can change data that other users can see/access that does create a whole other problem were you would need an external database or something (and database authentication that is secure/behind web services) which goes into building web services etc that you should avoid if it's not a requirement for your project
but to be honest building web services is very easy.. you could use Azure Functions or something like that to avoid having to host web services or a database anywhere. Azure Functions can access a database securely without any user auth.. there are ways to call azure functions via PlayFab Cloud Functions or whatever that is called.. and calling the PlayFab Functions (which are already authenticated to the playfab user) will forward the playfab user data to your Azure function - there is a mechanism for that.. but that is more involved and like I said if you don't need it you should avoid all that.. sounds like you can get away with just static data and that you can just hard code or make Scriptable Objects
I like the Cloud Code suggestion, that is a good one but also looks pretty involved.. I haven't used it yet and it's probably not terribly difficult but it won't be anywhere near as easy as PlayFab
I feel like Scriptable Objects a list of Streams is probably a better learning way to go, since learning scriptables and their usefulness is a good thing.. but you could also use PlayFab Title Data, its basically just a JSON string that gets stored (you use the PlayFab admin web site to put the JSON string there) and its whatever json data you want to put there, and then the client make a one line call to read it and in C# you use newtonsoft or C# to just deserialize it into your own C# class that matches the json data
storing playfab user data is the same thing, define your own class and serialize it to json and store it on the user via Playfab API
I use PlayFab in pretty much all my projects, it's too easy and gives you cross platform user authentication and user data with a few lines of code.. and the playfab website dashboard shows all the users and let you modify their data etc and manage them, and even has password recovery (it will send an email to let them set a new password)
Thank you for your advices, will surely try to use and implement PlayFab in the future. I will probably look more into it tomorrow after classes
Unity is definitely working on adding more features/toolsets so take a look at what is out there but PlayFab is hard not to mention because it's just so simple compared to anything else right now and gives you a web site to administer all the users and their data etc. Unity will probably catch up in the future.
We've rolled our a lot of improvements to Cloud Save this year:
- Increased storage for Player Data
- Game Data for non-player game data (e.g. NPCs, entities, game state)
- Files for save game data in any format (up to 1 GB)
- Queries search / filter data stored in Cloud Save
- Cloud Code Trigger integration (e.g. run code when data changes)
- Access Classes (e.g. public, protected and private properties)
Like our other game backend services it's also integrated with authentication out of the box.
Example usage:
https://docs.unity.com/ugs/en-us/manual/cloud-save/manual/tutorials/unity-sdk
using UnityEngine;
using Unity.Services.Core;
using Unity.Services.Authentication;
public class CloudSaveSample : MonoBehaviour
{
private async void Awake()
{
await UnityServices.InitializeAsync();
await AuthenticationService.Instance.SignInAnonymouslyAsync();
}
public async void SaveSomeData()
{
var data = new Dictionary<string, object>{{"key", "someValue"}};
await CloudSaveService.Instance.Data.Player.SaveAsync(data);
}
public async void LoadSomeData()
{
Task<Dictionary<string, Item>> savedData = await CloudSaveService.Instance.Data.Player.LoadAsync(new HashSet<string>{"key"});
Debug.Log("Loaded data: " + savedData["key"]);
}
}
I'd would love any feedback on any gaps you have run into!
I need an advice on another matter (not connected with my music streamer app)
So I am creating this game for my finals exam and this is what i have so far
I am trying to create UI window for Options which would open up over the entire screen, but when I try to scale my UI assets they lose a lot of quality, basically they look low quality and pixilated