Search Unity

Finding Game Objects and Components is now easier using this simple pattern

Discussion in 'Scripting' started by AhmedSabry19, Jul 2, 2018.

?

Was this useful ?

  1. Yes

    33.3%
  2. No

    66.7%
  1. AhmedSabry19

    AhmedSabry19

    Joined:
    Sep 17, 2015
    Posts:
    3
    I have some fun here
    for those who search for game objects and components in the scenes using
    Code (CSharp):
    1. GameObject.Find("")
    ,
    Code (CSharp):
    1. FindObjectOfType<>
    ,
    Code (CSharp):
    1. GetComponent<>
    or any other searching method. and since searching for objects and components is a high cost operation and annoying for some people. This is a very simple and easy pattern that will make the process easier.
    We start with making a class
    Code (CSharp):
    1. StaticObject
    which holds the game object, its name, and a reference to its components that we will need to use. and then we create a class
    Code (CSharp):
    1. AllStaticObjects
    which holds a list of the
    Code (CSharp):
    1. StaticObject
    to assign in the inspector, and an indexer to search for the desired game object by its name.

    Second step is creating a class
    Code (CSharp):
    1. StaticObjectComponent
    which holds a component and its name. then a class
    Code (CSharp):
    1. AllComponents
    which holds a list of
    Code (CSharp):
    1. StaticObjectComponent
    to assign in the inspector, and an indexer to search for the desired component by its name.

    Of course we don't forget to mark all the classes as
    Code (CSharp):
    1. [System.Serializable]
    so that they be displayed in the inspector.

    In our manager script, we make a singleton, and a public reference to
    Code (CSharp):
    1. AllStaticObjects
    class to assign them from the inspector.

    Now, if want to access a game object you can just type it like this :
    Code (CSharp):
    1. GameObject ui = (GameObject) StaticObjectsManager.Instance.AllObjects["UI Canvas"].TheGameObject;
    if you want to access a component, you will type it like this :
    Code (CSharp):
    1. GameObject ui = (GameObject) StaticObjectsManager.Instance.AllObjects["UI Canvas"].TheGameObject;
    It will look like this in the inspector


    This is the full code
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. [System.Serializable]
    7. public class AllStaticObjects
    8. {
    9.     public List<StaticObject> Objects;
    10.  
    11.     public StaticObject this[string name]
    12.     {
    13.         get
    14.         {
    15.             foreach (StaticObject staticObject in Objects)
    16.             {
    17.                 if (staticObject.Name == name)
    18.                 {
    19.                     return (staticObject);
    20.                 }
    21.             }
    22.  
    23.             throw (new MissingReferenceException("The game object with name -" + name + "- is not found"));
    24.         }
    25.     }
    26. }
    27.  
    28. [System.Serializable]
    29. public class StaticObject
    30. {
    31.     public string Name;
    32.     public GameObject TheGameObject;
    33.     public AllComponents AllComponents;
    34. }
    35.  
    36. [System.Serializable]
    37. public class AllComponents
    38. {
    39.     public List<StaticObjectComponent> Components;
    40.  
    41.     public StaticObjectComponent this[string name]
    42.     {
    43.         get
    44.         {
    45.             foreach (StaticObjectComponent component in Components)
    46.             {
    47.                 if (component.Name == name)
    48.                 {
    49.                     return (component);
    50.                 }
    51.             }
    52.  
    53.             throw (new MissingReferenceException("The component with name -" + name + "- is not found in the game object " + name));
    54.         }
    55.     }
    56. }
    57.  
    58. [System.Serializable]
    59. public class StaticObjectComponent
    60. {
    61.     public string Name;
    62.     public Component Component;
    63. }
    64.  
    65. public class StaticObjectsManager : MonoBehaviour
    66. {
    67.     public static StaticObjectsManager Instance;
    68.  
    69.     #region Singleton
    70.     private void Awake()
    71.     {
    72.         if (Instance == null)
    73.         {
    74.             Instance = this;
    75.             DontDestroyOnLoad(gameObject);
    76.         }
    77.         else
    78.         {
    79.             Destroy(gameObject);
    80.         }
    81.     }
    82.     #endregion
    83.  
    84.     #region Game Objects
    85.     public AllStaticObjects AllObjects;
    86.     #endregion
    87. }
     
    dinazihani and Oyedoyin1 like this.