Advertisement
jamieTheCoder

FindGameObjectWithMethod

Sep 19th, 2022 (edited)
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.11 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. #if UNITY_EDITOR
  6. using UnityEditor;
  7. #endif
  8. public class FindGameObjectWithMethod : MonoBehaviour
  9. {
  10.     public enum Method
  11.     {
  12.         Tag,
  13.         Name,
  14.        
  15.     }
  16.     public enum What_To_Store
  17.     {
  18.         GameObjects,
  19.         ReferenceHolder
  20.     }
  21.     public What_To_Store what_To_Store;
  22.     [Tooltip("Type a tag or name in here to find it")]
  23.     [HideInInspector]
  24.     public string stringToFind;
  25.     [Tooltip("where the data is stored")]
  26.     [HideInInspector]
  27.     public GameObject[] foundObject;
  28.     [Tooltip("Needed for if you choose Tag for currentMethod")]
  29.     [HideInInspector]
  30.     public int foundObjectLength;
  31.     [Tooltip("choose your poison")]
  32.     public Method currentMethod;
  33.     [Tooltip("The container for a reference holder This Should be found on start")]
  34.     [SerializeField]
  35.     public static GameObject referenceHolder;
  36.     void Start()
  37.     {
  38.             FindGameObjectWithMethodFunction();
  39.             referenceHolder = GameObject.Find("ReferenceHolder");
  40.     }
  41.     public void FindGameObjectWithMethodFunction()
  42.     {
  43.             switch (currentMethod)
  44.             {
  45.             case Method.Tag:
  46.                 Debug.Log("Method Is Tag");
  47.                 if(stringToFind != null)
  48.                 {
  49.                     foundObjectLength = GameObject.FindGameObjectsWithTag(stringToFind).Length;
  50.                     foundObject = new GameObject[foundObjectLength];
  51.                     foundObject = GameObject.FindGameObjectsWithTag(stringToFind);
  52.                     if(foundObject == null)
  53.                     {
  54.                         Debug.Log("No GameObject exists with tag " + stringToFind);
  55.                     }
  56.                     break;
  57.                 }else
  58.                 {
  59.                     Debug.Log("StringToFind was null Looking for Reference holder");
  60.                     break;
  61.                 }
  62.             case Method.Name:
  63.                 if(stringToFind != null)
  64.                 {
  65.                     Debug.Log("Method Is Name");               
  66.                     foundObjectLength = 1;
  67.                     foundObject = new GameObject[foundObjectLength];
  68.                     foundObject[0] = GameObject.Find(stringToFind);
  69.                     if(foundObject[0] == null)
  70.                     {
  71.                         Debug.Log("No GameObject exists with name " + stringToFind);
  72.                     }
  73.                     break;
  74.                 }else
  75.                 {
  76.                     Debug.Log("StringToFind was null Looking for Reference holder");
  77.                     break;
  78.                 }
  79.             }
  80.         if(foundObject != null)
  81.         {
  82.             Debug.Log("foundObject already exists on script looking for "+ stringToFind);
  83.         }
  84.     }
  85.     #region Editor
  86.     #if UNITY_EDITOR
  87.     [CustomEditor(typeof(FindGameObjectWithMethod))]
  88.     public class FindGameObjectWithMethodEditor : Editor
  89.     {
  90.             SerializedProperty m_foundObjectProperty;
  91.  
  92.             private void OnEnable() {
  93.             m_foundObjectProperty = serializedObject.FindProperty("foundObject");
  94.         }
  95.  
  96.         public override void OnInspectorGUI()
  97.         {
  98.             base.OnInspectorGUI();
  99.             FindGameObjectWithMethod findGameObjectWithMethod = (FindGameObjectWithMethod)target;
  100.             EditorGUILayout.Space();
  101.             EditorGUILayout.LabelField("details");
  102.             findGameObjectWithMethod.stringToFind = EditorGUILayout.TextField(findGameObjectWithMethod.stringToFind);
  103.             var foundObjectspace = GUILayoutUtility.GetRect(GUIContent.none, GUIStyle.none, GUILayout.Height(100));
  104.             EditorGUI.PropertyField(foundObjectspace, m_foundObjectProperty, new GUIContent("Found Object "));
  105.         }
  106.  
  107.     }
  108.     #endif
  109.     #endregion
  110.  
  111. }
  112.  
Tags: tools
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement