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 ?
I have a couple of ideas. Since I’ve never done this for “real,” I cannot make a recommendation.
-
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.
-
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.