public GenericRenderer(SpriteRenderer spriteRenderer = null, ColorGroup colorGroup = null, AlphaGroup alphaGroup = null, Tilemap tilemap = null, Image image = null, RawImage rawImage = null, Text text = null, TextMeshProUGUI tmProText = null, CanvasGroup canvasGroup = null) { #region Exceptions bool[] list = new bool[] { spriteRenderer, colorGroup, alphaGroup, tilemap, image, rawImage, text, tmProText, canvasGroup }; int qttOfNull = 0; for (int i = 0; i < list.Length; i++) // false == null, true == not null { if (!list[i]) { qttOfNull++; } } if (qttOfNull < list.Length - 1) // More than 1 ins't null { //Debug.LogError("<GenericRenderer> More than one renderer ins't null, conflict."); } else if (qttOfNull > list.Length - 1) // All is null { //Debug.LogError("<GenericRenderer> All renderers is null."); } #endregion _spriteRenderer = spriteRenderer; _colorGroup = colorGroup; _alphaGroup = alphaGroup; _tilemap = tilemap; _image = image; _rawImage = rawImage; _text = text; _tmProText = tmProText; _canvasGroup = canvasGroup; StartValues(); }
public override void OnInspectorGUI() { ColorGroup colorGroup = (ColorGroup)target; RendererType newRendererType = (RendererType)EditorGUILayout.EnumFlagsField(new GUIContent("Renderer Type", "Type of renderers accepted."), colorGroup.rendererType); if (newRendererType != colorGroup.rendererType) { Undo.RecordObject(colorGroup, "Changed RendererType"); colorGroup.rendererType = newRendererType; } EditorGUILayout.Separator(); Color newColor = EditorGUILayout.ColorField(new GUIContent("Color", "Atual color value of the group."), colorGroup.color); if (newColor != colorGroup.color) { Undo.RecordObject(colorGroup, "Changed color"); colorGroup.color = newColor; } EditorGUILayout.Separator(); bool useRemote = EditorGUILayout.Toggle( new GUIContent("Use Remote Sprites", "Mark to modify renderers that ins't children of this component. (Only for SpriteRenderers)"), colorGroup.useRemoteObjects); if (useRemote != colorGroup.useRemoteObjects) { Undo.RecordObject(colorGroup, "Changed UseRemoteObjects."); colorGroup.useRemoteObjects = useRemote; } if (colorGroup.useRemoteObjects) { List <GameObject> remoteObjects = colorGroup.remoteObjects; bool restartRenderers = false; for (int i = 0; i < remoteObjects.Count; i++) { EditorGUILayout.BeginHorizontal(); remoteObjects[i] = (GameObject)EditorGUILayout.ObjectField(remoteObjects[i], typeof(GameObject), true); bool delete = GUILayout.Button(new GUIContent("Delete", "")); if (delete) { remoteObjects.RemoveAt(i); restartRenderers = true; } else if (remoteObjects[i] != null) { if (!RendererGroupUtils.IsValid(remoteObjects[i])) { Debug.LogWarning("That GameObject is not valid for ColorGroup remoteObjects."); remoteObjects.RemoveAt(i); } else { Undo.RecordObject(colorGroup, "Added a remote object."); restartRenderers = true; } } EditorGUILayout.EndHorizontal(); } remoteObjects.RemoveAll(x => x == null); remoteObjects.Add(null); colorGroup.remoteObjects = remoteObjects; if (restartRenderers) { Undo.RecordObject(colorGroup, "Atualize renderers"); colorGroup.AtualizeRenderers(); } } else { Undo.RecordObject(colorGroup, "Atualize renderers"); colorGroup.AtualizeRenderers(); } }
/// <summary> /// Get all renderers (recursively) of a gameObject of specific types. /// </summary> /// <param name="transform"></param> /// <param name="rendererTypes"></param> /// <returns></returns> public static List <GenericRenderer> GetAllRenderers(this Transform transform, RendererType[] rendererTypes) { bool spriteRend = rendererTypes.Contains(RendererType.SpriteRenderer); bool colorGroupRend = rendererTypes.Contains(RendererType.ColorGroup); bool alphaGroupRend = rendererTypes.Contains(RendererType.AlphaGroup); bool tilemapRend = rendererTypes.Contains(RendererType.Tilemap); bool imageRend = rendererTypes.Contains(RendererType.Image); bool rawImageRend = rendererTypes.Contains(RendererType.RawImage); bool textRend = rendererTypes.Contains(RendererType.Text); bool tmTextRend = rendererTypes.Contains(RendererType.TmProText); bool canvasGroupRend = rendererTypes.Contains(RendererType.CanvasGroup); List <GenericRenderer> genericRenders = new List <GenericRenderer>(); SpriteRenderer spriteRenderer = spriteRend ? transform.GetComponent <SpriteRenderer>() : null; ColorGroup colorGroup = colorGroupRend ? transform.GetComponent <ColorGroup>() : null; AlphaGroup alphaGroup = alphaGroupRend ? transform.GetComponent <AlphaGroup>() : null; Tilemap tilemap = tilemapRend ? transform.GetComponent <Tilemap>() : null; Image image = imageRend ? transform.GetComponent <Image>() : null; RawImage rawImage = rawImageRend ? transform.GetComponent <RawImage>() : null; Text text = textRend ? transform.GetComponent <Text>() : null; TextMeshProUGUI tmText = tmTextRend ? transform.GetComponent <TextMeshProUGUI>() : null; CanvasGroup canvasGroup = canvasGroupRend ? transform.GetComponent <CanvasGroup>() : null; genericRenders.Add(new GenericRenderer( spriteRenderer, colorGroup, alphaGroup, tilemap, image, rawImage, text, tmText, canvasGroup)); for (int i = 0; i < transform.childCount; i++) { SpriteRenderer spriteRenderer2 = spriteRend ? transform.GetComponent <SpriteRenderer>() : null; ColorGroup colorGroup2 = colorGroupRend ? transform.GetComponent <ColorGroup>() : null; AlphaGroup alphaGroup2 = alphaGroupRend ? transform.GetComponent <AlphaGroup>() : null; Tilemap tilemap2 = tilemapRend ? transform.GetComponent <Tilemap>() : null; Image image2 = imageRend ? transform.GetComponent <Image>() : null; RawImage rawImage2 = rawImageRend ? transform.GetComponent <RawImage>() : null; Text text2 = textRend ? transform.GetComponent <Text>() : null; TextMeshProUGUI tmText2 = tmTextRend ? transform.GetComponent <TextMeshProUGUI>() : null; CanvasGroup canvasGroup2 = canvasGroupRend ? transform.GetComponent <CanvasGroup>() : null; Transform child = transform.GetChild(i); genericRenders.Add(new GenericRenderer( spriteRenderer2, colorGroup2, alphaGroup2, tilemap2, image2, rawImage2, text2, tmText2, canvasGroup2 )); if (child.childCount > 0) { List <GenericRenderer> childComponents = child.GetAllRenderers(rendererTypes); genericRenders.AddRange(childComponents); } } return(genericRenders); }