Introduction
We always like to define the protection level of the field as private when writing a script, but it is often wrong because of the spelling or case of the path and other reasons. And every time there is a lot of repetitive code that we need to write, FieldScriptWriter's just to optimize the problem.
We often need to write code in this form when making UI Script:
FieldScriptWriter can help us automatically generate this form of code.
instruction
1.AutoWriteVariablesWindow
Open the menu "Tools/AutoWriterVariablesWindox"then you can open a unity editor window
I create a demo ui panel for teach.First of all,we selected a protection level of the field,the selection can decide your field be public or private(Auto Wirte Findpath).We choose private.
Then, we drag the gameobject that we want to make our script attch to Attach Object field.It will be on the basis of the variables transform path.In this instraction,we drag the LoginPanelDemo to it.
and then we need to input a classname for our script.such as LoginPanelDemoContainer.
next step, we can choose one or many gameobjects that we want to get it or their compoent in our script and click the button"Please Selected The GameObject Then Click Me"
and then we can edit every variables type name and note.If the you want to define your custom type that you want to get,open the customType toggle.popon provide frequently-used type, and you can input the variables's name(default value is the gameobject'name to lower) and note,then click the button"Create"
Then you can select a path to save your script ,and generate it, Let's check the script!!!
That's pefect! you nerver worry about input wrong path and nerver write these repetitive code!
2.GetPrivateCode And Get The Transform Path.
Sometimes,we want to modify our code ,such as add new variables or change the path.In this case, we don't need to generate a new script file. Now we can use the GetPath function directly to get the code for this component or to get its path in the Hierarchy panel (to the root path).
We only added the feature directly to find the get code for some common components. Other components or a custom scripting component can use our access path function.
1.Copy FindPathCode To Clipboard
All we need to do is select a gameobject and then expand the menu of the components you want to get and select "CopyPrivateGetCode"
At this point, our code is written to the clipboard, so as long as we use Ctrl+V to paste this line of code:
transform.Find("Canvas/LoginPanelDemo").GetComponent<RectTransform>();
Note: the code path here starts with rootpath.Please make appropriate modifications to the path!!!
2.Get The Path From Root Only
For some components, such as user-defined scripts, there are no "CopyPrivateGetCode" options in the menu, so we can simply get the path of the gameobject from rootpath in the Hierarchy.
Right click on gameobject select FieldScriptWriter/GetPathToRoot to write the path of the gameobject to the clipboard.
We use Ctrl+V to paste this line of path:"Canvas/LoginPanelDemo/BackGround/Title"
Note: The path here starts with rootpath.Please make appropriate modifications to the path!!!
So, using this asset allows you to avoid many of the problems of when you get the subobject components in the script. That's what this asset is all about!