Some understandings of Unity3d learning presets (prefab)

Source: Internet
Author: User

I've been thinking about it. if you want to create many objects with the same structure on the unity3d, how to do it, and then check the relevant data found that the default can solve the problem!

The concept of a pre-set: the assembly of components, prefabricated objects can be instantiated into the game Object.

Create a preset: you can repeatedly create game objects with the same structure.

.1 below to explain how to make a simple preset (up and down for the process and the result graph):



.2 Creating multiple prefabs_new (code, result diagram):

1 usingunityengine;2 usingsystem.collections;3  4 //[executeineditmode] means to run in edit mode (note that changing the properties of prefab other properties will also Change)5  public classCreateinstance:monobehaviour {6      publicGameobject prefabs =NULL;7     PrivateVector3 pos;8     voidStart () {9         if(prefabs! =NULL)Ten         { onepos =NewVector3 (0,2.7f, -10.5114f); a              for(inti =0; I <5; ++I) -             { -Object tmpobj =Instantiate (prefabs, pos, quaternion.identity); thePos.x = pos.x-3; -             } -         } -         Else +         { -Debug.Log ("prefabs is null"); +         } a     } at}


If (the combination of the preset c = preset B and the preset a) will form the data that modifies the preset a will not affect the embedded data in the preset c, the following method can help solve!

. 4 about nesting between Perfab (mainly a Perfab data modified nested this PERFAB data to do the corresponding update):

The code for the preset nested presets (which will get the latest Perfab data when instantiated):

usingunityengine;#ifUnity_editorusingunityeditor;usingunityeditor.callbacks;#endifusingSystem.Collections.Generic; [executeineditmode] public classprefabinstance:monobehaviour{ publicGameobject prefab;#ifUnity_editor//Struct of all components . Used for Edit-time visualization and Gizmo drawing     public structThingy { publicMesh mesh;  publicmatrix4x4 matrix;  publicList<material>materials; } [system.nonserializedattribute] publiclist<thingy> things =NewList<thingy> (); voidOnValidate () {things.        Clear (); if(enabled) Rebuild (prefab, matrix4x4.identity); }     voidonenable () {things.        Clear (); if(enabled) Rebuild (prefab, matrix4x4.identity); }     voidRebuild (gameobject source, matrix4x4 inmatrix) {if(!Source)return; matrix4x4 Basemat= Inmatrix * Matrix4x4.trs (-source.transform.position, quaternion.identity,; foreach(meshrenderer MrinchSource. Getcomponentsinchildren (typeof(Renderer),true) {things. ADD (Newthingy () {mesh= Mr. Getcomponent<meshfilter>(). sharedmesh, Matrix= Basemat *mr.transform.localToWorldMatrix, Materials=NewList<material>(mr.sharedmaterials)}); }         foreach(prefabinstance PiinchSource. Getcomponentsinchildren (typeof(prefabinstance),true))        {            if(pi.enabled &&pi.gameObject.activeSelf) Rebuild (pi.prefab, basemat*pi.transform.localToWorldMatrix); }            }     //editor-time-only Update:draw The meshes so we can see the objects in the scene view    voidUpdate () {if(editorapplication.isplaying)return; matrix4x4 Mat=transform.localtoworldmatrix; foreach(thingy TinchThings) for(inti =0; I < t.materials.count; i++) Graphics.drawmesh (t.mesh, Mat* t.matrix, t.materials[i], gameobject.layer,NULL, i); }     //picking Logic:since We don ' t have gizmos.drawmesh, draw a bounding cube around each thingy    voidOndrawgizmos () {drawgizmos (NewColor (0,0,0,0)); }    voidondrawgizmosselected () {drawgizmos (NewColor (0,0,1,. 2f)); }    voidDrawgizmos (Color Col) {if(editorapplication.isplaying)return; Gizmos.color=col; matrix4x4 Mat=transform.localtoworldmatrix; foreach(thingy TinchThings) {gizmos.matrix= Mat *t.matrix;        Gizmos.drawcube (, t.mesh.bounds.size); }            }     //baking stuff:copy in all the referenced objects to the scene on play or build[postprocessscene (-2)]     public Static voidonpostprocessscene () {foreach(prefabinstance PiinchUnityEngine.Object.FindObjectsOfType (typeof(prefabinstance)))    Bakeinstance (pi); }      public Static voidbakeinstance (prefabinstance Pi) {if(!pi.prefab | |!Pi.enabled)return; Pi.enabled=false; Gameobject Go= Prefabutility.instantiateprefab (pi.prefab) asgameobject; quaternion rot=go.transform.localRotation; Vector3 scale=go.transform.localScale; Go.transform.parent=pi.transform;; Go.transform.localScale=scale ; Go.transform.localRotation=rot; Pi.prefab=NULL; foreach(prefabinstance childpiinchGo. Getcomponentsinchildren<prefabinstance>()) bakeinstance (childpi); } #endif

the above is two if there are multiple associations ?

Some understandings of Unity3d learning presets (prefab)

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: 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.