Unity Auto-generated Component Index class tool requirements origin
We need to get the components in the presets when we write the UI class
joystick = transform.Find("joystick");background = transform.Find("joystick/background");stick = transform.Find("joystick/stick");direction = transform.Find("joystick/direction");
Disadvantages
- Requires handwritten code
- Modifying game object names requires manual code modification
- Modifying levels requires manual code modification
- Handwriting is prone to problems
Solution Solutions
Use the editor to automatically generate the following index classes
public class MianBase : MonoBehaviour{ protected UISprite Left_Sprite; protected UITexture Right_Texture; protected UISprite Bottom_Sprite; protected UILabel Top_Label; public void InitBase() { Left_Sprite = transform.Find("Left/Sprite").GetComponent<UISprite>(); Right_Texture = transform.Find("Right/Texture").GetComponent<UITexture>(); Bottom_Sprite = transform.Find("Bottom/Sprite").GetComponent<UISprite>(); Top_Label = transform.Find("Top/Label").GetComponent<UILabel>(); }}
The UI code then inherits this class
You can use these components very conveniently.
Tool making
Effect
The core idea of tool making
- Select a preset Parent
- Iterating over the script on the Child and child objects
- Index variable names can be automatically populated with customizable
- Serialization classes
How to Use Tools
- Select a preset Parent
- Top left menu bar zouqiang/ui/Create Preset index class (shortcut key ctrl+alt+1) Open window
- Custom variable name or auto-fill variable name
- Click the Generate Code button
If you have a better solution, please do not hesitate to enlighten, thank you endless!
Source: Https://github.com/QiangZou/ZouQiang/blob/master/Assets/ZouQiang/Editor/AssetBundlesTool.cs
Unity Auto-Generate Component Index class tool