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
2
Question by vexe · Sep 01, 2013 at 03:59 PM · gameobjecttransformpositionchildrenngui

How to move a gameObject without affecting its childrens' positions?

Hello, I'm working on a File manager-like structure with NGUI, so far so good. The way I got my "Folder" set up, is that a folder consists of "Parts" which consists of a Background, Icon and a Label, "Contents" which carry the folder's content, a "FolderContent" could either be a "Folder" or "FILE".

Whenever I "Open" a folder, (in short) I hide the previous folder contents and show the new ones'.

The problem is, when I pickup a folder and move it with the mouse (drag and drop or just pick and hold) I auto-organize the current folder's contents (reposition them), when a folder gets repositioned (n shifts to the left/up), its contents also shift with it, so when I open the folder later, I will see that the contents' are not positioned correctly.

I can get around that by using one of my methods "OrganizeContents" which does exactly what you think, but that wouldn't be necessary to do each time I open a folder, it's redundant.

Have a look:

alt text

Any ideas how to move a gameObject without affecting its children?

Another work-around would be to move the Folder's "Parts" when auto-positioning it, but that would bring inconsistencies in other places.

If I can't do what I'm asking for, any other work-arounds beside the two I mentioned?

Thanks.

folder.png (110.1 kB)
Comment
Add comment · Show 7
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
avatar image vexe · Sep 01, 2013 at 04:22 PM 0
Share

Actually, each folder has a List of "FolderContent"s, if that's what you mean. It just makes sense to put the contents of a folder, inside the folder, doesn't it? - It has nothing to do with NGUI, just common sense.

avatar image ArkaneX · Sep 01, 2013 at 05:14 PM 1
Share

Of course that folder has some content, but this does not mean you have to create it using Unity objects hierarchy. If you gain nothing from it, and additionally it causes problems, then why even bother?

In my approach, you still have proper hierarchy. I'll try to describe it in more detailed way.

Let's assume every folder/file is represented by a Plane object for which you have created a prefab. You also have to create a FileSystemObject script and attach it to this prefab. Basically, FileSystemObject should look like (C#):

 public class FileSystemObject
 {
     public FileSystemObject[] children;
     public FileSystemObject[] parent;
 
     public ObjectType type; // this can be an enum {File, Directory}
 }

When browsing your data you can dynamically create a number of Plane instances, get their FileSystemObject components and sets their properties. And you have the hierarchy you wished for, but don't have the problems related to position.

Ins$$anonymous$$d of dynamic creation of Planes, it would be much better to reuse some instances created at the start of your application.

avatar image ArkaneX · Sep 01, 2013 at 05:21 PM 1
Share

As to navigation: when you click on my Plane (or in your case object visually representing the folder), you can retrieve its script and properly instantiate children:

 var fso = GetComponent();
 foreach(var child in fso.children)
 {
     // instantiate children
 }

I think it should be quite easy to convert your current solution to this approach.

avatar image vexe · Sep 01, 2013 at 05:42 PM 1
Share

why the array of parents? a folder content has only one parent. storing a ref to the parent was my previous setup, but then I figured I don't need it. Is there a reason for the parents array?

avatar image vexe · Sep 01, 2013 at 05:50 PM 1
Share

O$$anonymous$$ thanks I got your point, basically keep everything the same, but don't mess with the game objects' hierarchy, no parenting. gonna have to go now, I'll do it. could you move your comments to an answer? I will accept it once everything's ok.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by ArkaneX · Sep 01, 2013 at 04:15 PM

What is the reason behind storing subfolders and files as children of the folder? I know nothing about NGUI, but if I were to code it in pure Unity, I would probably store subfolders and files as an arrays of GameObjects[] in parent folder script, not as standard GameObject children. If required, I would add another property for keeping reference to parent (null for top level folder):

 public GameObject[] folders;
 public GameObject[] files;
 
 public GameObject parentFolder;

This way, repositioning parent would not affect children position. You can of course change GameObject in above snippet with proper objects.

EDIT: converted to answer after exchanging a few comments with OP (under question).

Comment
Add comment · Show 2 · Share
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
avatar image vexe · Sep 02, 2013 at 02:34 AM 1
Share

That did it just nice! A lot of code has been nuked on the way :) Easy conversion as well like you said.

However I think it's more elegant to use OOP here and not simply an enum to represent a FileSystemObject. There's a lot of benefits to that, for example, a folder doesn't need to have an array of files and folders, just an array (preferably a list) of FileSystemObjects, which could be either a File or a Folder.

Again, no need for the parentFolder, in case you're putting it so that each folder knows its parent when you "GoBack", you could just use a stack, each time you open a folder you just push, when you go back you pop, the parent would be left out as extra luggage in the folder class. Thanks a lot for your help :)

avatar image ArkaneX · Sep 02, 2013 at 07:27 AM 1
Share

You're right regarding OOP - my solution was just a quick 'how to' example. Glad it was helpful :)

avatar image
1

Answer by DESTRUKTORR · Sep 01, 2013 at 04:17 PM

The best method for moving the parent object without moving the child objects would be to save the child object's absolute position (transform.position) prior to moving the parent, then, after calling the move on the parent, set the child's absolute position back to what it was before the move.

Comment
Add comment · Show 4 · Share
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
avatar image vexe · Sep 01, 2013 at 04:24 PM 1
Share

This does sound quite like repositioning them once again, but performance-wise, it's better. 2 n loops, an assignment in each loop, saves some math/calculations. I'll benchmark it with the auto organizing method, if there's a huge dif, I'll choose your solution :)

avatar image vexe · Sep 01, 2013 at 04:36 PM 1
Share

I'm afraid it's not gonna work in my situation. Thinking about it, it is a good solution but not for me because what would happen if there ware more folders to the right/under 'Vids'? (in the previous example), using your method, I would have to save the contents positions of all those folders and then re-assign them again... :/

avatar image DESTRUKTORR · Sep 01, 2013 at 04:41 PM 1
Share

Why do it in the loop? The user will only notice that their position is changed after the next frame is loaded. Just ensure that it's moved back to its original position after everything else has moved XD. One way or another, it's only a reassignment of a Vector3 variable, which is little more than 3 floats (12 bytes, which, in the grand scheme of things, is really not much, considering we are beginning to see megabytes, or millions of bytes, as being laughably small amounts of data), and it's an O(n) efficiency, as the only level of variation is the first layer of children.

However, like ArkaneX said, it might just be easier if you simply didn't have the objects parented to one another, lol. The primary purpose, by and large, of making a game object a child of another is to ensure that they move, together, and sometimes to allow scripts to more easily access components of their children/parents (or, on occasion, to organize the hierarchy a bit better, but that's really not all that usual, nor should it take precedence over efficiency, I$$anonymous$$O).

avatar image vexe · Sep 02, 2013 at 02:36 AM 1
Share

Thanks for clearing out when to make objects childs of other objects. I obviously mis-used parenting there.

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

18 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 avatar image

Related Questions

I want to move a cube with rotation but I find this problem 1 Answer

transform.position error 1 Answer

Why is my variable updating when it shouldn't? 1 Answer

How to assign x and y transform values of an gameobject to user defined variables? 1 Answer

How do you tranform multiple objects 1 Answer


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