// dialgoue box private CharInfoWrapper ShowLetter(char ch, Vector2 lastPos, Transform charOrigin) { if (char.IsWhiteSpace(ch)) { return(null); } PFontLoader.CharInfo ci = pFontLoader.chars[char.ToUpper(ch)]; var go = OP.GetItem(); var sr = go.GetComponent <SpriteRenderer>(); CharInfoWrapper ciw = new CharInfoWrapper(ci, go); if (sr == null) { sr = go.AddComponent <SpriteRenderer>(); sr.sortingLayerID = DialogueBoxAnim.gameObject.GetComponent <SpriteRenderer>().sortingLayerID; sr.sortingOrder = 1; } go.transform.SetParent(charOrigin); lastPos = getNextPos(lastPos, ci); go.transform.localPosition = lastPos; go.SetActive(true); recycleObject.Add(go); sr.sprite = ci.sprite; this.lastPos.Set(lastPos.x + ci.width / 2 / pixelPerUnit, lastPos.y); return(ciw); }
private Sprite generateSprite(string text) { Texture2D texture = new Texture2D((int)pFontLoader.GetLengthInPixel(text), (int)pFontLoader.charHeightInPixel); texture.wrapMode = TextureWrapMode.Clamp; texture.filterMode = FilterMode.Point; texture = InitTexture2D(ref texture); Color[] cols = texture.GetPixels(); int offset = 0; foreach (char c in text.ToCharArray()) { PFontLoader.CharInfo chInfo = pFontLoader.chars[c]; Rect rect = chInfo.sprite.rect; Color[] chCols = chInfo.sprite.texture.GetPixels((int)rect.xMin, (int)rect.yMin, (int)rect.width, (int)rect.height); for (int i = 0; i < chCols.Length; ++i) { int row = i / (int)chInfo.sprite.rect.width; int col = i % (int)chInfo.sprite.rect.width; cols[row * texture.width + offset + col] = chCols[i]; } offset += (int)chInfo.sprite.rect.width; } texture.SetPixels(cols); texture.Apply(); return(Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f), pixelsPerUnit)); }
private Vector2 getNextPos(Vector2 prevPos, PFontLoader.CharInfo ci) { if (prevPos.x + (marginInPixel.x + ci.width) / pixelPerUnit > boxSize.x / 2 - borderInPixel.x / pixelPerUnit) { IncreaseLine(); prevPos.Set(originPoint.x - marginInPixel.x / pixelPerUnit, prevPos.y - (marginInPixel.y + pFontLoader.charHeightInPixel) / pixelPerUnit); int len = wordObject.Count; for (int i = 0; i < len; i++) { prevPos.Set(prevPos.x + (marginInPixel.x + wordObject[i].ci.width / 2) / pixelPerUnit, prevPos.y); wordObject[i].GO.transform.localPosition = prevPos; prevPos.Set(prevPos.x + wordObject[i].ci.width / 2 / pixelPerUnit, prevPos.y); } } prevPos.Set(prevPos.x + (marginInPixel.x + ci.width / 2) / pixelPerUnit, prevPos.y); return(prevPos); }
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 CharInfoWrapper(PFontLoader.CharInfo ci, GameObject GO) { this.ci = ci; this.GO = GO; }