I’m in the process of writing a script that will merge scenes with the option of removing duplicate GameObjects. The idea is to open a scene with EditorApplication.OpenSceneAdditive, and then check if any newly loaded GameObjects share the same name as other GameObjects in the scene.
How can we determine the list of GameObjects that have been loaded with OpenSceneAdditive? Perhaps there is some method we can call in OnHierarchyChange?
Edit: The approach I’ve decided to take is to get all GameObjects with GameObject.FindObjectsOfType(typeof(GameObject)); before and after the merge, and then comparing them to find the newly added GameObjects.
You could use this code to do that:
#pragma strict // remove all objects with duplicate names on additive loading of level
private var nameList: List.<String>;
function OnLevelWasLoaded () {
var allGobj: GameObject[] = GameObject.FindObjectsOfType(GameObject);
nameList = new List.<String>();
for(var gameObj : GameObject in allGobj){
if(nameList.Contains(gameObj.name)){
Destroy(gameObj);}else{
nameList.Add(gameObj.name);
}
}
}
you could try this… example is for all objects in a parent, delete ones with same position, rotation… could be all GO’s in a scene etc:
function roughly(a : float, b : float):boolean
{
//print ("float " + (Mathf.Abs(a - b) < 1));
return Mathf.Abs(a - b) < 1;
}
function roughlyV3(a : Vector3, b : Vector3):boolean
{
//print (" v3 "+ ((Mathf.Abs(a.x - b.x) + Mathf.Abs(a.y - b.y) + Mathf.Abs(a.z - b.z) ) < 0.052));
return (Mathf.Abs(a.x - b.x) + Mathf.Abs(a.y - b.y) + Mathf.Abs(a.z - b.z) ) < 0.052;
}
function same2 (){
var childs = parent.GetComponentsInChildren(Transform);
for (var ws = 0; ws<childs.length; ws++) {
for (var wk = 0; wk<childs.length; wk++) {
if ( roughlyV3( childs[ws].transform.position , childs[wk].transform.position )
&& roughly(childs[ws].transform.eulerAngles.z % 179.99 , childs[wk].transform.eulerAngles.z % 179.99 )
&& childs[ws].gameObject !=childs[wk].gameObject
&& ws > wk) // last line to make sure only one of the two was deleted, because, in the loop, object deleted isnt null till next frame.
{ print( " detected " ); Destroy( childs[ws].gameObject ); }
}
}
}