Unity Copy the selected script and modifier file name and class name

Source: Internet
Author: User

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_editor
6 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.