- Home /
Get value of clicked line of text field
Hi there. A quick question: I have a text UI in a panel. Is there any way to return the value of the clicked line?, using JS (as that's what most of my stuff is in at the moment, and I find it easier to understand).
So, just to clarify: if I have a text UI with:
item 1 item 2 item 3
and I click 'item 2', I'd want to be able to pass this to a variable so I could then use Debug.Log (myclickedline); // and It'd return 'Item 2'
Anyone have any ideas on the script I'd need to put together to do this with the minimum amount of CPU overhead? Many thanks.
yes, I could add button components throughout the UI element but in most program$$anonymous$$g languages, there's a way for the field you click to know which line you clicked. It can then return the line number and the value of what's on that line, so it can be stored in a variable for later use. I just wanted to avoid peppering the UI with buttons and wondered if there was a more straightforward way.
Answer by FortisVenaliter · May 20, 2015 at 09:28 PM
No, not that I'm aware of. Unity tries to expose only what's needed when it's needed to avoid overhead.
As far as I know, the only way to do this would be to split each line into it's own Text component and add buttons to each.
Answer by tperry1x · Oct 26, 2015 at 09:34 PM
Shame. In other languages, I'd do it like this:
put "option 1" & return & "option 2" & return & "option 3" into field "optionlist"
put the ClickedLine of field "optionlist" into myclicked // returns '2'
put line myclicked of field "optionlist" into myclickedoption
put myclickedoption // returns 'option 2'
(this is livecode), probably one of the most simplistic languages – can't believe there's no way to do this in Unity.
Answer by KillHour · Oct 27, 2015 at 06:43 AM
You could use a GraphicRaycaster cast through the mouse position to determine what Graphic the mouse is over and stick that in an onMouseDown() event. Also see Graphic.Raycast(). The only real downside is that each piece of text you want to check for needs to be its own gameObject for this to work.
Here's something similar I did to check which tile the mouse was over in my game (I used a Physics Raycaster, but the concept is similar)
public GameObject GetSelected() {
//Uses a ray cast from the camera to find what object the mouse is pointing at.
Ray ray = camera.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray, out hit, 1000F, camera.gameObject.GetComponent <Camera_Controller> ().layermask)) {
return hit.transform.root.gameObject;
} else
return null;
}
It's in C#, but should be easy enough to figure out what is going on.
Edit: This does a better job of explaining than I can.
http://gamedev.stackexchange.com/questions/93592/graphics-raycaster-of-unity-how-does-it-work
Ok, thank you bshalke. Seems like it'd work, but is far too much overhead and complexity. This isn't a problem with your answer, just a lack of foresight in the unity ID$$anonymous$$ Thank you for all your help.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Possible to edit the text markdown to call functions? 0 Answers
How to Convert FileInfo to TextAsset to add to List 1 Answer
Split Textasset into List 1 Answer