Пример #1
0
    public RichEditBlock(Transform parent, float width, float lineSpacing)
    {
        gameObject = new GameObject("RichEditBlock");
        transform = gameObject.AddComponent<RectTransform>();
        transform.SetParent(parent, false);
        transform.SetAsLastSibling();

        layoutElement = gameObject.AddComponent<LayoutElement>();
        layoutElement.preferredWidth = width;
        layoutElement.preferredHeight = 0;

        layoutGroup = gameObject.AddComponent<VerticalLayoutGroup>();
        layoutGroup.childForceExpandWidth = false;
        layoutGroup.childForceExpandHeight = false;
        layoutGroup.childAlignment = TextAnchor.LowerLeft;
        layoutGroup.spacing = lineSpacing;

        currentLine = AddNewLine();
    }
Пример #2
0
    public void Add(string text, Text textSample)
    {
        if (string.IsNullOrEmpty(text))
        {
            return;
        }

        textSample.gameObject.SetActive(true);

        string remain = text;
        string addableText = "";
        while (!string.IsNullOrEmpty(remain))
        {
            bool canAdd = false;
            bool forceAdd = false;

            addableText = GetAddableText(remain, textSample);
            canAdd = !string.IsNullOrEmpty(addableText);

            if (!canAdd && currentLine.IsEmpty)
            {
                canAdd = true;
                forceAdd = true;
                addableText = remain.Substring(0, 1);
            }

            if (!canAdd)
            {
                currentLine = AddNewLine();
                continue;
            }

            remain = remain.Substring(addableText.Length, remain.Length - addableText.Length);

            var textObject = GameObject.Instantiate(textSample.gameObject);
            var newText = textObject.GetComponent<Text>();
            newText.text = addableText;
            var textRectTransform = textObject.transform as RectTransform;
            textRectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, newText.preferredWidth);
            textRectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, newText.preferredHeight);

            currentLine.Add(textObject.transform as RectTransform, forceAdd);
            UpdateHeight();
        }

        textSample.gameObject.SetActive(true);
    }
Пример #3
0
    private RichEditLine AddNewLine()
    {
        RichEditLine line = new RichEditLine(transform, layoutElement.preferredWidth);
        lineList.Add(line);

        UpdateHeightForNewLine(line);

        return line;
    }
Пример #4
0
 private void UpdateHeightForNewLine(RichEditLine line)
 {
     if (currentLine != null)
     {
         previousHeight += currentLine.LayoutElement.preferredHeight + LineSpacing;
     }
     layoutElement.preferredHeight = previousHeight + line.LayoutElement.preferredHeight;
 }
Пример #5
0
    public void Add(RectTransform child)
    {
        bool canAdd = currentLine.CanAdd(child);
        if (!canAdd && !currentLine.IsEmpty)
        {
            currentLine = AddNewLine();
        }

        currentLine.Add(child, true);
        UpdateHeight();
    }