Several common methods for components in the Unity plug-in extension

Source: Internet
Author: User

Recently, an extension of the Unity editor has been written for art, mainly to alleviate the tedious and error of the mechanized operation of the fine arts in modifying prefabricated objects. Specific implementation of several functions:

1, delete the specified components;

2, copy, paste the specified components;

3. Re-associate the new attribute;

4. Re-preservation of prefabricated objects;

Delete a component of the specified type

 Public Static void Removecomponenthandler (Gameobject gameobject, Type componenttype)    {        foreach (var in gameobject.getcomponents<component>())        } {             if (Component. GetType () = = ComponentType)            {                gameobject.destroyimmediate (component);         }}}

Second, the replication component (here is to replicate only one type of component at a time)

 Public Static voidCopycomponenthandler (Type componenttype, Gameobject fromgameobject, Gameobject togameobject) {RemoveCompon        Enthandler (Togameobject, ComponentType); //Find the Component you want to copyComponent needcopycomponent =NULL; foreach(varComponentinchFromgameobject.getcomponents<component>())        {            if(Component. GetType () = =ComponentType) {needcopycomponent=component;  Break; }        }        //to paste an operation// http://answers.unity3d.com/questions/907294/copy-all-components-from-a-gameobject-and-paste-to.htmlUnityEditorInternal.ComponentUtility.CopyComponent (needcopycomponent);    UnityEditorInternal.ComponentUtility.PasteComponentAsNew (Togameobject); }

Third, associating new attributes

is to iterate over the specified gameobject, then find the component it is attached to and reset its value.

Iv. Replacement of prefabricated objects

Gameobject Activegameobject =Selection.activegameobject;if(Activegameobject! =NULL){    //gets the current ID    if(NewRegex (@"^\d+h$"). IsMatch (Activegameobject.name)) {Unityengine.object ParentObject=NULL; stringStrprefabpath =""; if(Prefabutility.getprefabtype (activegameobject) = =prefabtype.prefabinstance) {parentobject=editorutility.getprefabparent (Activegameobject); Strprefabpath=Assetdatabase.getassetpath (ParentObject); }        //Find ID        stringStrid =NewRegex (@"h$"). Replace (Activegameobject.name,""); //Sixth step Save the Prefab object        stringStrcurrselectprefabname =Activegameobject.name; if(Strprefabpath.endswith (". Prefab"))        {            //string[] dependpaths = assetdatabase.getdependencies (Strprefabpath);Gameobject go = Gameobject.instantiate (Gameobject.find (strcurrselectprefabname)) asGameobject;            Prefabutility.replaceprefab (Go, ParentObject, replaceprefaboptions.connecttoprefab);            Gameobject.destroyimmediate (Activegameobject); Go.name=Strcurrselectprefabname;        Assetdatabase.refresh (); } Debug.Log ("prefabricated Objects"+ Strcurrselectprefabname +"The modification is complete. "); }    Else{Debug.Log ("the currently selected Gameobject name does not meet the requirements, format: Id+h. \tgameobject Name:"+activegameobject.name); }           }

The most core lines of code:

1, the instantiation of a new gameobject;

2. Replacement of prefabricated objects;

3, destroy the old Gameobject;

4, refresh the resources;

The most complicated and troublesome thing for a colleague of art is to re-associate attributes, especially skeletal animation. Because there is no unified specification, so which part of the animation is actually need a layer to find, I looked at them all feel tired, how to do? I came up with the idea of finding a new component by name and then re-assigning the value association. Find a child node under a gameobject by name (if the name is unique)

 Public StaticGameobject Findchildgameobject (gameobject parent,stringchildname) {        if(Parent.name = =childname) {            returnparent; }        if(Parent.transform.childCount <1)        {            return NULL; } gameobject obj=NULL;  for(inti =0; i < Parent.transform.childCount; i++) {Gameobject go=Parent.transform.GetChild (i). Gameobject; Obj=findchildgameobject (go, childname); if(obj! =NULL)            {                 Break; }        }        returnobj; }

The above basically implements a few common methods of components:

1, add components (first copy and paste);

2, delete components;

3. Find sub-components by name;

4, update the prefabricated objects;

Several common methods for components in the Unity plug-in extension

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.