Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by tperry1x · May 18, 2015 at 07:16 PM · listtextfieldtextassetclicked

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image iuliu_ · May 18, 2015 at 08:24 PM 0
Share

Have you tried adding a button component to the lines?

avatar image tperry1x · May 20, 2015 at 09:20 PM 0
Share

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tperry1x · Oct 27, 2015 at 09:12 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

how to split TextAsset into a Dictionary? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges