Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by sxer · Jan 13, 2013 at 06:05 PM · meshunity 4copy

Unity 4.0 mesh duplication possible?

I need a code to duplicate mesh without original mesh disappearing. is it possible? how?

Update: Full Code

 #pragma strict
 
 var Test : Transform;
 
 var Nyil : Transform;
 var NyilStorage = new Array();
 
 var hit : RaycastHit;
 var curhit : RaycastHit;
 
 var hSliderOldValue : int[] = new int[10] ;
 var hSliderValue : int[] = new int[10] ;
 
 var Vertices : Vector3[];
 
 private var a = new Array("Models","Morph","Vertices");
 private var b = new Array("Number");
 private var c = new Array("Id","Mod","DirX" ,"DirY" ,"DirZ","Target");
 var LD : String[,,] = new String[0,0,0];
 
 var MousePos : Vector2;
 var ScreenPosV : Vector2;
 var SelNyil : int;
 
 function Start () {
     XML.OpenXML("Modules/" + "Modul"  );
     LD = XML.ReadXMLAdv(a,b,c);
     TestNewObj();
     
 }
 
 function Update () {
     
     SelectObject();
 }
 
 
 function OnGUI(){
 //print(hit.collider);
     if (GUI.Button(Rect(0,0,50,20),"press") ){
         Vertices = (hit.collider as MeshCollider).sharedMesh.vertices; 
         print((hit.collider as MeshCollider).sharedMesh.vertices[0]);
         for(var i = 1;i<LD.GetLength(1);i++){
             Vertices[ parseInt(LD[0,i,0]) ] = Vertices[ parseInt(LD[0,i,0]) ] + Vector3.up ;
             print(i);
             
         }
         (hit.collider as MeshCollider).sharedMesh.vertices = Vertices;
     }
 
     MorphSlider();
     
     if (GUI.changed){
         for(i = 0;i<hSliderValue.length;i++){
             if(hSliderOldValue[i] != hSliderValue[i]){
                 
                 hSliderOldValue[i] = hSliderValue[i];
                 NyilRePos(i);
                 //print("Changed " + i);
             }        
         }
     }
         
 
 
 }
 
 function SelectObject(){
 
     if(Input.GetMouseButtonUp(0)){
         if (Physics.Raycast (camera.ScreenPointToRay(Input.mousePosition), curhit)){
 //        print(curhit.collider.name + "  " + (curhit.collider as MeshCollider).sharedMesh );
             if (curhit.collider != null || (curhit.collider as MeshCollider).sharedMesh != null ){ 
                 if(hit.collider != curhit.collider && curhit.collider.name != "Arrow(Clone)"){
                     hit=curhit;
                     NyilDestroy();
                     NyilInit();
                     
                 }
                             
             }
         }
     }
     if(Input.GetMouseButton(0)){
     if(Input.GetMouseButtonDown(0)){
     SelNyil = -1;
         if (Physics.Raycast (camera.ScreenPointToRay(Input.mousePosition), curhit)){
         
         print("ar");
             if( curhit.collider.name == "Arrow(Clone)"){
                  var screenPosA : Vector3 = camera.WorldToScreenPoint (curhit.transform.position);
                  var screenPosB : Vector3 = camera.WorldToScreenPoint (hit.transform.position);
                  ScreenPosV = (Vector2( screenPosA.x,screenPosA.y ) - Vector2( screenPosB.x, screenPosB.y )).normalized ;
                  MousePos = Vector2(Input.mousePosition.x,Input.mousePosition.y) ;
                  SelNyil = (curhit.transform.GetComponent("Destroy") as Destroy).Count ;
                 //print(ScreenPosV + "  mp: " + MousePos);
                 //print("a");
             }
                 
         }}        
 //        print(SelNyil);
         if(SelNyil != -1){
             var MousePosCurr : Vector2 = Vector2(Input.mousePosition.x,Input.mousePosition.y) ;
             var NewSliderPos : int = hSliderOldValue[SelNyil] + Mathf.CeilToInt( ((MousePosCurr.x - MousePos.x) /30 * ScreenPosV.x ) + ( (MousePosCurr.y - MousePos.y) /30 * ScreenPosV.y) );
             hSliderValue[SelNyil] = (NewSliderPos < 0) ? 0 : NewSliderPos ;
             NyilRePos(SelNyil);
             
         }
     }
     if(Input.GetMouseButtonUp(0)){
         if(SelNyil != -1){
             hSliderOldValue[SelNyil] = hSliderValue[SelNyil];
         }
     }
 }
 
 
 function MorphSlider(){
 
     for( var i = 0;i<LD.GetLength(0) ;i++){
         hSliderValue[i] = GUI.HorizontalSlider(Rect(0,0 + i*22,80,20), hSliderValue[i], 0, 10 );
         GUI.Label(Rect(85,0 +i*22,80,20),hSliderValue[i] + "  Morph"+ i.ToString() );
     }
 
 
 }
 
 function NyilInit(){
 
     for(var i =0;i<LD.GetLength(0);i++){
         var pos = Vector3( parseInt(LD[i,0,2]) , parseInt(LD[i,0,3]) , parseInt(LD[i,0,4]) );
         NyilStorage.Add( Instantiate(Nyil,hit.collider.transform.position + pos, Quaternion.LookRotation(pos*-1 )) );
         ((NyilStorage[NyilStorage.length-1] as Transform).GetComponent("Destroy") as Destroy).SetCount(i);
     }
 
 }
 
 function NyilDestroy(){
 
      for (var value : Transform in NyilStorage) {
          (value.GetComponent("Destroy") as Destroy).SelfDestroy();
      }
      NyilStorage.Clear();
 
 }
 
 function NyilRePos(i){
     //print("value " + i +"pos: " + Vector3( parseInt(LD[i,0,2]) , parseInt(LD[i,0,3]) , parseInt(LD[i,0,4])) * (hSliderOldValue[i]+1) );
     (NyilStorage[i] as Transform).position = hit.collider.transform.position + Vector3( parseInt(LD[i,0,2]) , parseInt(LD[i,0,3]) , parseInt(LD[i,0,4])) * (hSliderValue[i]+1);
 
 }
 
 function TestNewObj(){
     
     
      var m = new Mesh();
      m.name = "Scripted_Plane_New_Mesh";
      
      // var MeshSource : Mesh = Test.GetComponent(MeshFilter).mesh;
     
      /* 
      var ver : Vector3[] = Test.GetComponent(MeshFilter).mesh.vertices;
      var tri : int[] = Test.GetComponent(MeshFilter).mesh.triangles;
      var uv : Vector2[] = Test.GetComponent(MeshFilter).mesh.uv;
      var norm : Vector3[] = Test.GetComponent(MeshFilter).mesh.normals;
      
      m.vertices = ver;
      m.triangles = tri;
      m.uv = uv;
      m.normals = norm;
     */
     
      var obj : GameObject = new GameObject("New_Plane_Fom_Script", MeshRenderer, MeshFilter, MeshCollider);
         
      obj.GetComponent(MeshFilter).mesh = Instantiate(Test.GetComponent(MeshFilter).mesh);//m;
      obj.GetComponent(MeshCollider).mesh = Instantiate(Test.GetComponent(MeshFilter).mesh);// m;
      obj.GetComponent("Renderer").renderer.material = Resources.Load("Materials/Default2") as Material;
 
 //    MeshFilter mf = GetComponent<MeshFilter>();
 //    mf.mesh = (Mesh)Instantiate(mf.mesh);
     
 }
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sxer · Jan 13, 2013 at 06:52 PM 0
Share

Thanks, but this is not a duplication. Prefabs use the same mesh, and i need a mesh copy not instancing one mesh several times.

update

 var Test : Transform;
 
 function TestNewObj(){
         
         
          var m = new $$anonymous$$esh();
          m.name = "Scripted_Plane_New_$$anonymous$$esh";
          
         //  var $$anonymous$$eshSource : $$anonymous$$esh = Test.GetComponent($$anonymous$$eshFilter).mesh;
          
          var ver : Vector3[] = Test.GetComponent($$anonymous$$eshFilter).mesh.vertices;
          var tri : int[] = Test.GetComponent($$anonymous$$eshFilter).mesh.triangles;
          var uv : Vector2[] = Test.GetComponent($$anonymous$$eshFilter).mesh.uv;
          var norm : Vector3[] = Test.GetComponent($$anonymous$$eshFilter).mesh.normals;
          
          m.vertices = ver;
          m.triangles = tri;
          m.uv = uv;
          m.normals = norm;
         
          var obj : GameObject = new GameObject("New_Plane_Fom_Script", $$anonymous$$eshRenderer, $$anonymous$$eshFilter, $$anonymous$$eshCollider);
             
          obj.GetComponent($$anonymous$$eshFilter).mesh = m;
          obj.GetComponent($$anonymous$$eshCollider).mesh = m;
          obj.GetComponent("Renderer").renderer.material = Resources.Load("$$anonymous$$aterials/Default2") as $$anonymous$$aterial;
     
     
     }

this is my last try (original mesh disapper)

avatar image Lovrenc sxer · Jan 13, 2013 at 06:52 PM 0
Share

Use comments to comment...

What are you tryng to do? And what is instatiation different than copying? Its not definitely linked, it has its own properties.

avatar image sxer sxer · Jan 13, 2013 at 07:22 PM 0
Share

no, unity 4.0 upgrade , all instantiated object shared one mesh without own properties. you can only change size, rotation and position, but if you change vertex data , all clone mesh vertex data change, AND your prefabs mesh AND imported mesh vertex data change also.

avatar image QuestionAsker · Jul 07, 2014 at 03:16 AM 0
Share

Did you ever find a solution to this? I have the exact same issue

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Jan 13, 2013 at 07:54 PM

Use Instantiate():

 MeshFilter mf = GetComponent<MeshFilter>();
 mf.mesh = (Mesh)Instantiate(mf.mesh);
Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sxer · Jan 13, 2013 at 08:33 PM 0
Share

not working. Original prefab mesh filter error ( type mismatch) so if i try to create a mesh copy, original mesh disappearing or if i change clone mesh data, original vertex data change also.

avatar image Lovrenc · Jan 13, 2013 at 08:35 PM 0
Share

This is exactly same code as i posted before. Casting mesh and saving reference into variable definately makes no changes.

You still havent told us what it is you want to do.

avatar image sxer · Jan 13, 2013 at 09:00 PM 0
Share

Ok i want to create a Starship creator with a morphable component. Example : youtube video

I stored build elements in a simple prefab with loaded fbx mesh. I need a copy of my selected prefab mesh data which i can morphing(change vertex data without original mesh data modifying).

First problem: Simple prefab instantiate. If i modifying vertex structure all clone and original vertex data modifying too.

Sencond Problem: If i try creating a clear mesh and copy all vertex, triangle, uv information from my donor/original prefab mesh, original prefab mesh data = null , and also modifying the original vertex data.

i hope this is answer for your question:) And sorry for my bad english, this is realy not my language.

avatar image Lovrenc · Jan 13, 2013 at 09:12 PM 0
Share

I might be wrong, but this seems to bo done exclusively by rotation/translation/scale. Just bunch of composite components with carefully positioned pivots and scripting.

avatar image robertbu · Jan 13, 2013 at 09:20 PM 0
Share

Note I testing the Instantiate() code above with a GameObject (not a Prefab) before posting, and it worked fine. Perhaps you need create a single GameObject from the prefab first that you use as a basis for your other ships. In my test, I saved off the original mesh, modified the Instantiated mesh, and then a few seconds later the code restored the original mesh.

Show more comments
avatar image
0

Answer by alwynd · Oct 13, 2013 at 03:12 PM

Hi , you should be able to do this with a simply one liner, clone the mesh, and assign it back to the shared mesh, this will effectively break the instance, and you can make any changes to the vertices, and it will only affect that instance, of the skinnedmeshrenderer.

myRenderer.sharedMesh = (Mesh) Instantiate( myRenderer.sharedMesh );

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

replace character mesh with new one, or transfer components from old to new mesh 0 Answers

Batching between copied meshes 1 Answer

Possible Unity Glitch. A few lines of code erasing prefab data. 1 Answer

Mesh dissapearing from Resources 0 Answers

Reading mesh data without Unity making a copy 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges