public void AddLabel(ref LabelCreateOptionsInterop createOptions)
        {
            var labelID = Marshal.PtrToStringAnsi(createOptions.LabelID);

            if (m_enableLabels)
            {
                if (m_labelViews.ContainsKey(labelID))
                {
                    DestroyLabel(labelID);
                }

                var labelView = new LabelView(ref createOptions, m_unityCanvas, m_iconTexturePages);

                m_labelViews.Add(labelID, labelView);
            }
        }
Example #2
0
        public LabelView(ref LabelCreateOptionsInterop createOptions, Canvas unityCanvas, List <Texture> iconTexturePages)
        {
            if (createOptions.HasTextComponent)
            {
                var newTextGameObject = GameObject.Instantiate(UnityEngine.Resources.Load <GameObject>(LabelTextPrefabPath));
                var createOptionsText = Marshal.PtrToStringAnsi(createOptions.Text);
                newTextGameObject.name = createOptionsText;

                m_textComponent          = newTextGameObject.GetComponent <UnityEngine.UI.Text>();
                m_textComponent.fontSize = createOptions.BaseFontSize;
                m_textComponent.text     = createOptionsText;
                m_textComponent.transform.SetParent(unityCanvas.transform, false);
                m_textComponent.transform.SetAsFirstSibling();

                float fontScaleFactor = (float)createOptions.FontScale / unityCanvas.scaleFactor;
                m_textComponent.transform.localScale = new Vector3(fontScaleFactor, fontScaleFactor, fontScaleFactor);

                var newOutline = newTextGameObject.GetComponent <UnityEngine.UI.Outline>();
                if (newOutline != null)
                {
                    newOutline.effectColor = createOptions.HaloColor.ToColor();
                }
            }

            if (createOptions.HasIconComponent)
            {
                var newIconGameObject = GameObject.Instantiate(UnityEngine.Resources.Load <GameObject>(LabelIconPrefabPath));
                newIconGameObject.name = createOptions.Text + " - Icon";

                m_iconComponent = newIconGameObject.GetComponent <UnityEngine.UI.RawImage>();
                m_iconComponent.transform.SetParent(unityCanvas.transform, false);
                m_iconComponent.transform.SetAsFirstSibling();

                m_iconComponent.texture = iconTexturePages[createOptions.iconTexturePage];

                var uvScale = new Vector2(1.0f / 65536.0f, 1.0f / 65536.0f); // UV Scaling done on Native-side when texture is loaded, corrected here.

                m_iconComponent.uvRect = new Rect((float)createOptions.iconU0 * uvScale.x, (float)createOptions.iconV1 * uvScale.y, (float)createOptions.iconU1 * uvScale.x - (float)createOptions.iconU0 * uvScale.x, (float)createOptions.iconV0 * uvScale.y - (float)createOptions.iconV1 * uvScale.y);
                m_iconComponent.rectTransform.sizeDelta = new Vector2((float)createOptions.iconWidth, (float)createOptions.iconHeight);

                float iconScaleFactor = 1.0f / unityCanvas.scaleFactor;
                m_iconComponent.transform.localScale = new Vector3(iconScaleFactor, iconScaleFactor, iconScaleFactor);
            }
        }
        public static void AddLabel(IntPtr labelServiceHandle, ref LabelCreateOptionsInterop createOptions)
        {
            var labelService = labelServiceHandle.NativeHandleToObject <LabelServiceInternal>();

            labelService.AddLabel(ref createOptions);
        }