- Home /
Android 3d game - laggy, bad performance
Hey,
currently I'm developing a game with touch controls (5 Gui-buttons) but sadly it's really laggy :/ Right now there aren't that many objects in my scene nor big textures nor crazy lightning (all materials are unlit)
There's a Screenshot of the Performance Profiler ↓
Obviously it has something to do with the touchbuttons
.
Update-function of the LocThroww.cs - Script:
if (isPressed == true){
guiTexture.color = Color.gray;
}
else{
guiTexture.color = new Color(0.5f, 0.5f, 0.5f, 0.5f);
}
if (Screen.dpi != 0) {
dpi = Screen.dpi * 0.000006f;
}
else{
dpi = 1000;
}
guiTexture.pixelInset = new Rect(- 105000 * dpi, 130000 * dpi, 100000 * dpi, 100000 * dpi);
.
My Touchmanagerscript if needed:
using UnityEngine;
using System.Collections;
public class Touchmanager : MonoBehaviour {
public static int currTouch = 0;//so other scripts can know what touch is currently on screen
private Ray ray;//this will be the ray that we cast from our touch into the scene
private RaycastHit rayHitInfo = new RaycastHit();//return the info of the object that was hit by the ray
[HideInInspector]
public int touch2Watch = 64;
void Update ()
{
if(Input.touches.Length <= 0)
{
//if no touches then execute this code
}
else //if there is a touch
{
//loop through all the the touches on screen
for(int i = 0; i < Input.touchCount; i++)
{
currTouch = i;
Debug.Log(currTouch);
//executes this code for current touch (i) on screen
if(this.guiTexture.HitTest(Input.GetTouch(i).position)){
if(Input.GetTouch(i).phase == TouchPhase.Began)
{
this.SendMessage("OnTouchBegan");
}
if(Input.GetTouch(i).phase == TouchPhase.Ended)
{
this.SendMessage("OnTouchEnded");
}
if(Input.GetTouch(i).phase == TouchPhase.Moved)
{
this.SendMessage("OnTouchMoved");
}
if(Input.GetTouch(i).phase == TouchPhase.Stationary)
{
this.SendMessage("OnTouchStayed");
}
if(Input.GetTouch(i).phase == TouchPhase.Canceled)
{
this.SendMessage("OnTouchCanceled");
}
}
//outside so it doesn't require the touch to be over the guitexture
ray = Camera.mainCamera.ScreenPointToRay(Input.GetTouch(i).position);//creates ray from screen point position
switch(Input.GetTouch(i).phase)
{
case TouchPhase.Began:
//OnTouchBeganAnywhere();
this.SendMessage("OnTouchBeganAnyWhere");
if(Physics.Raycast(ray, out rayHitInfo))
rayHitInfo.transform.gameObject.SendMessage("OnTouchBegan3D");
break;
case TouchPhase.Ended:
//OnTouchEndedAnywhere();
this.SendMessage("OnTouchEndedAnywhere");
if(Physics.Raycast(ray, out rayHitInfo))
rayHitInfo.transform.gameObject.SendMessage("OnTouchEnded3D");
break;
case TouchPhase.Moved:
//OnTouchMovedAnywhere();
this.SendMessage("OnTouchMovedAnywhere");
if(Physics.Raycast(ray, out rayHitInfo))
rayHitInfo.transform.gameObject.SendMessage("OnTouchMoved3D");
break;
case TouchPhase.Stationary:
//OnTouchStayedAnywhere();
this.SendMessage("OnTouchStayedAnywhere");
if(Physics.Raycast(ray, out rayHitInfo))
rayHitInfo.transform.gameObject.SendMessage("OnTouchStayed3D");
break;
}
}
}
}
}
.
I'd be really really happy if someone could help me with my problem :/
thanks in advance :)
Both scripts are connected to a guitexture which changes the color as long as it is touched. The variable "isPressed" is set to static in order to get used by another script...
"The variable "isPressed" is set to static in order to get used by another script" - that's not what static
is for! That's what public
is for... https://unity3d.com/learn/tutorials/modules/beginner/scripting/getcomponent
Oh, thanks... :´D good to know
But my game is still laggy. Any Ideas why the "LocThroww"-Script uses that much CPU? :/
honestly, i saw scripts taking too much cpu time when they are multiplying numbers with many 0s after the point. And what are you doing with so high dpi numbers?
Your answer
