Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- #if UNITY_EDITOR
- using UnityEditor;
- #endif
- public class FindGameObjectWithMethod : MonoBehaviour
- {
- public enum Method
- {
- Tag,
- Name,
- }
- public enum What_To_Store
- {
- GameObjects,
- ReferenceHolder
- }
- public What_To_Store what_To_Store;
- [Tooltip("Type a tag or name in here to find it")]
- [HideInInspector]
- public string stringToFind;
- [Tooltip("where the data is stored")]
- [HideInInspector]
- public GameObject[] foundObject;
- [Tooltip("Needed for if you choose Tag for currentMethod")]
- [HideInInspector]
- public int foundObjectLength;
- [Tooltip("choose your poison")]
- public Method currentMethod;
- [Tooltip("The container for a reference holder This Should be found on start")]
- [SerializeField]
- public static GameObject referenceHolder;
- void Start()
- {
- FindGameObjectWithMethodFunction();
- referenceHolder = GameObject.Find("ReferenceHolder");
- }
- public void FindGameObjectWithMethodFunction()
- {
- switch (currentMethod)
- {
- case Method.Tag:
- Debug.Log("Method Is Tag");
- if(stringToFind != null)
- {
- foundObjectLength = GameObject.FindGameObjectsWithTag(stringToFind).Length;
- foundObject = new GameObject[foundObjectLength];
- foundObject = GameObject.FindGameObjectsWithTag(stringToFind);
- if(foundObject == null)
- {
- Debug.Log("No GameObject exists with tag " + stringToFind);
- }
- break;
- }else
- {
- Debug.Log("StringToFind was null Looking for Reference holder");
- break;
- }
- case Method.Name:
- if(stringToFind != null)
- {
- Debug.Log("Method Is Name");
- foundObjectLength = 1;
- foundObject = new GameObject[foundObjectLength];
- foundObject[0] = GameObject.Find(stringToFind);
- if(foundObject[0] == null)
- {
- Debug.Log("No GameObject exists with name " + stringToFind);
- }
- break;
- }else
- {
- Debug.Log("StringToFind was null Looking for Reference holder");
- break;
- }
- }
- if(foundObject != null)
- {
- Debug.Log("foundObject already exists on script looking for "+ stringToFind);
- }
- }
- #region Editor
- #if UNITY_EDITOR
- [CustomEditor(typeof(FindGameObjectWithMethod))]
- public class FindGameObjectWithMethodEditor : Editor
- {
- SerializedProperty m_foundObjectProperty;
- private void OnEnable() {
- m_foundObjectProperty = serializedObject.FindProperty("foundObject");
- }
- public override void OnInspectorGUI()
- {
- base.OnInspectorGUI();
- FindGameObjectWithMethod findGameObjectWithMethod = (FindGameObjectWithMethod)target;
- EditorGUILayout.Space();
- EditorGUILayout.LabelField("details");
- findGameObjectWithMethod.stringToFind = EditorGUILayout.TextField(findGameObjectWithMethod.stringToFind);
- var foundObjectspace = GUILayoutUtility.GetRect(GUIContent.none, GUIStyle.none, GUILayout.Height(100));
- EditorGUI.PropertyField(foundObjectspace, m_foundObjectProperty, new GUIContent("Found Object "));
- }
- }
- #endif
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement