Although we are programmers, the unnecessary coding should be free, and repetitive work will take up much of our time. In some cases we have to create a lot of similar footsteps, the structure of the same, but its function is not the same, the function we want to write their own, but the structure and other code we can actually lazy.
1 Unity copies the selected script and resets it2 This code is intended to respond to the creation of a large, repetitive script that, when used, will create a new script that modifies the class name inside the script when it is renamed3 4 scripts should be placed in editor5 ```
#if Unity_editor6 usingUnityengine;7 usingSystem.Collections;8 usingUnityeditor.projectwindowcallback;9 usingSystem.IO;Ten usingUnityeditor; One A Public classCopyselectscript - { - Static stringPath =""; the Public Static stringOldfilename =""; -[MenuItem ("Assets/copyselectscript",false,0)]//create/ - Public Static voidCreatebmobtab () - { + varFile =Selection.activeobject; - if(file!=NULL&& file. GetType ()! =typeof(File)) + { APath =Assetdatabase.getassetpath (file); atOldfilename =File.name; - -Projectwindowutil.startnameeditingifprojectwindowexists (0,scriptableobject.createinstance<copyscriptassetaction>(), -Getselectedpathorfallback () +"/newclonescript.cs", - NULL, - path); in } - } to Public Static stringGetselectedpathorfallback () + { - stringPath ="Assets"; the foreach(Unityengine.object objinchSelection.getfiltered (typeof(Unityengine.object), selectionmode.assets)) * { $Path =Assetdatabase.getassetpath (obj);Panax Notoginseng if(!string. IsNullOrEmpty (PATH) &&file.exists (path)) - { thePath =Path.getdirectoryname (Path); + Break; A } the } + returnpath; - } $ } $ - classcopyscriptassetaction:endnameeditaction - { the Public Override voidAction (intInstanceId,stringPathName,stringresourcefile) - {Wuyi //Create a resource theUnityengine.object obj =createassetfromtemplate (PathName, resourcefile); - //Highlight the resource Wu projectwindowutil.showcreatedasset (obj); - } About Internal StaticUnityengine.object Createassetfromtemplate (stringPahtname,stringresourcefile) $ { - //gets the absolute path of the resource to be created - stringFullName =Path.GetFullPath (pahtname); - //read local template file AStreamReader reader =NewStreamReader (resourcefile); + stringContent =Reader. ReadToEnd (); the Reader. Close (); - $ //gets the file name of the resource the stringFileName =path.getfilenamewithoutextension (pahtname); the stringOldfilename =path.getfilenamewithoutextension (resourcefile); the //Replace the default file name the //content = content. Replace ("#TIME", System.DateTime.Now.ToString ("yyyy mm month DD day HH:mm:ss dddd"); -Content =content. Replace (Oldfilename, fileName); in the //Write new file theStreamWriter writer =NewStreamWriter (FullName,false, System.Text.Encoding.UTF8); About writer. Write (content); the writer. Close (); the the //Refresh Local Resources + Assetdatabase.importasset (pahtname); - Assetdatabase.refresh (); the Bayi returnAssetdatabase.loadassetatpath (Pahtname,typeof(Unityengine.object)); the } the } -#endif -```
Unity Copy the selected script and modifier file name and class name