Click here to Skip to main content
15,888,097 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Unity 3d: how save Player position to the SQL Server database using MVC Code first?
Posted

1 solution

1. Create a unity project and add a game object into the scene.

2. Create a c# script and call it "SavePosition".

This is my script..

using UnityEngine;

using System.Collections;

public class SavePosition : MonoBehaviour
{
string url = "http://localhost:57495/";

public void SavePos()
{
string customUrl = url + "Players/Create";

//set up a form
WWWForm form=new WWWForm();

string x = transform.position.x.ToString("0.00");
string y = transform.position.y.ToString("0.00");
string z = transform.position.z.ToString("0.00");

string rx = transform.rotation.x.ToString("0.00");
string ry = transform.rotation.y.ToString("0.00");
string rz = transform.rotation.z.ToString("0.00");

//add stuff to the form now
form.AddField("Position",x+","+y+","+z);
form.AddField("Rotation",rx+","+ry+","+ry);

//call the form server
WWW www = new WWW(customUrl,form);

//call the request method
StartCoroutine(WaitForResponce(www));
}

// this method is responsible for accepting data from the server.....
public IEnumerator WaitForResponce(WWW www)
{
yield return www;

//checks for errors
if (www.error == null)
{
//print whatever is returned from the server if the is no error
Debug.Log(www.text);
}
else
{
//prints out if the is an error returned.......
Debug.Log(www.error);
}
}

void Update()
{
if (Input.GetKeyDown(KeyCode.Z))
{
//if the Z button is pressed the position and rotation will be saved!!!
SavePos();
}
}
}

Unity script ends here..

Once you done drag the script and drop into the game object you created

Now let go create an mvc project(I am using MVC 5)

1. Create a class database class call it "SavePlayerState"

Here is my class

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace SavePlayerState.Models
{
public class Player
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int PlayerId { get; set; }

public string PlayerPos { get; set; }
public string PlayerRotation { get; set; }
}
}

Now add a controller for the database class you just created...

On the controller your `Getall()` and `Create()` method must look like this..

public ActionResult Index()
{
return View(db.Players.ToList());
}

public ActionResult Create( Player player)
{
player.PlayerPos = Request.Form["Position"]; // The name position must be the same as one on unity script "AddField"
player.PlayerRotation = Request.Form["Rotation"];

if (ModelState.IsValid)
{
db.Players.Add(player);
db.SaveChanges();

//when everything is saved we want to send back a feedback to unity
Response.Clear();
Response.ContentType = "application/text;charset=utf-8";
Response.Write("Player states were save!!!");
Response.End();

return View();
}

return View();
}

Now if you don't have the views for these methods go ahead and scaffold the view.

On the create view go and remove everything that is there(html code)...leave it empty

Save everything and run your mvc project

Copy the link on top only this "http://localhost:57495/"(in my case).and paste it on the unity script on url

string url = "http://localhost:57495/";

And on the customUrl navigate to the create view

string customUrl = url + "Players/Create";

Save everything.....

Run your Mvc project...(this must be done before running the unity project)

Run unity and hit Z....

If it displays Player is saved on the console. It worked... :)

If it displays 404 Not found on the console... make sure you mvc project is running..

Thank you for reading this, I hope it helps.

And if am missing something I stand to be corrected..

Thanks again.....
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900