- Home /
Trying to Recreate Transform Child Heirarchy to a List
Hi,
I have spent most of the day trying to work out how to 'copy' the structure/hierarchy of a transform and all it's children, grandchildren, etc, but to a different class (structure example below).
I have tried recursion but can't maintain the order. A 'flat' list for example is easy to get but I just can't get it how I need. Here is an example of the simple class structure I am experimenting with:
[Serializable]
public class BasicCopy
{
public string Name;
public List<Transform> Transforms;
}
public List<BasicCopy> Test;
public Transform Target;
void Start()
{
// Iterate through Target and ALL it's children and generate a matching hierarchy/structure using the BasicCopy class
}
The whole process keeps tying me in knots, and is now driving me insane lol! Any help will be much appreciated.
EDIT: I may have worded this badly. For clarity, I am looking to specify my 'Target' transform then recursively find all child transforms, as I find them, they are added to my BasicCopy class, maintaining the same hierarchy/structure as the children.
How about giving each object a unique ID and then just keeping track of three things:
the GameObject
It's ID
It's parent's ID, if any.
Hi Cherno,
Thanks, but this does not provide me with the matching structure, in the exact way specified above.
As mentioned, I can already generate a 'flat' version of the data. I need the parent / child/ grandchild structure to exactly match that of the target transform.
It requires processing, but this DOES in-fact provide you with the structure information. Additionally, all those things Cherno listed can be stored in a flat array, and thus, always be serialized properly. You can create and implement a serialize() and deserialize() pair of functions to convert from the flat array (for serialized storage), to a tree structure (for run-time usage).
Answer by rober-psious · May 03, 2016 at 08:55 AM
Here you have an example:
[SerializeField]
public class BasicCopy
{
public string Name;
public List<Transform> Transforms;
}
public List<BasicCopy> Test;
public Transform Target;
private void Start()
{
Test = new List<BasicCopy>();
Test.Add(GetBasicCopy(Target));
}
private BasicCopy GetBasicCopy(Transform currentTarget)
{
BasicCopy result = new BasicCopy();
result.Name = currentTarget.name;
result.Transforms = new List<Transform>();
Transform[] childs = currentTarget.GetComponentsInChildren<Transform>();
foreach (Transform current in childs)
if (current.parent != null && current.parent.GetInstanceID() == currentTarget.GetInstanceID())
{
result.Transforms.Add(current);
Test.Add(GetBasicCopy(current));
}
return result;
}
Hi,
That has really helped me get started. Unfortunately my example class was wrong as it would only go so far, and would only get the first level. However after your help I am getting close (Thanks!).
So this code I have worked on (I have modified the class) gets me the first children of the transform, but fails to get the rest.
Here is where I am currently at:
[Serializable]
public class BasicCopy
{
public string Name;
public Transform $$anonymous$$odel;
public List<BasicCopy> Children;
}
public Transform Target;
public List<BasicCopy> Test;
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
private void Start()
{
Test = new List<BasicCopy>();
Test.Add(GetBasicCopy(Target));
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
private BasicCopy GetBasicCopy(Transform currentTarget)
{
BasicCopy result = new BasicCopy();
result.Name = currentTarget.name;
result.Children = new List<BasicCopy>();
Transform[] childs = currentTarget.GetComponentsInChildren<Transform>();
List<BasicCopy> convertedChilds = new List<BasicCopy>();
foreach (Transform t in childs)
{
BasicCopy newChild = new BasicCopy();
newChild.$$anonymous$$odel = t;
newChild.Name = t.name;
convertedChilds.Add(newChild);
}
foreach (BasicCopy current in convertedChilds)
{
if (current.$$anonymous$$odel.parent != null && current.$$anonymous$$odel.parent.GetInstanceID() == currentTarget.GetInstanceID())
{
result.Children.Add(current);
}
}
return result;
}
Any ideas where I am going wrong? It feels like I am really close. Thanks again for the help so far! Here is an example of the result so far:
P.S. I know my new class will generate a "Serialization depth limit exceeded at 'BasicCopy'. There may be an object composition cycle in one or more of your serialized classes." error. I am serializing it just to get quick access to results. It compiles fine.
You are using serialization system which has an unity problem. This http://answers.unity3d.com/answers/577519/view.html can help you, but I don't believe you need this.
This gave me enough in the end, and I learnt lots from this. $$anonymous$$arking as answered. Thank you!
Answer by meat5000 · May 03, 2016 at 08:39 AM
Try using GetChild and incrementing the index
http://docs.unity3d.com/ScriptReference/Transform.GetChild.html