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 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.

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

changing font + size of text 1 Answer

How to make text damage notifiers above enemy 1 Answer

Script to change GUI text's displayed text value? 7 Answers

HELP with scripting where GUI is involved 2 Answers

GUIText not working properly with a timer. 1 Answer


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