Search Unity

Which script [method(s)] would be "efficient"?

Discussion in 'Scripting' started by triLight, Sep 25, 2014.

  1. triLight

    triLight

    Joined:
    May 7, 2014
    Posts:
    30
    I have two scripts that I have created and I was wondering of the people among the community could tell be which would be better and efficient then the other and why and in what instances would one be better than the other? I am aware unity's new UI makes this irrelevant but it would help me improve on a more technical level.

    (Sample 1)
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Button : MonoBehaviour {
    5.  
    6.     public string data = "www.google.com";        //Reference to string data
    7.     public bt buttonType;                        //Reference to the enum bt
    8.     public enum bt {Game, Options, Exit, URL};    //Reference to the enum bt
    9.  
    10.     void Update () {
    11.  
    12.         //Iterate through all the touches
    13.         for(int i = 0; i < Input.touchCount; i++){
    14.  
    15.             //If they are in the 'Began' touch phase
    16.             if(Input.GetTouch(i).phase == TouchPhase.Began){
    17.  
    18.                 //Create a reference of the touches position in world space
    19.                 Vector3 x = Camera.main.ScreenToWorldPoint(Input.GetTouch(i).position);
    20.  
    21.                 //Check if it overlaps this collider
    22.                 if(Physics2D.OverlapPoint(x) == collider2D){
    23.  
    24.                     //If so run this switch statement
    25.                     switch(buttonType){
    26.  
    27.                     //Quit the application
    28.                     case bt.Exit:
    29.                         Application.Quit ();
    30.                         break;
    31.  
    32.                     //Load a new scene
    33.                     case bt.Game:
    34.                         Application.LoadLevel(data);
    35.                         break;
    36.  
    37.                     //Load the 'Options' scene
    38.                     case bt.Options:
    39.                         Application.LoadLevel("Options");
    40.                         break;
    41.  
    42.                     //Open a URL
    43.                     case bt.URL:
    44.                         Application.OpenURL(data);
    45.                         break;
    46.                     }
    47.                 }
    48.             }
    49.         }
    50.     }
    51. }
    52.  
    and/or

    (Sample 2)
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class VirtualButtonHandler : MonoBehaviour
    5. {
    6.  
    7.     public Button[] buttons;        //Reference to Button array
    8.  
    9.     void Update () {
    10.  
    11.         //If touch count less then 0, return
    12.         if(Input.touchCount < 1)
    13.             return;
    14.  
    15.         //If the first touch has began
    16.         if(Input.GetTouch(0).phase == TouchPhase.Began){
    17.  
    18.             //Create a reference of the touch's position in world space
    19.             Vector3 touchPos = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
    20.  
    21.             //For each button in the array
    22.             foreach(Button b in buttons){
    23.  
    24.                 //Check for any overlay with the Vector3
    25.                 if(Physics.Overlap(touchPos) == b.myCol){
    26.  
    27.                     //If there is collsion...
    28.                     if(!b.isURL){
    29.  
    30.                         //...Load the scene
    31.                         Application.LoadLevel(b.data);
    32.                     }else{
    33.                         //...Open the URL
    34.                         Application.OpenURL(b.data);
    35.                     }
    36.                 }
    37.             }
    38.         }
    39.     }
    40. }
    41.  
    42. public class Button
    43. {
    44.     public Collider2D myCol; //Reference to collider
    45.     public string data;            //Reference to string
    46.     public bool isURL;            //Reference to boolean
    47. }
    48.