CreateImage() public static method

public static CreateImage ( Resources resources ) : GameObject
resources UnityEngine.Resources
return UnityEngine.GameObject
Exemplo n.º 1
0
        private Image NewImage()
        {
            var resources = new DefaultControls.Resources();
            var go        = DefaultControls.CreateImage(resources);

            if (Application.isPlaying)
            {
                GameObject.DontDestroyOnLoad(go);
            }
            go.layer = gameObject.layer;
            var rt = go.transform as RectTransform;

            if (rt)
            {
                rt.SetParent(rectTransform);
                rt.localPosition = Vector3.zero;
                rt.localRotation = Quaternion.identity;
                rt.localScale    = Vector3.one;
            }
            Image img = go.GetComponent <Image>();

            img.raycastTarget = false;
            return(img);
        }
Exemplo n.º 2
0
        protected void UpdateQuadImage()
        {
#if UNITY_EDITOR && !UNITY_2018_3_OR_NEWER
            if (UnityEditor.PrefabUtility.GetPrefabType(this) == UnityEditor.PrefabType.Prefab)
            {
                return;
            }
#endif
            m_OutputText = GetOutputText();

            matches = s_Regex.Matches(m_OutputText);

            if (matches != null && matches.Count > 0)
            {
                for (int i = 0; i < matches.Count; i++)
                {
                    m_ImagesPool.RemoveAll(image => image == null);

                    if (m_ImagesPool.Count == 0)
                    {
                        GetComponentsInChildren <Image>(true, m_ImagesPool);
                    }

                    if (matches.Count > m_ImagesPool.Count)
                    {
                        DefaultControls.Resources resources = new DefaultControls.Resources();
                        GameObject go = DefaultControls.CreateImage(resources);

                        go.layer = gameObject.layer;

                        RectTransform rt = go.transform as RectTransform;

                        if (rt)
                        {
                            rt.SetParent(rectTransform);
                            rt.anchoredPosition3D = Vector3.zero;
                            rt.localRotation      = Quaternion.identity;
                            rt.localScale         = Vector3.one;
                        }

                        m_ImagesPool.Add(go.GetComponent <Image>());
                    }

                    string spriteName = matches[i].Groups[1].Value;

                    Image img = m_ImagesPool[i];

                    Vector2 imgoffset = Vector2.zero;

                    if (img.sprite == null || img.sprite.name != spriteName)
                    {
                        if (inspectorIconList != null && inspectorIconList.Length > 0)
                        {
                            for (int s = 0; s < inspectorIconList.Length; s++)
                            {
                                if (inspectorIconList[s].name == spriteName)
                                {
                                    img.sprite                  = inspectorIconList[s].sprite;
                                    img.preserveAspect          = true;
                                    img.rectTransform.sizeDelta = new Vector2(fontSize * ImageScalingFactor * inspectorIconList[s].scale.x,
                                                                              fontSize * ImageScalingFactor * inspectorIconList[s].scale.y);
                                    imgoffset = inspectorIconList[s].offset;

                                    break;
                                }
                            }
                        }
                    }

                    img.enabled = true;

                    if (positions.Count > 0 && i < positions.Count)
                    {
                        img.rectTransform.anchoredPosition = positions[i] += imgoffset;
                    }
                }
            }
            else
            {
                // If there are no matches, remove the images from the pool
                for (int i = m_ImagesPool.Count - 1; i > 0; i--)
                {
                    if (m_ImagesPool[i])
                    {
                        if (!culled_ImagesPool.Contains(m_ImagesPool[i].gameObject))
                        {
                            culled_ImagesPool.Add(m_ImagesPool[i].gameObject);
                            m_ImagesPool.Remove(m_ImagesPool[i]);
                        }
                    }
                }
            }

            // Remove any images that are not being used
            for (int i = m_ImagesPool.Count - 1; i >= matches.Count; i--)
            {
                if (i >= 0 && m_ImagesPool.Count > 0)
                {
                    if (m_ImagesPool[i])
                    {
                        if (!culled_ImagesPool.Contains(m_ImagesPool[i].gameObject))
                        {
                            culled_ImagesPool.Add(m_ImagesPool[i].gameObject);
                            m_ImagesPool.Remove(m_ImagesPool[i]);
                        }
                    }
                }
            }

            // Clear the images when it is safe to do so
            if (culled_ImagesPool.Count > 0)
            {
                clearImages = true;
            }
        }