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 mheyman89 · Jun 03, 2014 at 04:29 AM · c#performanceefficiency

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");
     }
 }
 
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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:

  1. 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.

  2. The button will receive the message that it has been hit. Then it dispatches a message to the appropriate listener.

  3. 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.

Comment
Add comment · Show 3 · 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 iwaldrop · Jun 03, 2014 at 05:09 AM 0
Share

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.

avatar image mheyman89 · Jun 03, 2014 at 05:39 AM 0
Share

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.

avatar image iwaldrop · Jun 08, 2014 at 06:43 AM 0
Share

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

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

22 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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