void Awake()
    {
        npc          = GetComponentInParent <NPC>();
        player       = GameObject.Find("Player").GetComponent <Player>();
        gameManager  = GameObject.Find("GameManager").GetComponent <GameManager>();
        pFontLoader  = new PFontLoader(pFontInfo);
        OP           = new ObjectPool();
        charOrigin   = DialogueBoxAnim.gameObject.transform.GetChild(0);
        selectionBox = DialogueBoxAnim.gameObject.transform.GetChild(1);

        recycleObject = new List <GameObject>();
        nextSentence  = new Queue <char>();
        wordObject    = new List <CharInfoWrapper>();
        npcSelections = new List <NPCSelection>();
    }
    public void OnTextChange()
    {
        displayImage = GetComponent <Image>();

        if (text.Length == 0)
        {
            displayImage.color = transparent_color;
            return;
        }
        else
        {
            displayImage.color = Color.white;
        }

        pFontLoader = new PFontLoader(pFontInfo);

        text = text.ToUpper();
        string[] sentences = text.Split('\n');
        int      length    = sentences.Length;
        // Debug.Log("Length" + length);
        Vector2 totalSize = new Vector2(Mathf.CeilToInt(getLongestPixelStringWidth(sentences) + marginPixel * (text.Length - 1)),
                                        Mathf.CeilToInt((pFontLoader.charHeightInPixel + marginPixelVert) * length - marginPixelVert));
        Texture2D finalTex = new Texture2D((int)totalSize.x, (int)totalSize.y);

        finalTex.wrapMode   = TextureWrapMode.Clamp;
        finalTex.filterMode = FilterMode.Point;
        Color[] finalCols = finalTex.GetPixels(0, 0, finalTex.width, finalTex.height);
        for (int i = 0; i < finalCols.Length; ++i)
        {
            finalCols[i].a = 0.0f;
        }

        // parse each sentence
        int yOffset = finalTex.height - (int)pFontLoader.charHeightInPixel;

        foreach (string text in sentences)
        {
            Vector2 size = new Vector2(Mathf.CeilToInt(GetTotalPixelWidth(text) + marginPixel * (text.Length - 1)),
                                       Mathf.CeilToInt(pFontLoader.charHeightInPixel));
            Texture2D tex = new Texture2D((int)size.x, (int)size.y);

            //Debug.Log("texture size: " + tex.width + " , " + tex.height);
            tex.wrapMode   = TextureWrapMode.Clamp;
            tex.filterMode = FilterMode.Point;
            Color[] cols = tex.GetPixels(0, 0, tex.width, tex.height);

            int xOffset = 0;

            // Set all pixels alpha to transparent
            for (int i = 0; i < cols.Length; ++i)
            {
                cols[i].a = 0f;
                // cols[i] = Color.white;
            }

            for (int i = 0; i < text.Length; ++i)
            {
                PFontLoader.CharInfo charInfo = pFontLoader.chars[text[i]];
                Sprite  charSprite            = charInfo.sprite;
                Color[] charCols  = charSprite.texture.GetPixels((int)charSprite.rect.xMin, (int)charSprite.rect.yMin, (int)charSprite.rect.width, (int)charSprite.rect.height);
                int     charWidth = (int)charInfo.width;
                for (int j = 0; j < charCols.Length; ++j)
                {
                    int row    = Mathf.FloorToInt(j / charWidth);
                    int column = xOffset + j % charWidth;
                    //Debug.Log("row: " + row + " column: " + column);

                    cols[row * tex.width + column] = charCols[j];
                }
                xOffset += marginPixel + (int)charInfo.width;
            }

            //tex.SetPixels(cols);
            //tex.Apply();

            // apply texture to final texture
            int beginOffset  = yOffset * finalTex.width;
            int beginXOffset = 0;
            if (isCentered)
            {
                beginXOffset = Mathf.FloorToInt((finalTex.width - tex.width) / 2);
            }
            //Color[] texCols = tex.GetPixels(0, 0, tex.width, tex.height);
            for (int i = 0; i < cols.Length; ++i)
            {
                int finalIndex = beginOffset + Mathf.FloorToInt(i / tex.width) * finalTex.width + beginXOffset + i % tex.width;
                finalCols[finalIndex] = cols[i];
            }

            yOffset -= (int)(pFontLoader.charHeightInPixel + marginPixelVert);
        }

        finalTex.SetPixels(finalCols);
        finalTex.Apply();

        Sprite sprite = Sprite.Create(finalTex, new Rect(0, 0, finalTex.width, finalTex.height), new Vector2(0.5f, 0.5f), pixelPerUnit);

        displayImage.sprite = sprite;
        //displayImage.SetNativeSize();
        //Debug.Log(displayImage.sprite.rect);


        // else{
        //     GetComponent<RectTransform>().sizeDelta = new Vector2(sprite.rect.width, sprite.rect.height);
        // }
    }
 public void SetText(string text)
 {
     pFontLoader = new PFontLoader(pFontInfo);
     this.text   = parseString(text);
     UpdateView();
 }