- Home /
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?
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.
Is there a debug only tag? Editor only means my text display won't show for dev builds on Android for example, correct?
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
Follow this Question
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