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 Kasc · Jun 01, 2015 at 05:53 PM · prefabdebug

Creating prefabs at runtime for debugging purposes

Is this a good or bad practice? I've set up the #if UNITY_DEBUG directive and I am currently creating some text display game objects within that.

The reason why I'm doing is this because I don't want to have to ship any prefabs out with the game that I'm only using for debug purposes (in this case, writing the contents of an int[,] on the screen).

You can see my runtime creation here:

Declared within class:

 #if UNITY_DEBUG
     private GameObject _boardDisplayCanvas, _boardText, _boardTextShadow;
 #endif

Run during Awake():

 #if UNITY_DEBUG
     // Set up text display of board if debug
     _boardDisplayCanvas = new GameObject("BoardDisplayCanvas (debug)");

     var canvas =_boardDisplayCanvas.AddComponent<Canvas>();
     canvas.renderMode = RenderMode.ScreenSpaceOverlay;

     var scaler = _boardDisplayCanvas.AddComponent<CanvasScaler>();
     var raycaster = _boardDisplayCanvas.AddComponent<GraphicRaycaster>();

     // Text shadow will be instantiated from this
     _boardText = new GameObject("BoardText (debug)");
     _boardText.transform.SetParentAndPosition(_boardDisplayCanvas.transform);

     var text = _boardText.AddComponent<Text>();
     var rectT = _boardText.GetComponent<RectTransform>();
     rectT.anchorMin = new Vector2(0, 1);
     rectT.anchorMax = new Vector2(0, 1);
     rectT.pivot = new Vector2(0, 1);
     rectT.anchoredPosition = new Vector3(10, -10, 0);

     text.text = "Board:\n0 0 0 0 0";
     text.font = Resources.GetBuiltinResource(typeof(Font), "Arial.ttf") as Font;
     text.fontStyle = FontStyle.Bold;
     text.fontSize = 15;
     text.horizontalOverflow = HorizontalWrapMode.Overflow;
     text.verticalOverflow = VerticalWrapMode.Overflow;

     _boardTextShadow = Instantiate(_boardText);
     _boardTextShadow.name = "BoardTextShadow (debug)";
     _boardTextShadow.transform.SetParentAndPosition(_boardDisplayCanvas.transform);
     _boardTextShadow.transform.SetAsFirstSibling();
     Vector2 pos = rectT.anchoredPosition;
     pos.x += 2;
     pos.y -= 2;
     _boardTextShadow.GetComponent<RectTransform>().anchoredPosition = pos;
     _boardTextShadow.GetComponent<Text>().color = Color.black;
 #endif

This works as I want, but it's annoying to have to type this out so perhaps I'm overthinking this and should just set up some text UI prefabs even if they will only appear in debug/editor builds?

I'm new to Unity so maybe there's a better way to do this? For me, I'd love to be able to create a prefab within the editor, and then somehow convert the prefab in to an equivalent generation script... is that possible?

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
0

Answer by Ashkan_gc · Jun 01, 2015 at 05:59 PM

Well in scene you can mark your game objects as editor only by setting their tag to "editor only" then they'll not be present in the builds of your project. Also what you are doing is possible as well. However I'm not sure if unity detects that your reference is only for editor time when trying to find dependencies or not. Probably you can test this by building a web player and then taking a look at the report which unity spits out about what is included.

Editor Only tag surely works however, You can put all your debugging stuff in them and then in other codes call them only in editor or just inside them write a code which finds stuff on other objects and show them.

Keep in mind that the code will be included in the build if you don't enclose it in #if UNITY_EDITOR #endif so make sure to do that if you don't absolutely want any of the code in the build. It is harmless most of the times to keep the code hwoever so don't lose much sleep on it unless it becomes a size/security issue.

Comment
Add comment · Show 2 · 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 Kasc · Jun 01, 2015 at 07:02 PM 0
Share

Is there a debug only tag? Editor only means my text display won't show for dev builds on Android for example, correct?

avatar image Ashkan_gc · Jun 02, 2015 at 06:14 AM 0
Share

Well the flag is only for editor. If you want to show it in debug builds then you need to do what you are doing at the moment. It's good to create for yourself calls which are only called at debug mode using the [conditional] attribute which you can look at here as well https://msdn.microsoft.com/en-us/library/aa664622(v=vs.71).aspx

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

MissingMethodException: UnityEngine.Collision.GetComponent 1 Answer

How to instantiate prefab when colliding with specified prefab 1 Answer

Script won't destroy prefab clones when overlapping... plz help 1 Answer

Removing A Component From An Instantiated Prefab After X More Are Instantiated 1 Answer

Putting prefab into scene messes with the transform origin? 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