- Home /
NGUI widget sprite background no longer moves with its button
I've been going crazy trying to solve this and decided to see if anyone had experienced the same thing. It's complicated to explain so please bear with me and ask me to clarify if necessary.
I'm working a ship building game consisting of two types of parts: Hulls and Mechanical Parts. Hulls are self explanatory and Mechanical Parts are any parts with an active function (e.g. rockets, laser cannons, etc). A Hull is always the child of a parent GameObject called Ship. When a Mechanical Part is dragged onto a Hull or vice versa, the Mechanical Part becomes a child of the Hull and grand child of the Ship.
To combine parts I'm using free NGUI to create widget buttons that act as Attach Points for both types of parts. You select an Attach Point and you drag it to another Attach Point in order to attach parts together. This is doing using the UIDragObject script. This works perfectly for simple combinations such as when combining a Mechanical Part to a Hull or a Hull to a Hull. When this is done, the UIDragObject script's target is set to the Ship for every child (Hulls and Mechanical Parts) as well as their children (Attach Points) making it possible to drag the combined object from either a part or an Attach Point. Furthermore, a complex combination (i.e. attaching a Hull with mechanical parts to another Hull with or without mechanical parts) works. The problem arises only when detaching one Hull from another in one of these complex combinations.
I'll explain how a detach works and post some code. When I detach one complex Hull from another Hull (complex or not), I instantiate a new empty Ship and move the Hull and it's children (this includes Attach Points and Mechanical Parts) to this new Ship. I then set the UIDragObject script targets to this new Ship so that they can all be dragged together. The drag works fine in this new Ship. I can drag the entire Ship from any of the Hulls or Mechanical parts as well as from any of the Attach Points.
The problem is that after doing this, the backgrounds on the Attach Points (they are NGUI widgets) no longer follow the rest of the objects.
Their colliders remain in place, meaning everything still functions as it should, however, the backgrounds don't move anywhere unless I hover over it's sphere collider. Here's some code:
void detach(){
//Can only detach if the Attach Point has a target Attach Point to which it is attached.
if(this.target != null){
//AP is currently selected attach point. AP parent is the currently selected attach point's parent.
//Target is the AP's targeted attach point.
GameObject apParent = this.transform.parent.gameObject;
GameObject targetParent = this.target.transform.parent.gameObject;
//detaching mechanical part from part
if(apParent.CompareTag("Mechanical Part") && targetParent.CompareTag("Part")){
//Sets the detached mech part's parent to the Ship's parent (putting it outside of ship's hierarchy).
apParent.transform.parent = targetParent.transform.parent.transform.parent;
//Sets the detached mech part's drag target to itself
apParent.GetComponent<UIDragObject>().target = apParent.transform;
//Cycle through the detached mech part's APs to set their drag targets to the detached mech part.
for(int i = 0; i < apParent.transform.childCount; i++){
apParent.transform.GetChild(i).GetComponent<UIDragObject>().target = apParent.transform;
}
}
//detaching part from part (even if either is attached to more parts or mech parts
else if(apParent.CompareTag("Part") && targetParent.CompareTag("Part")){
//Create a new empty ship and set it's parent to the old ship's parent.
GameObject newShip = (GameObject)Instantiate (Resources.Load ("Empty Ship"));
newShip.transform.parent = apParent.transform.parent.transform.parent;
//Set detached part's parent to the new ship and set its drag target to the new ship
apParent.transform.parent = newShip.transform;
apParent.GetComponent<UIDragObject>().target = newShip.transform;
//Cycle through the detached part's children setting their drag target to the new ship
for(int i = apParent.transform.childCount - 1; i >= 0; i--){
//If the child is a mechanical part
if(apParent.transform.GetChild(i).CompareTag("Mechanical Part")){
//Then cycle through it's children (APs) and set their drag targets to the new ship.
for(int j = 0; j < apParent.transform.GetChild(i).transform.childCount; j++){
apParent.transform.GetChild (i).transform.GetChild(j).GetComponent<UIDragObject>().target = newShip.transform;
}
}
apParent.transform.GetChild (i).GetComponent<UIDragObject>().target = newShip.transform;
}
}
}
setIsAttached(false);
}
And here is an image of the hierarchy:
And here is one of what the problem looks like:
The dark grey objects are Hulls, the light grey objects are Rockets, and the blue dots are Attach Points. After detaching the hull, the blue dotes (which are the backgrounds on the Attach Point widgets) no longer drag with the rest of the Ship.
Your answer
Follow this Question
Related Questions
[NGUI] How can I drag item from UIlist and insert to new position 0 Answers
GUI button to NGUI button ? 1 Answer
how to set a public GameObject when button clicked 2 Answers
nGUI how to deactivate UICheckbox by script? 1 Answer
Toggle/Button is Clickable Behind Image unless Image has no Parent? 0 Answers