c# - Deep copy of a List -


this should simple problem solve i've tried several ways of doing results same.

i'm trying copy list containing gameobjects list. problem seems i'm copying references since changes done gameobjects of original list, affect ones on new list, don't want happen. i've read i'm doing shallow copy instead of deep copy, tried use following code clone each object:

public static class objectcopier     {         /// <summary>         /// perform deep copy of object.         /// </summary>         /// <typeparam name="t">the type of object being copied.</typeparam>         /// <param name="source">the object instance copy.</param>         /// <returns>the copied object.</returns>         public static gameobject clone<gameobject>(gameobject source)         {              if (!typeof(gameobject).isserializable)             {                 throw new argumentexception("the type must serializable ", "source: " + source);             }              // don't serialize null object, return default object             /*if (object.referenceequals(source, null))             {                 return default(gameobject);             }*/              iformatter formatter = new binaryformatter();             stream stream = new memorystream();             using (stream)             {                 formatter.serialize(stream, source);                 stream.seek(0, seekorigin.begin);                 return (gameobject)formatter.deserialize(stream);             }         }     } 

i following error:

argumentexception: type must serializable parameter name: source: sp0 (unityengine.gameobject) objectcopier.clone[gameobject] (unityengine.gameobject source) (at assets/scripts/scenariomanager.cs:121)

the function posted above called here:

    void savescenario(){         foreach(gameobject obj in slemanager.listofsourcepoints){             tempobj = objectcopier.clone(obj);              listofscenariosourcepoints.add(tempobj);             debug.log("saved scenario source list point");         }         foreach(gameobject obj in slemanager.listofdestpoints){             tempobj = objectcopier.clone(obj);             listofscenariodestpoints.add(tempobj);             debug.log("saved scenario dest list point");         }     }      void loadscenario(){         slemanager.listofsourcepoints.clear();         slemanager.listofdestpoints.clear ();         foreach(gameobject obj in listofscenariosourcepoints){             tempobj = objectcopier.clone(obj);             slemanager.listofsourcepoints.add(tempobj);             debug.log("loaded scenario source list point");         }         foreach(gameobject obj in listofscenariodestpoints){             tempobj = objectcopier.clone(obj);             slemanager.listofdestpoints.add(tempobj);             debug.log("loaded scenario dest list point");         }     } 

now, original list created here:

if (child.name == "destinationpoints") {      parentdestinationpoints = child.gameobject;      foreach (transform grandchilddp in parentdestinationpoints.transform)      {           //debug.log("added dp object named: " + grandchilddp.name);           tempobj = grandchilddp.gameobject;           listofdestpoints.add(tempobj);           tempobj.addcomponent<destinationcontrol>();           tempobj.transform.renderer.material.color = color.white;      } }  // hide sourcepoints in scene if (child.name == "sourcepoints") {     parentsourcepoints = child.gameobject;     foreach (transform grandchildsp in parentsourcepoints.transform)     {          tempobj = grandchildsp.gameobject;          listofsourcepoints.add(tempobj);          tempobj.transform.renderer.enabled = false;     } } 

this "tempobj" has [serializefield] property must missing here. appreciated.

edit: forgot mention, app in unity3d.

i think need mark class gameobject attempting clone [serializable] attribute. make clone method generic not limited type:

/// <summary> /// creates deep clone of object using serialization. /// </summary> /// <typeparam name="t">the type cloned/copied.</typeparam> /// <param name="o">the object cloned.</param> public static t deepclone<t>(this t o) {     using (memorystream stream = new memorystream())     {         binaryformatter formatter = new binaryformatter();         formatter.serialize(stream, o);         stream.position = 0;         return (t)formatter.deserialize(stream);     } } 

i hope helps.


edit. perhaps deep copy without serialization using thing like

class {     // copy constructor     public a(a copy) {} }  // referenced class implementing  class b : ideepcopy {     object copy() { return new b(); } }  class c : ideepcopy {     a;     b b;     object copy()     {         c copy = new c();          // copy property property in appropriate way         copy.a = new a(this.a);         copy.b = this.b.copy();      } } 

i have found copyable uses reflection provide deep copies of objects. again, hope of use...


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -