• 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
-1
Question by DeuS · Oct 09, 2013 at 12:02 PM · stringautomaticallyword

Split string, add action to every word

There is a text. Which must be split into words and to create an action to every word, when pressed, plays a sound file, specified for this word.

How to do it automatically? Let's say I know how to split a string into words, and how to add action to every word?

I need to create gameObect according to each word and specify Rect according to a word position, then add script to interact with this Rect? But how to calculate each word position?

And maybe there are another solution?

I add a little more explanation:

I dont know how to do 1) and 2).

 public class Texttext : MonoBehaviour {
     public GUIText guiText;
     public ActionOnClick actionScript;
     // Use this for initialization
     void Start () {
         guiText.text = "Text, text text!\nText? Text!\nText, text text, text...";
         string[] split = guiText.text.Split(new char[] { ' ', ',', '.', ':', '\t', '\n'});
         foreach (string str in split)
             if (str != "")
             {
                 Debug.Log(str);
                 // Create GameObject
                 GameObject go = new GameObject(str);
 
                 // 1) Calculate word position (!!!)
                 Vector3 wordPosition = new Vector3(0, 0, 0);
 
                 // 2) Create Rect according this position (!!!)
                 Rect wordRect = new Rect(wordPosition.x, wordPosition.y, wordWidth,wordHeight);
 
                 // Add action to this gameObject
                 go.AddComponent<ActionOnClick>();
                 go.GetComponent<ActionOnClick>().wordPosition = wordPosition;
                 go.GetComponent<ActionOnClick>().wordRect = wordRect;
             }
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 }

The problem is to automate this: Write text -> Calculate -> Click on Word get a Sound.

And that's what I want to get:

alt text

text.png (4.2 kB)
Comment
Add comment · Show 1
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 verenion · Oct 09, 2013 at 12:37 PM 0
Share

"HOW DO I CODE?" is basically the question you are asking. String manipulation, iteration and arrays are pretty basic concepts.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Sisso · Oct 09, 2013 at 12:29 PM

split a string into words,

http://msdn.microsoft.com/en-us/library/b873y76a.aspx

create gameObject

http://docs.unity3d.com/Documentation/Manual/InstantiatingPrefabs.html

specify Rect according to a word position

http://docs.unity3d.com/Documentation/ScriptReference/Camera.WorldToScreenPoint.html

how to calculate each word position

http://docs.unity3d.com/Documentation/Manual/GameInterfaceElements.html

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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

17 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

Related Questions

while passing to a function in Prime31 throws error 1 Answer

Word Wrapping!? I dont get it! 1 Answer

Remove the last word from a string 2 Answers

how to display a long string sentence by sentence instead of character by character? 1 Answer

int.parse with decimal? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges