Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Caestar · Aug 16, 2016 at 06:12 PM · scripting problemui

Can't seem to move a grouping of panels

I'm trying to create a grid of panels and found my problems are reversed: I was easily able to create a system that creates the grid within a defined space, but I can't seem to make it spawn in the space. Currently, no matter what variations I have tried (beyond hardcoding, which is something I'm trying to avoid), the value never hits anywhere near the field I'm trying to lay the grid on. Here's the function that generates everything:

 public void GeneratePanel()
     {
         cursorVector = DMGGrid.GetComponent<RectTransform>().anchorMin;
         int panelWidth = GetPanelSize(inFieldRec.width, columns);
         int panelHeight = GetPanelSize(inFieldRec.height, rows);
 
         for (int rowCount=1; rowCount <= rows; rowCount++)
         {
             for (int colCount=1; colCount <= columns; colCount++)
             {
                 dmgPanel = Instantiate(panelPrefab, cursorVector, Quaternion.identity) as GameObject;
                 
                 //Setting dmgPanelParams (i.e. creating the board)
                 dmgPanel.transform.SetParent(DMGGrid.transform); //set as parent for DMGCalcScript(LoadWeapon)
                 RectTransform newPanelRectT = dmgPanel.GetComponent<RectTransform>();
                 Rect newPanelRect = newPanelRectT.rect;
                 newPanelRect.width = panelWidth;
                 newPanelRect.height = panelHeight;
                 cursorVector.x += panelWidth;
             }
             cursorVector.x = DMGGrid.GetComponent<RectTransform>().anchorMin.x;
             cursorVector.y += panelHeight;
         }
     }

DMGGrid is a public gameobject/panel that the sort of backdrop for the input field. Any help would be greatly appreciated!

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

Answer by Caestar · Nov 11, 2016 at 03:56 PM

So, got this working and posting what I have for others who might have the same problem/thoughts, variable names changed so that it's clearer to general circumstances than just my application. And my comments because I don't want to edit those out

 public GameObject gridGO; //contains 2D Game Object to determine grid size & GO denoting "Game Object" in code
 public GameObject panelPrefabGO;
 
 private _panelGO;
 
 public void GeneratePanel()
     {
         ZeroCheck(); //ensure zero isn't used to divide by (i.e. 0 columns/0 rows)
         cursorVector = gridGO.GetComponent<RectTransform>().rect.min; //starting spawn point
         float panelWidth = GetPanelSize(columns);
         float panelHeight = GetPanelSize(rows);
         Vector2 tileSizeV = new Vector2(panelWidth, panelHeight);
         RectTransform newPanelRectT;
         float cursorYChange = 0f;
         for (int rowCount=1; rowCount <= rows; rowCount++)
         {
             for (int colCount=1; colCount <= columns; colCount++)
             {
                 _panelGO = Instantiate(panelPrefabGO, new Vector2(), Quaternion.identity) as GameObject; //standard instatiate
                 _panelGO.transform.SetParent(gridGO.transform); //set as parent for separate script
                 #region Setting attributes
                 newPanelRectT = _panelGO.GetComponent<RectTransform>();
                 newPanelRectT.localPosition = cursorVector; //move spawned tile to cursor position relative to parent
                 newPanelRectT.sizeDelta = tileSizeV; //Size of tiles
                 newPanelRectT.localScale = tileSizeV; //<- Needed to make the tiles lie edge to edge (kinda stumbled onto this one)
                 #endregion
                 cursorVector.x += newPanelRectT.rect.size.x*tileSizeV.x; //move cursor from left to right via tilesize
                 cursorYChange = newPanelRectT.rect.size.y*tileSizeV.y; //set the cursorYChange (probably a better place to do this so it doesn't keep resetting, not sure where though)
                 //The reason for the equations is that the tile spawns at it's center point, so this moves it over so edges seem to be touching
             }
             cursorVector.x = gridGO.GetComponent<RectTransform>().rect.min.x; //reset cursor to left side of field
             cursorVector.y += cursorYChange; //move by the cursorYchange factor
         }
     }
 
     private float GetPanelSize(float num) //num being columns/rows
     {
         float dimensionF = 1 / num; //Here caused me a lot of frustration, but what it boils down to is 1 is needed so it's the entire length of the side (using the actual measurements of the distance causes headaches vs using the side as it's whole, regardless of measurement)
         return dimensionF;
     }

Comment
Add comment · 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

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

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

Related Questions

i try make enemy health slider or any uI just show the health of Enemy Over head 0 Answers

Script doesn't let me disable a second panel and I cant find a way around this. 0 Answers

NullReferenceException on Text element 1 Answer

How Can i Get This bool to work? Just need someone willing to explain. 2 Answers

How to write a script for an existing button 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