Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by quantum_rez · Jan 07, 2013 at 12:30 PM · androidgamelagonlineonline-scoreboards

why my android game lag when getting data from online mysql

hi all, i need your help.

i want to ask, why my game is lag when i get data from online mysql database, how can my game not lag if i want to have database online in it ???

any solution? if yes please help me here. i developed game for android device, using mysql online.

Thanks

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Landern · Jan 07, 2013 at 01:26 PM 0
Share

Why not post the offending code that makes the call. Can you supply some more information, are you using the unity's WWW class and post to some php/asp/form page?

avatar image quantum_rez · Jan 07, 2013 at 03:28 PM 0
Share

ok, this is my code for connect into database online :

DatabaseCS.cs :

 using UnityEngine;
 using System.Collections;
 using System.Data;
 using $$anonymous$$ySql.Data.$$anonymous$$ySqlClient;
 
 public class DatabaseCS : $$anonymous$$onoBehaviour {
     
     private static IDbConnection dbConnection;
     
     // Use this for initialization
     void Start () {
         //openSqlConnection();
     }
     
     public void OnApplicationQuit() {
 
         closeSqlConnection();
 
     }
     
     public static void openSqlConnection() {
         
         print ("Open");
         
        string connectionString = 
             
             "Server = ip;" +
 
             "Database = database name;" +
 
             "User ID = user id;" +
 
             "Password= my password;" +
 
             "Pooling=false";
         
         dbConnection = new $$anonymous$$ySqlConnection(connectionString);
 
         dbConnection.Open();
 
         Debug.Log("Connected to database.");
 
     }
     
     public static void closeSqlConnection() {
 
         dbConnection.Close();
 
         dbConnection = null;
 
         Debug.Log("Disconnected from database.");
 
     }
     
     public static IDataReader doQuery(string sqlQuery) {
 
         IDbCommand dbCommand = dbConnection.CreateCommand();
     
         dbCommand.CommandText = sqlQuery;
     
         IDataReader reader;
         
         reader = dbCommand.ExecuteReader();
     
         dbCommand.Dispose();
     
         dbCommand = null;
     
         return reader;
 
     }
 
 }


and then i use this code to get my data from database, just simple code for example how i can get data from my database into my android..

this code to set radius :

 DatabaseCS.openSqlConnection();
 IDataReader reader = DatabaseCS.doQuery("SELECT Radius FRO$$anonymous$$ mytower");
 reader.Read();
 
 sphereCol.radius = (float)reader["Radius"]+(curLvl)*(0.2f*(float)reader["Radius"]);


just like that how i get data from my database, it's for all the data that i want to get from database like attack, speed, health, radius, and many more, anything wrong that make my game lag ?

or maybe i used the wrong code to connect into database?

Thanks :D

avatar image quantum_rez · Jan 07, 2013 at 06:18 PM 0
Share

sorry, can you please give me example put the connection code and query code into coroutines? because i don't really understand about it, you can use my code above for the example

Thanks :D

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by $$anonymous$$ · Jan 07, 2013 at 04:09 PM

I'm more than certain that it lags because it waits for the database reply in the frame you're in when you call the query. You should not block the game while you wait for your query result. Try putting the connection code and query code into coroutines so you don't block your game while waiting for them.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image quantum_rez · Jan 08, 2013 at 01:29 PM 0
Share

sorry, can you please give me example put the connection code and query code into coroutines? because i don't really understand about it, you can use my code above for the example

Thanks :D

avatar image bruce965 · Jan 08, 2013 at 01:57 PM 0
Share

http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$onoBehaviour.StartCoroutine

avatar image
1

Answer by Bunny83 · Jan 08, 2013 at 02:04 PM

Never direct connect to a database from a game. Everyone that uses your game can read your username / password. Use a webserver + pup to encapsulate the db functions.

In the case that this isn't a game but an application that should connect to the users local database, you should put database queries into a desperate thread. Using coroutines won't help here since the database query is like an atomic instruction unless you use asynchronous queries which also uses a seperate thread.

Can you give a bit more details about what you actually do? Is it a game or is it just an application?

Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image quantum_rez · Jan 08, 2013 at 02:58 PM 0
Share

i want to make mobile game.

hmm, in my game i have some data like attack, speed, radius, etc. i used web server so i can change the data easily just like ad$$anonymous$$ area, but the problem i have is the game must online because it connect into web server and another problem is lag because the game must get data from my online web server and i used $$anonymous$$ySQL.

something like that, do you understand right? sorry for my bad english :D

hope you understand and can help me here.

thanks

avatar image bruce965 · Jan 08, 2013 at 03:02 PM 1
Share

Then it's absolutely a bad idea to use a DB$$anonymous$$ exposed to public as your. You should write a little proxy (in PHP?) that connects to database and sends client the requested data.

another problem is lag just use coroutines as explained in this link:

http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$onoBehaviour.StartCoroutine

avatar image quantum_rez · Jan 08, 2013 at 03:26 PM 0
Share

or maybe i must use sqlite to solved this? but i never use sqlite and don't understand about it at all..

hmm, ya i look into it but i don't still get how can start coroutines solve this, because i must always get the data every second right? but when i use coroutine there's time before i get my data, or i'm wrong? sorry i don't get it :D

can you please give me some example here with my code above?

thanks

avatar image bruce965 · Jan 08, 2013 at 05:10 PM 0
Share

 // WARNING: UNTESTED CODE!
 using UnityEngine;
 using System.Data;
 using $$anonymous$$ySql.Data.$$anonymous$$ySqlClient;

 public class example : $$anonymous$$onoBehaviour {
     void Start() {
         StartCoroutine(openSqlConnection());
     }

     public static void openSqlConnection() {
        print ("Open");

        string connectionString = 
          "Server = ip;" +
             "Database = database name;" +
             "User ID = user id;" +
             "Password= my password;" +
             "Pooling=false";

         dbConnection = new $$anonymous$$ySqlConnection(connectionString);

         yield return dbConnection.Open();

         Debug.Log("Connected to database.");
     }
 }

avatar image quantum_rez · Jan 08, 2013 at 10:20 PM 0
Share

my game still lag after i use coroutine haha..

any other way so my game not lag again?

Show more comments
avatar image
0

Answer by pippo19 · Sep 28, 2014 at 03:09 PM

Hi quantum_rez, I work at android app that should connect to online database. On PC and on unity editor it work; but if i build apk itsnt work. How u do to work it? Have u add uses permission in AndroidManifest?

Thanks

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Do we have 3d online game for android developing in unity3d now? 0 Answers

How to spawn a player in multiplayer on Android with Photon? 1 Answer

Unity Android lag when close to terrain 0 Answers

Simple 2D motion around a circle is laggy in Android. 1 Answer

Android Sound Crackling. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges