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)); }
// -------------------------------------------------------------------------------- // ----- SELECTION BOX ------------------------------------------------------------ private bool initSelectionBox() { npcSelections = npcConversation.GetSelections(); if (npcSelections.Count == 0) { return(false); } // calculate size float length = 0; float charCount = 0; foreach (NPCSelection npcSelection in npcSelections) { if (length < pFontLoader.GetLengthInPixel(npcSelection.selection)) { length = pFontLoader.GetLengthInPixel(npcSelection.selection); charCount = npcSelection.selection.Length; } } float height = npcSelections.Count; selectionBoxSize = new Vector2((borderInPixel.x * 2 + (length + marginInPixel.x * charCount)) / pixelPerUnit, (borderInPixel.y * 2 + height * pFontLoader.charHeightInPixel + (height - 1) * marginInPixel.y) / pixelPerUnit); selectionBox.GetComponent <SpriteRenderer>().size = selectionBoxSize; // calculate position Vector2 boxPos = new Vector2(this.boxSize.x / 2 + selectionBoxSize.x / 2 + borderInPixel.x / pixelPerUnit, 0f); selectionBox.localPosition = boxPos; setSelectionIconPos(0, selectionBoxSize); // collider var oldSize = selectionBox.GetComponent <BoxCollider>().size; selectionBox.GetComponent <BoxCollider>().size = new Vector3(selectionBoxSize.x, selectionBoxSize.y, oldSize.z); // set active selectionBox.gameObject.SetActive(true); // set current selection currentSelection = 0; // reset selections items Vector2 startPoint = new Vector2(-selectionBoxSize.x / 2 + borderInPixel.x / pixelPerUnit, selectionBoxSize.y / 2 - (borderInPixel.y + pFontLoader.charHeightInPixel / 2) / pixelPerUnit); lastPos = startPoint; for (int i = 0; i < npcSelections.Count; i++) { List <char> chars = npcSelections[i].selection.ToList(); foreach (char ch in chars) { if (char.IsWhiteSpace(ch)) { lastPos.Set(lastPos.x + pFontLoader.charWidthInPixel / pixelPerUnit, lastPos.y); } ShowLetter(ch, lastPos, selectionBox.transform); } lastPos.Set(-selectionBoxSize.x / 2 + borderInPixel.x / pixelPerUnit, lastPos.y - (marginInPixel.y + pFontLoader.charHeightInPixel) / pixelPerUnit); } return(true); }