- Home /
Script Efficiency
I have a pretty simple question. I have scripts for each button in my game's UI, all the scripts are similar but each does something different when the button is pressed.
My question is, is it more efficient to have a unique script on each object, or make one script that finds what object it is attached to and does a different action depending on the name/tag/whatever of the object. Here's an example of one of my buttons:
using UnityEngine;
using System.Collections;
public class playButton : MonoBehaviour {
public AudioClip buttonSound;
public Sprite downSprite;
public Sprite upSprite;
private SpriteRenderer myRenderer;
private float buttonY;
private float buttonX;
private bool beenMoved = false;
private GameObject adObject;
private bool touched = false;
// Use this for initialization
void Start () {
myRenderer = gameObject.GetComponent<SpriteRenderer>();
buttonY = this.transform.position.y;
buttonX = this.transform.position.x;
}
// Update is called once per frame
void Update () {
if (this.enabled) {
if (Input.touchCount == 1) {
Vector3 wp = Camera.main.ScreenToWorldPoint (Input.GetTouch (0).position);
Vector2 touchPos = new Vector2 (wp.x, wp.y);
var touch = Input.GetTouch (0);
if (collider2D == Physics2D.OverlapPoint (touchPos)) {
beenMoved = true;
if (touch.phase == TouchPhase.Began) {
myRenderer.sprite = downSprite;
transform.position = new Vector3 (buttonX, (buttonY - 0.05f));
}
if (touch.phase == TouchPhase.Canceled) {
myRenderer.sprite = upSprite;
transform.position = new Vector3 (buttonX, (buttonY + 0.05f));
}
if (touch.phase == TouchPhase.Ended) {
myRenderer.sprite = upSprite;
transform.position = new Vector3 (buttonX, (buttonY + 0.05f));
if(!touched){
audio.PlayOneShot (buttonSound);
CameraFade.StartAlphaFade (Color.black, false, 1f, 0f, () => {LevelLoad (); });
touched = true;
}
}
}
else {
if(beenMoved){
myRenderer.sprite = upSprite;
transform.position = new Vector3 (buttonX, (buttonY + 0.05f));
}
}
}
}
}
void LevelLoad(){
Application.LoadLevel ("level");
}
}
Answer by iwaldrop · Jun 03, 2014 at 05:08 AM
This is a case where I'd advocate for using SendMessage. You want to limit the number of Update loops running, that is the most expensive thing when it comes to rendering frames. If you ran that update loop 5 times then you'd be checking input 5 times, when you really only need it once.
Try the following:
Separate your input gathering and your button logic. Have your input gathering mechanism send a message to whatever game object it hits using a raycast.
The button will receive the message that it has been hit. Then it dispatches a message to the appropriate listener.
The listener will allow you to perform whatever action you want when it receives a message.
Psudo code:
Input:
if (raycast from pointer to world)
hitObject.sendMessage("OnPress", isDown)
Button:
public GameObject target;
public string methodName;
void OnPress(bool isDown)
{
target.SendMessage(methodName);
}
LevelLoader:
void LoadLevel()
{
Application.LoadLevel(SOME_LEVEL_NAME);
}
This kind of pattern allows great extensibility, and lets you design by composition. It also conforms to Unity's Component-Object-Observer model.
Note: Send$$anonymous$$essage here is completely acceptable because it's not happening every frame. If you were talking about sending a message every frame, I'd advocate for a c# event.
Hey thanks for the response! Sorry I am kind of new to scripting, I'm not really sure how to incorporate that into my existing code. I appreciate the psudo code that you posted and I looked up the script reference for GameObject.Send$$anonymous$$essage but I'm still having a hard time trying to put it all together.
Sorry for the late reply, but I seem to have stopped receiving emails from this site for some reason. I'll check if it's on my end.
The 'psudo code' I posted was actually mostly functional c# code. The only exception is the "raycast from pointer to world" bit. The most relevant page in the docs that covers that kind of thing is here.
Drop a message here if you have any problems or specific questions, and I'll try to manually check back here as well tomorrow.
Your answer
Follow this Question
Related Questions
WorldToScreenPoint in Update/OnGUI: Bad Performance 1 Answer
C# Script Template - how to make custom changes? 1 Answer
c# Adjust In-Game audio 1 Answer
C# Shooting a cannonball (ballistics) 1 Answer
need teleport script fixed slightly 1 Answer