Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by paranoidmarv · Nov 15, 2013 at 06:46 AM · buttondraghierarchyngui

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:

alt text

And here is one of what the problem looks like:

alt text

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.

screen shot 2013-11-14 at 11.02.19 pm.png (35.3 kB)
screen shot 2013-11-14 at 9.36.25 pm.png (19.5 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges