- Home /
Ways to break up a text label into separate game objects?
Hi guys so I am programming something that is going to function more like an app than a game and maintains certain functionalities similar to instagram. Right now my current challenge is taking a text label with text and essentially dissecting it into multiple game objects that are then pressable as buttons. it is supposed to act like hashtags and handles on instagram . So within the text labels giving certain texts the ability to be pressed.
The ability to divide a text game object seems next to impossible , but creating a mask or a duplicate text label with the pressable functionality may work . are there any methods you would recommend ?
Answer by robertbu · May 07, 2013 at 03:41 AM
I have a couple of ideas. Since I've never done this for "real," I cannot make a recommendation.
1) You could make invisible GUI buttons. Place normal buttons over all the text you want to be hot. Then you can define your own GUIStyle leaving out a texture for the 'Normal' state of the buttons. This solution has the added benefit that you can specify a texture 'Active' texture...quarter opacity perhaps...so the user gets a flash of a texture when the button is pressed as feedback.
2) Your mask idea could be implement as a GUI.DrawTexture and you could sample the color of the texture at the mouse cursor as feedback about the button. I played a bit with this idea:
public class DetectColor : MonoBehaviour {
public Texture2D tex;
private Rect rect;
void Start() {
rect = new Rect(100f, 100f, tex.width, tex.height);
}
void OnGUI () {
Event e = Event.current;
if (e.type == EventType.mouseDown) {
if (rect.Contains (e.mousePosition)) {
int x = (int)(e.mousePosition.x - rect.x);
int y = tex.height - (int)(e.mousePosition.y - rect.y);
Color color = tex.GetPixel(x, y);
Debug.Log ("x-y ("+x+"-"+y+" color="+color);
}
}
GUI.DrawTexture (rect, tex);
}
}
Note with this solution the opacity had to be non-zero for GetPixel to return correct values. Even the lightest value in the alpha channel and it worked.
That's Definitely Helpful Rober the only issue is the buttons I make are going to be based on user input such as #Unity @workingonStuff . I would have to position the button immediately over the text that is " marked" so to speak . I thought about writing an algorithm that too in the character size of text , looked at the combined length of a string and somehow positioned the buttons within the ball park of the texts in 3D space but that is going to be extremely difficult and it's a situation where there isn't much concrete within the project so all of those values are subject to change or be changed based on my clients decision.
I do appreciate the idea though , I am asking more for the brainstor$$anonymous$$g effect as it is because I am definitely going to have to explore solutions a bit above my own current understanding of string manipulation and interacting with text labels
Your answer
Follow this Question
Related Questions
Using a lot of calls and if else statement, is there a better way to do this? 1 Answer
Access specific Text among multiple children 0 Answers
How to display text on a gamebject? 3 Answers
Load a GameObject that is outside of script and set it active at the same time 1 Answer
I can't pause my game? How to make a pause menu in C# for SPace Shooter type Game? 2 Answers