- Home /
Where can I view the code created by the editor?
I created a Main Menu using the new UI in the editor and want to see how this looks in a C# script. In Visual Studio when you design a window you can see all the code by opening FormX.designer.cs. Is there a way to view the code that is created by the Editor?
No, it's not possible. UI in Unity uses completely different code (likely not even C#, but rather C++ and GL or DirectX) to draw windows and other UI elements. The generator code lives in a library that is compressed into a resources file and as far as I know can't be extracted and decompiled. Is there any particular reason for wanting to "see under the hood," as it were?
Yes, I want to replicate the main menu I created using the editor with code to dynamically change it
Answer by maximeb-valtech · Sep 07, 2017 at 03:40 PM
When editing UI in Unity, it doesn't create code like in Visual Studio Forms. Instead, it creates data that is serialized and serialized which has all the information about what type of object, it's position, and the values of its properties.
However, from what I understand of the comments on the question, what you want to do is a procedural UI and all you need to do is write your own code to make it. Just create a new GameObject and use the method AddComponent to add text, sprite, or whatever you want to it.
Here is an example:
var textObject = new GameObject("Text");
textObject.layer = UILayer;
textObject.transform.SetParent(openGroup.transform, false);
var text = textObject.AddComponent<Text>();
text.font = Settings.Font;
text.fontSize = Settings.FontSize;
text.color = Settings.FontColor;
text.text = Settings.OpenText;
text.alignment = TextAnchor.MiddleRight;
text.text = "Hello World";
However, for having done it and regretted it later, I cannot recommend this method at all. It is long, tedious, prone to error, and hard to modify.
Instead, if you want to make dynamic that you can modify and do fun stuff with, I recommend you make some template UI in the Unity Editor and save it to a Prefab. Later, when you want to instantiate it and change it's content, it is a lot easier to just do a prefab instantiation and just change the one value that is going to be different, like the text for example, rather than do the whole configuration programatically.
When I asked this question this was also my approach, thats why I wanted to see the code generated by the editor to have an idea of how it will look like. In fact I already did what you described without copying automatically created code, but ins$$anonymous$$d looking at the objects in the inspector and replicating them (what isn't as easy as it sounds because some stuff works way different in code than in the editor). Anyway after trial and error I figured out that creating and using prefabs is indeed the better way. But thank you anyway :D
Your answer
Follow this Question
Related Questions
Make custom editor for generic class to apply to all current and future child classes 1 Answer
Keep track of how many times the editor goes into play mode? 2 Answers
How can i add a second camera that will be showing only specific gameobject in game window ? 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers