Hi everyone. In this exercise we are going to pass data from UNITY 3D to an ASP.NET site. Eventually the site can connect and store data in SQL Server.
Unity 3D – Setup
1. Create a new GameObject, name it Scripts and assign with it a C# Script (LogData.cs)
2. For testing purposes we are going to create a new button in the OnGUI() method which will pass parameters to ASP.NET site upon click. Here is the whole implementation of the LogData class.
public class LogData : MonoBehaviour { //The URL to the server - In our case localhost with port number 2475 private string url = "http://localhost:2475/"; // Use this for initialization void Start () { } // Update is called once per frame void Update () { } void OnGUI() { // Make a background box GUI.Box(new Rect(10,10,100,90), "Loader Menu"); // Make the first button. if(GUI.Button(new Rect(20,40,80,20), "Connect")) { //when the button is clicked //setup url to the ASP.NET webpage that is going to be called string customUrl = url+ "LogUserScore.aspx"; //setup a form WWWForm form = new WWWForm(); //Setup the paramaters form.AddField("UserID", "47"); form.AddField("Score", "100"); //Call the server WWW www = new WWW(customUrl, form); StartCoroutine(WaitForRequest(www)); } } IEnumerator WaitForRequest(WWW www) { yield return www; // check for errors if (www.error == null) { //write data returned from ASP.NET Debug.Log(www.text); } else { Debug.Log("WWW Error: "+ www.error); } } }
ASP.NET – Setup
1. Create a new ASP.NET web application and create a Web Form with name LogUserScore.aspx
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace UnityConnectASP.NET { public partial class LogUserScore : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (Page.Request.HttpMethod == "POST") { //read paramaters string UserID = (Request.Form["UserID"]); string Score = (Request.Form["Score"]); //store data into MS SQL Database //---Insert into database etc etc--- //load total score from database //Using a dummy value 200 string totalScore = "200"; //Return data to Unity, such as total score Response.Clear(); Response.ContentType = "application/text; charset=utf-8"; Response.Write("Total Score: " + totalScore); Response.End(); } } } }
2. Optional: To allow your game to be played from any domain, in the root folder of the site (where the web.config is located), create a crossdomain.xml file and write:
<?xml version="1.0"?> <cross-domain-policy> <allow-access-from domain="*"/> </cross-domain-policy>
Testing the Code
1. Connect Button is clicked
3. Data returned to UNITY
WWW Error: 403 Forbidden
UnityEngine.Debug:Log(Object)
c__Iterator0:MoveNext() (at Assets/LogCat.cs:57)
Help Please.
LikeLike
The 403 Forbidden error is an HTTP status code which means that accessing the page or resource you were trying to reach is absolutely forbidden for some reason. Have you tried the crossdomain.xml file?
LikeLike
hello admin! i am new in unity please guide me.
i am working on a project in which i have to develop online multiplayer quiz game .
my question is what type of DB should i use for this kind of project i.e. how to handle multiplayers and how to connect unity to db, your this post “Write data from Unity 3D to SQL Server using ASP.NET” is making sense to me except the last part “ASP.NET – Setup”. please help me if you can this is my university project.
LikeLike
To handle Multiplayer in Unity I suggest to use PhotonNetwork but for a quiz the above method is acceptable.
“your this post “Write data from Unity 3D to SQL Server using ASP.NET” is making sense to me except the last part “ASP.NET – Setup”
You need to create an ASP.NET website. Here is a tutorial how to create an ASP.NET website: https://www.youtube.com/watch?v=KVlXccl-XBA
LikeLike
How to return data to Unity more than 1 rows?
LikeLike
For example: From ASP.Net, I return 2 row data. In Unity, first label will get first row in ASP, and second label will get second row in ASP
LikeLike
You can use JSON. Covert rows into JSON array and pass it to Unity.
LikeLike
Could you give a example about JSON to pass to Unity, please? I’m newbie and dont have more experience about this.
LikeLike
I will create a tutorial in the coming days.
LikeLike
I’m really waiting for your next tutorial. Thanks for your help.
LikeLike
Can i connect directly to a sql database without passing by ASP.net ?
LikeLike
Thank you for the great help.
But when I tried the same concept on my code It gave me 500 server error.. Could you help me figure out why and how to fix it?
Thanks
LikeLike
In visual studio debug your code to get more details about the error
LikeLike
very very thankfull
LikeLike