I have 2 GameObject and each other has a lot of child. This two GameObject same as same except some child's exist or child's position. I found each child's name. And also I found locations info for each item. Then I compared this Lists and i found relocated, exist, added and removed items. I want to match with relocated items with names. How can I do that?
On Project1
List<string> existItems = GetNames().Intersect(revisedBasicProject4.GetNames4()).ToList();
List<Vector3> comparePosition = GetLocations().Except(revisedBasicProject4.GetLocations()).ToList();
foreach (var item in existItems)
{
isExist = true;
}
if (isExist)
{
foreach (var item in comparePosition)
{
Debug.Log("RELOCATED ITEMS :" item);
}
}
CodePudding user response:
I do not know if I understood what you meant correctly or not, but you can use the following code
List<string> existItems = GetNames().Intersect(revisedBasicProject4.GetNames4()).ToList();
List<Vector3> comparePosition = GetLocations().Except(revisedBasicProject4.GetLocations()).ToList();
foreach (var item in existItems)
foreach (var li in comparePosition)
if (item.(...) == li.(...)) {
Debug.Log("RELOCATED ITEMS :" item);
}
CodePudding user response:
Following on from https://stackoverflow.com/users/7111561/derhugo and https://stackoverflow.com/users/13922490/mohammad-asadi - You want to store names and locations together, perhaps in a simple struct:
public struct NamedPosition
{
string Name;
Vector3 Position;
}
Then, you can compare your revisedBasicProject4 positions/names with the current class like so:
foreach(var namedPosition in GetNamedPositions())
{
foreach(var revisedBasicProject4NamedPosition in revisedBasicProject4.GetNamedPositions())
{
if ( namedPosition.Name == revisedBasicProject4.Name
&& namedPosition.Position != revisedBasicProject4.Position)
{
Debug.Log("RELOCATED ITEM : " namedPosition.Name);
}
}
}
If you find this is taking forever (because you have millions of NamedPositions, you may be able to optimise this by doing the following:
var namedPositions = new List<NamedPosition>(GetNamedPositions());
var otherNamedPositions = new List<NamedPosition>(revisedBasicProject4.GetNamedPositions());
for(var i = 0; i < namedPositions.Count; i )
{
for(var j = 0; j < otherNamedPositions.Count; j )
{
if ( namedPositions[i].Name == otherNamedPositions[j].Name )
{
if ( namedPositions[i].Position != otherNamedPositions[j].Position )
Debug.Log("RELOCATED ITEM : " namedPosition.Name);
otherNamedPositions.RemoveAt(j); //We found it so remove it from our temporary "otherNamedPositions" list
break; //We found it so move on to the next "namedPositions" item
}
}
}
