| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | using System; |
| | using System.Collections; |
| | using System.Collections.Generic; |
| | using System.Linq; |
| | using UnityEngine; |
| |
|
| | namespace Mujoco { |
| |
|
| | |
| | public static class MjHierarchyTool { |
| |
|
| | public static IEnumerable<MjComponent> LinearizeHierarchyBFS(Transform start) { |
| | var open = new Queue<Transform>(); |
| | open.Enqueue(start); |
| | while (open.Count > 0) { |
| | var transform = open.Dequeue(); |
| |
|
| | var components = transform.GetComponents<MjComponent>(); |
| | foreach (var component in components) { |
| | if (component.isActiveAndEnabled) { |
| | yield return component; |
| | } |
| | } |
| |
|
| | foreach (Transform child in transform) { |
| | open.Enqueue(child); |
| | } |
| | } |
| | } |
| |
|
| | |
| | public static MjComponent FindParentComponent(MonoBehaviour start) { |
| | return FindParentComponent<MjComponent>(start); |
| | } |
| |
|
| | |
| | public static T FindParentComponent<T>(MonoBehaviour start) where T : MonoBehaviour { |
| | if (start.transform.parent == null) { |
| | return null; |
| | } |
| | var loopTransform = start.transform.parent; |
| | T result = loopTransform.GetComponent<T>(); |
| | while (result == null && loopTransform.parent != null) { |
| | loopTransform = loopTransform.parent; |
| | result = loopTransform.GetComponent<T>(); |
| | } |
| | return result; |
| | } |
| |
|
| | public static List<T> GetComponentsInImmediateChildren<T>(Transform parent) |
| | where T : MonoBehaviour { |
| | var components = new List<T>(); |
| | foreach (Transform child in parent) { |
| | components.AddRange(child.gameObject.GetComponents<T>()); |
| | } |
| | return components; |
| | } |
| |
|
| | |
| | public static T FindComponentOfTypeAndName<T>(string name) where T : MjComponent { |
| | var components = UnityEngine.Object.FindObjectsOfType<T>().Where( |
| | component => component.gameObject.name == name); |
| | var numComponents = components.Count(); |
| | if (components == null || numComponents == 0) { |
| | throw new ArgumentException($"No components named '{name}' were found."); |
| | } |
| | if (numComponents > 1) { |
| | throw new ArgumentException($"More than one component named '{name}' was found."); |
| | } |
| | return components.FirstOrDefault() as T; |
| | } |
| | } |
| | } |
| |
|