Untested structure to facilitate using void Reset() for getting components.
- Interface
- Implementation - MonoBehaviour
- Implementation - ScriptableObject
- Static Methods
- Abstract MonoBehaviour
- Abstract ScriptableObject
public interface IHasComponents
{
public void GetComponents();
}
public class MyScript : MonoBehaviour, IHasComponents
{
[SerializeField] private Transform _transform;
public void GetComponents()
{
_transform = this.transform;
}
private void Reset()
{
GetComponents();
}
}
public class MySO : ScriptableObject, IHasComponents
{
[SerializeField] private Transform _transform;
public void GetComponents()
{
_transform = GameObject.Find("some name").transform;
}
private void Reset()
{
GetComponents();
}
}
public static class InterfaceHelper
{
public static void RunAll_IHasComponents_GetComponents()
{
foreach (var behaviour in FindLoadedMembersOfInterface<IHasComponents>())
{
try
{
behaviour.GetComponents();
}
catch (System.Exception exception)
{
Debug.LogError(exception.Message);
}
}
}
public static List<T> FindLoadedMembersOfInterface<T>(bool includeScriptableObjects = true)
{
var found = new List<T>();
foreach (var monoBehaviour in Resources.FindObjectsOfTypeAll<MonoBehaviour>())
{
if (monoBehaviour is T behaviour)
{
found.Add(behaviour);
}
}
if (includeScriptableObjects)
{
foreach (var scriptableObject in Resources.FindObjectsOfTypeAll<ScriptableObject>())
{
if (scriptableObject is T behaviour)
{
found.Add(behaviour);
}
}
}
return found;
}
}
public abstract class ComponentBehaviour : MonoBehaviour
{
private protected virtual void Reset()
{
GetComponents();
}
private protected virtual void GetComponents()
{
throw new System.NotImplementedException();
}
private static void AllMembersGetComponents()
{
foreach (var behaviour in Object.FindObjectsOfType<ComponentBehaviour>())
{
try
{
behaviour.GetComponents();
}
catch (System.Exception exception)
{
Debug.LogError(exception.Message);
}
}
}
}
#if UNITY_EDITOR
[UnityEditor.MenuItem("Tools / " + nameof(ComponentBehaviour) + " / Get Components for Loaded Members")]
private static void MenuItemAllMembersGetComponents()
{
if (EditorUtility.DisplayDialog("Confirmation", "Are you sure you want to get all components " +
"for all Loaded members of type " + nameof(ComponentBehaviour) + "?", "Yes", "Cancel"))
{
AllMembersGetComponents();
}
}
#endif
Uses Resources.FindObjectsOfTypeAll<T>()
which finds all project objects, including assets and inactive objects. It is slow, but the benefit is that the ScriptableObject assets don't need to be inside a Resources folder.
public abstract class ComponentObject : ScriptableObject
{
private protected virtual void Reset()
{
GetComponents();
}
private protected virtual void GetComponents()
{
throw new System.NotImplementedException();
}
private static void AllMembersGetComponents(ComponentObject[] componentObjects = null)
{
if (componentObjects == null) componentObjects = Resources.FindObjectsOfTypeAll<ComponentObject>();
foreach (var behaviour in componentObjects)
{
try
{
behaviour.GetComponents();
}
catch (System.Exception exception)
{
Debug.LogError(exception.Message);
}
}
}
#if UNITY_EDITOR
[UnityEditor.MenuItem("Tools / " + nameof(ComponentObject) + " / Get Components for Loaded Members")]
private static void MenuItemAllMembersGetComponents()
{
var found = Resources.FindObjectsOfTypeAll<ComponentObject>();
if (EditorUtility.DisplayDialog("Confirmation", "Are you sure you want to get all components " +
"for all Loaded members of type " + nameof(ComponentObject) + "?" + System.Environment.NewLine +
"Found: " + found.Length + "", "Yes", "Cancel"))
{
AllMembersGetComponents(found);
}
}
#endif
}