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 $$anonymous$$ · Jul 01, 2014 at 04:31 PM · guipositionupdateglitchstart

Script variables resetting on play

I'm trying to find out why my variables are being set to the minimum possible size when I hit play, and how to fix this problem

I'm working with the Unity GUI and the OnGUI function and I've setup a GroupGUI class which is able to contain any number and any kind of general GUI item, by being able to contain one GUIContent variable, a couple of Rect for relative positioning, and an array of GroupGUI.

When I hit play to view my GUI (The master GroupGUI is attached to a camera and contains 2 children attached to junk) the children get re-sized to 12px by 12px... This is what I have hard coded as being the smallest possible width and height for the objects but I can't find why they're being re-sized to this because their default size set in the inspector is much larger (although smaller than their parent's limit).

The way this system works is that you can attach a master GUI item to the camera (could be empty, or a border for a menu, or just the main item if only one item needed), then all the children are limited to be within that item's area, and the position of the children is relative to the master, rather than relative to the screen. If a master/parent item moves then all it's children and their children move along with it.

This may seem pointless or others may have done it but I'm new to making things in Unity and thought that this "advanced" menu API could be a good way to practice with a couple of things.

Their aren't any unused values or undefined functions, I've just removed anything that doesn't alter the behavior of the item positioning. Thanks in advance for any help

The code

 public class GroupGUI: MonoBehaviour{
     public bool isActive; //Not important for this question
     public GUITYPES guiType; //Not important for this question
     public bool CenterPosOnX;
     public bool posCenterOffset;
     public bool forceUpdate;
     public Rect position;
     public GUIContent content;
     public GroupGUI[] subs;
 
     private string textEdit; //Not important for this question
     private GroupGUI parent;
     private Rect realPos;
     private Rect prevPos;
     private Rect positionLimit;
 
     void Start(){
         forceUpdate = true;
         textEdit = content.text;
         prevPos = position;
 
         if(parent){
             parent.correctAlignment();
             positionLimit = parent.position;
         }
         else{
             positionLimit = new Rect(0, 0, Screen.width, Screen.height);
         }
         for(int i=0; i<subs.Length; i++){
             if(subs[i] == this)
                 subs[i] = null;
             else{
                 subs[i].positionLimit = realPos;
                 subs[i].parent = this;
             }
         }
     }
 
     void Update(){
         if(forceUpdate || prevPos.width != position.width || prevPos.height != position.height || prevPos.x != position.x || prevPos.y != position.y){
             for(int i=0; i<subs.Length; i++) {
                 if(subs[i].parent != this) {
                     subs[i].parent = this;
                 }
             }
             
             forceCorrectAlignment();
             forceUpdate = false;
             
             if(posCenterOffset) {
                 if(!CenterPosOnX)
                     realPos.x = (positionLimit.x) + (positionLimit.width / 2) - (realPos.width / 2) + position.x;
                 realPos.y = (positionLimit.y) + (positionLimit.height / 2) - (realPos.height / 2) + position.y;
             }
             else {
                 realPos.x = (positionLimit.x) + position.x;
                 realPos.y = (positionLimit.y) + position.y;
             }
             if(CenterPosOnX) {
                 realPos.x = positionLimit.x + (positionLimit.width / 2)  - (realPos.width / 2);
                 position.x = (posCenterOffset) ? 0 : positionLimit.width/2;
             }
 
             prevPos = position;
         }
     }
 
     void OnGUI() {
         if(!isActive)
             return;
 
         switch(guiType){
             case GUITYPES.Empty:
                 break;
             case GUITYPES.Box:
                 GUI.Box(realPos, content);
                 break;
             case GUITYPES.Button:
                 if(GUI.Button(realPos, content)){
                     GUI_ImHit();
                 }
                 break;
             case GUITYPES.RptButton:
                 if(GUI.RepeatButton(realPos, content)){
                     GUI_ImHit();
                 }
                 break;
             case GUITYPES.Label:
                 GUI.Label(realPos, content);
                 break;
             case GUITYPES.Password:
                 GUI.PasswordField(realPos, content.text, '*');
                 break;
             case GUITYPES.TextArea:
                 textEdit = GUI.TextArea(realPos, content.text);
                 if(textEdit != content.text){
                     GUI_ImHit();
                 }
                 break;
             case GUITYPES.TextField:
                 textEdit = GUI.TextField(realPos, content.text);
                 if(textEdit != content.text){
                     GUI_ImHit();
                 }
                 break;
         }
     }
 
     private void forceCorrectAlignment(){
         correctAlignment();
         for(int i=0; i<subs.Length; i++){
             subs[i].positionLimit = realPos;
             subs[i].forceCorrectAlignment();
         }
     }
     private void correctAlignment(){
         correctAlignment(this, positionLimit);
     }
     private static void correctAlignment(GroupGUI arg, Rect limit){
         correctAlignment(arg, limit.width, limit.height);
     }
     private static void correctAlignment(GroupGUI arg, float w, float h){
         if(arg.position.width < 12)
             arg.position.width = 12;
         if(arg.position.height < 12)
             arg.position.height = 12;
         if(arg.position.width > w)
             arg.position.width = w;
         if(arg.position.height > h)
             arg.position.height = h;
 
         if(arg.posCenterOffset){
             if(arg.position.x < (arg.position.width / 2) - (w / 2))
                 arg.position.x = (arg.position.width / 2) - (w / 2);
             if(arg.position.x + (arg.position.width / 2) > w / 2)
                 arg.position.x = (w / 2) - (arg.position.width / 2);
             
             if(arg.position.y < (arg.position.height / 2) - (h / 2))
                 arg.position.y = (arg.position.height / 2) - (h / 2);
             if(arg.position.y + (arg.position.height / 2) > h / 2)
                 arg.position.y = (h / 2) - (arg.position.height / 2);
         }
         else{
             if(arg.position.x < 0)
                 arg.position.x = 0;
             if(arg.position.x + arg.position.width > w)
                 arg.position.x = w - arg.position.width;
         
             if(arg.position.y < 0)
                 arg.position.y = 0;
             if(arg.position.y + arg.position.height > h)
                 arg.position.y = h - arg.position.height;
         }
 
         arg.realPos.width = arg.position.width;
         arg.realPos.height = arg.position.height;
     }
 }


The variables

 Master:
   isActive: true
   GuiType: Box
   CenterPosOnX: false
   PosCenterOffset: true
   Position:
     X: 0, H: 0, W: 100, H: 100
   Content:
     Text: Menu
     Image: (None)
     Tooltip: (None)
   Subs:
     Size: 2
     Element 0: Child0
     Element 1: Child1

 Child0:
   isActive: true
   GuiType: Button
   CenterPosOnX: false
   PosCenterOffset: true
   Position:
     X: 0, H: 25, W: 75, H: 25
   Content:
     Text: Hello
     Image: (None)
     Tooltip: (None)
   Subs:
     Size: 0

 Child1:
   isActive: true
   GuiType: Button
   CenterPosOnX: false
   PosCenterOffset: true
   Position:
     X: 0, H: 60, W: 75, H: 25
   Content:
     Text: Hello2
     Image: (None)
     Tooltip: (None)
   Subs:
     Size: 0

More Info

When play is hit the master box appears in center of screen at correct size as it should but the 2 children are at smallest possible size, and are positioned with an offset relative to the top left corner of the screen that is equal to half of the width and height of the master item. The child items will jump into the master item once the master item has been updated (moved/re-sized) and THEN the child is updated.

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

1 Reply

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

Answer by Bunny83 · Jul 05, 2014 at 01:33 PM

There are some things that are prone to fail or simply strange:

  • "Start" of each object can be called in any order, same for "Update". So it isn't guaranteed that for example Start of the parent object is called before the child(s).

  • It seems that each child will call "parent.correctAlignment();" on the parent. That doesn't make much sense.

  • Are you sure your "positionLimit" is always correct? Especially when your objects are initialized in a random order?

  • If you want a hierarchical relationship, why don't you use the Transform component for that? You get the parenting for free.

  • Those kind of class-based GUI systems are usually better done with a single OnGUI method on the topmost object. You would simply call your own "GUI" callback on the childs from the "master". That way you have full control over the execution order.

However if it isn't "that" urgent it's worth waiting for Unity 4.6 and the new GUI system

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 $$anonymous$$ · Jul 14, 2014 at 11:36 AM 0
Share

I was able to fix the problem before your reply but I did so by making all instances not actually use OnGUI until 5 Update ticks have passed, this ensures that the parent sets all it's children to have the parent value, and updates are forced on all objects and the limits are in place correctly. I didn't actually work out how many ticks are needed though and just used trial and error, I fear that the deeper the child list gets then it'll take longer to do this. Anyways as I said this was more just for getting into Unity3D and C#, thanks for the help.

avatar image $$anonymous$$ · Jul 14, 2014 at 01:37 PM 0
Share

I was able to fix the problem before your reply but I did so by making all instances not actually use OnGUI until 5 Update ticks have passed, this ensures that the parent sets all it's children to have the parent value, and updates are forced on all objects and the limits are in place correctly. I didn't actually work out how many ticks are needed though and just used trial and error, I fear that the deeper the child list gets then it'll take longer to do this. Anyways as I said this was more just for getting into Unity3D and C#, thanks for the help.

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

2 People are following this question.

avatar image avatar image

Related Questions

GUI position not updated on build & run 0 Answers

Saving the starting position of a Lerp during Update 2 Answers

Failed to start unity package manager. Version 2017.2 1 Answer

Why is OnGUI not working? 1 Answer

Changing an objects transform position 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