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 CaptainBeeftits · Oct 22, 2016 at 01:28 PM · guidynamicresize

Trying to get Canvas child Panel to resize.

Basically what i am trying to do is write a script that can resize Ui element that are child of the canvas dynamically so the user can have control over the size and placement of them. Placement and moving them was easy, resizing them based on input how ever is giving me fits lol. I clearly am either not understanding how anchors work or i am just not taking the proper approach.

I've read the doc's and tried a bunch of different tutorials and asset store scripts and i still couldn't find a "simple" working solution. Tyvm in advance for your time i do greatly appreciate it and if i could have slogged it out on my own i would have.

 using UnityEngine;
 using System.Collections;
 
 public class ResizeElements : MonoBehaviour {
 
     public RectTransform elementSize; //Assigning this to my canvas child panel i want to resize from the editor.
     public float sizingFactor = 0.02f;
     private float startSizeX;
     private float startSizeY;
     private float startX;
     private float startY;
 
     void Update()
     {
         if (Input.GetMouseButtonDown(0))
         {
             /*Here it is resizing my panel too 0 or 1 based off of my input not working the way i intended at all
             but i cant quit figure out how to make it do what i want it to do.
             */
             Vector2 position = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
             startX = position.x;
             startY = position.y;
             Debug.Log("Mouse position X = " + startX);
             Debug.Log("Mouse position Y = " + startY);
         }
 
         if (Input.GetMouseButton(0))
         {
             Vector2 size = new Vector2(elementSize.sizeDelta.x, elementSize.sizeDelta.y);
             size.x = startSizeX + (Input.mousePosition.x - startX) * sizingFactor;
             size.y = startSizeY + (Input.mousePosition.y - startY) * sizingFactor;
             elementSize.sizeDelta = size;
         }
     }
 }
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 CaptainBeeftits · Oct 24, 2016 at 12:51 PM

Back again i got things working in a predictable way need to add functionality and limits but i find seeing a basic script that does something is easier to understand than seeing a monster script. 1. Create a parent canvas, then create a child panel. 2. Put your resize script on either child or parent i usually put it on the Canvas myself. 3. Assign your child via the inspector as the RectTransfrom.

 using UnityEngine;
 using System.Collections;
 
 public class ResizeElements : MonoBehaviour {
 
     public RectTransform elementSize; //Assigning this to my canvas child panel i want to resize from the inspector.
 
     public float sizingFactor = 0.1f; //How much size we will gain
     public float sizingMagnitude = 100f; //Eventually want to set this via somekind of input Ie: Mouse, drag handles or what ever.
 
     void Update()
     {
         //Grows our Panel Child when mouse is clicked currently just an example will be adding functionality.
         if (Input.GetMouseButton(0))
         {
             Vector2 size = new Vector2(elementSize.sizeDelta.x, elementSize.sizeDelta.y);
             Debug.Log("size = " + size);
             size.x += sizingFactor * sizingMagnitude;
             Debug.Log("sizingFactor = " + sizingFactor);
             size.y += sizingFactor * sizingMagnitude;
             elementSize.sizeDelta = size;
         }
     }
 }
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

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

Related Questions

I cant see border on dynamically created button 0 Answers

How can I resize an image with drag-and-drop? 0 Answers

Dynamic Menu creation 2 Answers

Resizing objects relative to screen and other objects 0 Answers

How can i make a button appears when an inputfield is filled? 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