• 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 gilgada · Apr 14, 2012 at 11:55 AM · guiobjecttextguitextmethod

Making a script for all gameobject GUITEXT.

Hi, I have a script that puts a few GUIText objects on to the screen at Start. For each one I am having to set its Font, Material and FontSize amongst other things. For the elements that stay the same, this feels too repetitive. I know I could declare a method that automates this process and then passes the variables to each GUItext object upon call, but I'm not sure how to start the method.

I tried doing

 public GUIText TextProperties(GUIText textGUI, Vector2 myOffset, int myFontSize, Vector2 textOffset)
 {
     textGUI.guiText.font = pipFont;
     textGUI.guiText.fontSize = myFontSize;
     textGUI.guiText.material = textMaterial;
     textOffset = textGUI.guiText.pixelOffset;
     textOffset.x = textGUI.guiText.pixelOffset.x + myOffset.x;
     textOffset.y = textGUI.guiText.pixelOffset.y + myOffset.y;
     textGUI.guiText.pixelOffset = textOffset;    
 }

where textGUI is declared upon method call, but it doesn't seem to work.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by GuyTidhar · Apr 14, 2012 at 03:05 PM

For this you should use prefabs. Read what they are: http://unity3d.com/support/documentation/Manual/Prefabs.html

Then how to instantiate (create) them: http://unity3d.com/support/documentation/Manual/Instantiating%20Prefabs.html

Here is an edit for you:

File #1: GUITextPlus.cs:

 // Attach this file to a GameObject, and make a prefab out of it
 using UnityEngine;
 using System.Collections;
 
 [System.Serializable]
 public class GUITextConfig
 {
     public Font     font;
     public int        fontSize;
     public Material material;
     
     public GUITextConfig(Font font, int fontSize, Material material)
     {
         this.fontSize = fontSize;
         this.font = font;
         this.material = material;
     }
 }

 [RequireComponent (typeof (GUIText))]
 public class GUITextPlus : MonoBehaviour 
 {
     public void Initialize(GUITextConfig initData)
     {
         
         guiText.font = initData.font;
         guiText.fontSize = initData.fontSize;
         guiText.material = initData.material;
     }
 }

File #2: GUITextManager.cs:

 // Attach this file to a game object, drag the prefab you created above to the      
 // "prefabWithGUITextPlus" slot.
 // Next step up as many GUITextConfig sets as you need (first number of total such 
 // elements and then the define/drag in the inspector your desired data.
 using UnityEngine;
 using System.Collections;
 
 public class GUITextManager : MonoBehaviour 
 {
     public GUITextPlus prefabWithGUITextPlus;
     
     public GUITextConfig[] guiTextSets;
     
     void Start() 
     {
         foreach(GUITextConfig guiTextSet in guiTextSets)
         {
             GUITextPlus gtext = Instantiate(prefabWithGUITextPlus) as GUITextPlus;
                gtext.Initialize(guiTextSet);
         }
     }
 }
Comment
Add comment · Show 20 · 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 GuyTidhar · Apr 14, 2012 at 03:13 PM 1
Share

The base are the same. I personal prefer C#. Do you have certain examples you are referring to?

avatar image GuyTidhar · Apr 14, 2012 at 03:14 PM 1
Share

You would still call "Instantiate" and assign it in C# exactly like in JS.

You are welcome to ask me specific questions if you have troubles with it.

avatar image GuyTidhar · Apr 14, 2012 at 03:22 PM 1
Share

Bottom line, once you have a prefab set (a work you do inside the editor). All you need is this:

http://unity3d.com/support/documentation/ScriptReference/Object.Instantiate.html

You can set the code to be C#.

avatar image bodec · Apr 14, 2012 at 03:32 PM 1
Share

upper right corner of the javascript sample is a drop down to show C# and boo. also i think making a couple GUISkins may speed up your set up, kinda make a skin assign it to each one.

avatar image GuyTidhar · Apr 15, 2012 at 09:17 PM 1
Share

You could do something like this:

'using UnityEngine; using System.Collections;

public class GUITextConfig { public Font font; public int fontSize; public $$anonymous$$aterial material;

     public GUITextConfig(Font font, int fontSize, $$anonymous$$aterial material)
 {
     this.fontSize = fontSize;
     this.font = font;
     this.material = material;
 }

}

[RequireComponent (typeof (GUIText))] public class GUITextPlus : $$anonymous$$onoBehaviour { public void Initialize(GUITextConfig initData) {

     guiText.font = initData.font;
     guiText.fontSize = initData.fontSize;
     guiText.material = initData.material;
 }

}'

Now, when you need a GUIText, you can attach this scripts (Remember to save it as GUITextPlus.cs). Once you instantiate the Prefab that has this script and GUIText on it, by referencing the GUITextPlus, you can call Initialize with new GUITextConfig;

Show more comments

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Scrolling Text 1 Answer

Moving GUIbutton over an object that does not follow the player? 2 Answers

Why this Error when trying to add a score using the new GUI ? 0 Answers

Restrict characters in GUI.TextField 4 Answers

How to find the origin point of a text line? 0 Answers

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