using System.Collections.Generic; using UnityEngine; namespace GeNa.Core { public static class ObjectLayerUtility { #region Public Static /// /// Function used to apply the Object layers decorator too /// /// /// public static void ApplyLayersToObjects(GameObject rootGameObject) { if (rootGameObject == null) { return; } IEnumerable decorators = GetLayerObjectChildenObjects(rootGameObject); foreach (GeNaObjectLayerDecorator decorator in decorators) { int layerMask = LayerMask.NameToLayer(decorator.m_layerName); if (layerMask < 0 || layerMask > 31) { layerMask = 0; } string tag = string.Empty; if (decorator.Tags != null && decorator.TagIndex >= 0 && decorator.TagIndex < decorator.Tags.Length) tag = decorator.Tags[decorator.TagIndex]; if (string.IsNullOrEmpty(tag)) { tag = "Current"; } if (decorator.ApplyToChilden) { SetLayerToObject(GetChildenObjects(decorator.gameObject), layerMask); SetTagToObject(GetChildenObjects(decorator.gameObject), tag); } else { SetLayerToObject(decorator.gameObject, layerMask); SetTagToObject(decorator.gameObject, tag); } } } #endregion #region Private Static /// /// Apllies layer to object and it's childern /// /// /// private static void SetLayerToObject(GameObject[] currentGameObjects, int layerValue) { if (currentGameObjects.Length < 1) { return; } foreach (GameObject gameObject in currentGameObjects) { gameObject.layer = layerValue; } } /// /// Apllies layer to object /// /// /// private static void SetLayerToObject(GameObject currentGameObject, int layerValue) { if (currentGameObject == null) { return; } currentGameObject.layer = layerValue; } /// /// Apllies tag to object and it's childern /// /// /// private static void SetTagToObject(GameObject[] currentGameObjects, string tagValue) { if (currentGameObjects.Length < 1 || string.IsNullOrEmpty(tagValue) || tagValue == "Current") { return; } foreach (GameObject gameObject in currentGameObjects) { gameObject.tag = tagValue; } } /// /// Apllies tag to object /// /// /// private static void SetTagToObject(GameObject currentGameObject, string tagValue) { if (currentGameObject == null || string.IsNullOrEmpty(tagValue) || tagValue == "Current") { return; } currentGameObject.tag = tagValue; } /// /// Gets all the decorators from the root prefab /// /// /// private static IEnumerable GetLayerObjectChildenObjects(GameObject currentGameObject) { if (currentGameObject == null) { return null; } List decorators = new List(); Transform[] transforms = currentGameObject.GetComponentsInChildren(); if (transforms.Length > 0) { foreach (Transform transform in transforms) { GeNaObjectLayerDecorator decorator = transform.GetComponent(); if (decorator != null) { decorators.Add(decorator); } } } return decorators.ToArray(); } /// /// Gets all the childern gameobjects from the supplied object /// /// /// private static GameObject[] GetChildenObjects(GameObject currentGameObject) { if (currentGameObject == null) { return null; } List objects = new List(); Transform[] transforms = currentGameObject.GetComponentsInChildren(); if (transforms.Length > 0) { foreach (Transform transform in transforms) { objects.Add(transform.gameObject); } } return objects.ToArray(); } #endregion } }