Example #1
0
        /// <summary>
        /// Finds UI Elements. If none found will create them and return false to indicate that placeholders are being used.
        /// </summary>
        /// <returns></returns>
        public bool FindUIElement()
        {
            // First see if this is on a canvas, if so note it
            if (!canvas)
            {
                canvas = GetComponent <Canvas>();
            }

            // Is this a placeholder canvas we just created for another bar?
            if (canvas && canvas.gameObject.name == PLACEHOLDER_CANVAS_NAME)
            {
                // This means another HealthUI created a placeholder, so we will use that canvas.
            }
            // If we have a canvas then search for the text/images
            else if (canvas)
            {
                if (canvas.gameObject.name != PLACEHOLDER_CANVAS_NAME)
                {
                    if (!UIText)
                    {
                        UIText = (searchChildren) ? canvas.GetComponentInChildren <Text>() : canvas.GetComponent <Text>();
                    }

                    if (!UIImage)
                    {
                        UIImage = (searchChildren) ? canvas.GetComponentInChildren <Image>() : canvas.GetComponent <Image>();
                    }
                }
            }
            // No canvas, so this may be the UI Element itself this is attached to
            else
            {
                if (!UIText)
                {
                    UIText = (searchChildren) ? GetComponentInChildren <Text>() : GetComponent <Text>();
                }

                if (!UIImage)
                {
                    UIImage = (searchChildren) ? GetComponentInChildren <Image>() : GetComponent <Image>();
                }
            }

            // If nothing was found after all of that - we need to make a canvas and UI elements.
            if (!UIText && !UIImage)
            {
                DebugX.LogWarning(!DebugX.logWarnings ? null : ("NSTSampleHealthUI on gameobject '" + name + "' cannot find any UI Canvas, Text or Image. Will create some placeholders until you supply them."), nst);
                DebugX.LogError(!DebugX.logErrors ? null : ("NSTSampleHealthUI on gameobject '" + name + "' cannot find a NetworkSyncTransform, UI Text, or UI Image component. Be sure the object we are attaching to conains one of those."), !nst);

                // Put some bars of this things head if it is an NST - Otherwise they won't make much sense on other objects.
                if (nst)
                {
                    CreatePlaceholderVitalBar(monitoredVitalId);
                }

                return(false);
            }
            return(true);
        }