SpriteText.Layout_Multiline C# (CSharp) Method

Layout_Multiline() protected method

protected Layout_Multiline ( string lines ) : void
lines string
return void
	protected void Layout_Multiline(string[] lines)
	{
		float[] widths = new float[lines.Length];
		float totalHeight;
		float largestWidth = 0;
		Vector3 startPos = Vector3.zero;
		int charIndex = 0;

		// Find the total height of the text:
		float distBetweenLines = lineSpaceSize - characterSize;
		totalHeight = characterSize * ((float)lines.Length) + distBetweenLines * ((float)lines.Length - 1);

		// Find the width of each line:
		for (int i = 0; i < lines.Length; ++i)
		{
			widths[i] = spriteFont.GetWidth(lines[i]) * worldUnitsPerTexel * characterSpacing;

			// Find the longest line:
			if (largestWidth < widths[i])
				largestWidth = widths[i];
		}

		totalWidth = largestWidth;

		// Find the starting position
		switch (anchor)
		{
			case Anchor_Pos.Upper_Left:
				startPos = new Vector3(0, 0, offsetZ);
				topLeft = startPos;
				bottomRight = new Vector3(largestWidth, -totalHeight, offsetZ);
				break;
			case Anchor_Pos.Upper_Center:
				startPos = new Vector3(largestWidth * -0.5f, 0, offsetZ);
				topLeft = startPos;
				bottomRight = new Vector3(largestWidth * 0.5f, -totalHeight, offsetZ);
				break;
			case Anchor_Pos.Upper_Right:
				startPos = new Vector3(-largestWidth, 0, offsetZ);
				topLeft = startPos;
				bottomRight = new Vector3(0, -totalHeight, offsetZ);
				break;
			case Anchor_Pos.Middle_Left:
				startPos = new Vector3(0, totalHeight * 0.5f, offsetZ);
				topLeft = startPos;
				bottomRight = new Vector3(largestWidth, totalHeight * -0.5f, offsetZ);
				break;
			case Anchor_Pos.Middle_Center:
				startPos = new Vector3(largestWidth * -0.5f, totalHeight * 0.5f, offsetZ);
				topLeft = startPos;
				bottomRight = new Vector3(largestWidth * 0.5f, totalHeight * -0.5f, offsetZ);
				break;
			case Anchor_Pos.Middle_Right:
				startPos = new Vector3(-largestWidth, totalHeight * 0.5f, offsetZ);
				topLeft = startPos;
				bottomRight = new Vector3(0, totalHeight * -0.5f, offsetZ);
				break;
			case Anchor_Pos.Lower_Left:
				startPos = new Vector3(0, totalHeight, offsetZ);
				topLeft = startPos;
				bottomRight = new Vector3(largestWidth, 0, offsetZ);
				break;
			case Anchor_Pos.Lower_Center:
				startPos = new Vector3(largestWidth * -0.5f, totalHeight, offsetZ);
				topLeft = startPos;
				bottomRight = new Vector3(largestWidth * 0.5f, 0, offsetZ);
				break;
			case Anchor_Pos.Lower_Right:
				startPos = new Vector3(-largestWidth, totalHeight, offsetZ);
				topLeft = startPos;
				bottomRight = new Vector3(0, 0, offsetZ);
				break;
		}

		switch (alignment)
		{
			case Alignment_Type.Left:
				for (int i = 0; i < lines.Length; ++i)
				{
					Layout_Line(startPos, lines[i], charIndex);
					charIndex += lines[i].Length + 1; // Add one for the newline character at the end
					// Zero out the vertices corresponding to the newline:
					ZeroQuad(charIndex - 1);
					startPos.y -= lineSpaceSize;
				}
				break;
			case Alignment_Type.Center:
				for (int i = 0; i < lines.Length; ++i)
				{
					Layout_Line(startPos + (Vector3.right * 0.5f * (largestWidth - widths[i])), lines[i], charIndex);
					charIndex += lines[i].Length + 1; // Add one for the newline character at the end
					// Zero out the vertices corresponding to the newline:
					ZeroQuad(charIndex - 1);
					startPos.y -= lineSpaceSize;
				}
				break;
			case Alignment_Type.Right:
				for (int i = 0; i < lines.Length; ++i)
				{
					Layout_Line(startPos + (Vector3.right * (largestWidth - widths[i])), lines[i], charIndex);
					charIndex += lines[i].Length + 1; // Add one for the newline character at the end
					// Zero out the vertices corresponding to the newline:
					ZeroQuad(charIndex - 1);
					startPos.y -= lineSpaceSize;
				}
				break;
		}

		// See if we have any remaining capacity we need to
		// make disappear:
		if (charIndex < capacity)
		{
			for (int i = charIndex; i < capacity; ++i)
			{
				vertices[i * 4] = Vector3.zero;
				vertices[i * 4 + 1] = Vector3.zero;
				vertices[i * 4 + 2] = Vector3.zero;
				vertices[i * 4 + 3] = Vector3.zero;
			}
		}
	}