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 generalhak · May 09, 2014 at 04:56 PM · resolutionguitexturerectresize

gui texture crop and resize

hi i use guitexture and i want crop that but i want that be good for any resolution and my gui resizer script is this: (1) and my crop script is this : (2) how can i combine this two wich my gui texture resize to any size and crop every update (like health bar) ..... scripts : (1) :

 public class GUITextureResizer : MonoBehaviour 
 {
     private GUITexture _guiTextureRef;
     public const float BASE_WIDTH = 800;
     public const float BASE_HEIGHT = 600;
     private float _baseHeightInverted;
     private float _originalPixelInsetWidth;
     private float _originalPixelInsetHeight;
     
     void Awake()
     {
         _guiTextureRef = GetComponent<GUITexture>();
     }
     
     void Start() 
     {
         _baseHeightInverted = 1/BASE_HEIGHT;
         
         _originalPixelInsetWidth = _guiTextureRef.pixelInset.width;
         
         _originalPixelInsetHeight = _guiTextureRef.pixelInset.height;
     }
     
     void FixedUpdate()
     {   
         float ratio = Screen.height * _baseHeightInverted;
 
         _guiTextureRef.pixelInset = new Rect(0, 0, _originalPixelInsetWidth * ratio, _originalPixelInsetHeight * ratio);
     }
 
 }


............... script (2) :

 public class GUITexturecrop : MonoBehaviour {
 
     
     private GameObject tire;
     private GUITexture texture;
     //public Rect textureCrop = new Rect( 0f, 0f, 0f, 0f );
     private float textureCrop_x = 0f;
     private float textureCrop_y = 0f;
     private float textureCrop_width = 0f;
     private float textureCrop_height = 1f;
     
     public Vector2 position = new Vector2( 0, 0 );
     
     // Use this for initialization
     void Start () {
         texture = gameObject.GetComponent<GUITexture>();
         
     }
     
     // Update is called once per frame
     void Update()
     {
         textureCrop_width = /// my change ///  ;
     }
     
     
     
     void OnGUI () {
         GUI.BeginGroup( new Rect( position.x, position.y, texture.pixelInset.width * textureCrop_width, texture.pixelInset.height * textureCrop_height ) );
         GUI.DrawTexture( new Rect( -texture.pixelInset.width * textureCrop_x, -texture.pixelInset.height * textureCrop_y, texture.pixelInset.width, texture.pixelInset.height ), texture.texture );
         GUI.EndGroup();
         
     }
     
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
1
Best Answer

Answer by generalhak · May 09, 2014 at 06:44 PM

i solved myself ! i just needed to this script ! : with this script we can crop guitexture which will resize with aspect ratio

 using UnityEngine;
 using System.Collections;
 
 public class GUIcropengain : MonoBehaviour {
     
     public Texture mytexture;
 
     private float textureCrop_width = 0f;
     private float textureCrop_height = 1f;
     public Vector2 position = new Vector2( 0, 0 );
 
     //resize
     public const float BASE_WIDTH = 800;
     public const float BASE_HEIGHT = 600;
     private float _baseHeightInverted;
     private float ratio = 0f;
 
     // Use this for initialization
     void Start () {
 
         _baseHeightInverted = 1/BASE_HEIGHT;
     }
     
     // Update is called once per frame
     void Update()
     {
         textureCrop_width =  10 ;//// change width of gui 
 
         ratio = Screen.height * _baseHeightInverted;
     }
 
     void OnGUI () {
         GUI.BeginGroup( new Rect( position.x * ratio, position.y * ratio , mytexture.width * textureCrop_width * ratio, mytexture.height * textureCrop_height * ratio ) );
         GUI.DrawTexture( new Rect( 0 , 0 , mytexture.width * ratio, mytexture.height * ratio ), mytexture );
         GUI.EndGroup();
     }
 }
 
Comment
Add comment · Show 1 · 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 CHRISYS · Sep 15, 2014 at 09:28 PM 0
Share

HI...!! plz can you tell me for what BASE_WIDTH will be used...?

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

20 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

Related Questions

how to size the guitexture which with animation on it in different resolutions? 1 Answer

Sprites are pixelated after resizing 0 Answers

Always have GUI Texture at Said Place Regardless of Resolution 1 Answer

ASPECT RATIO FILTER IN OnGUI? 3 Answers

Scale Animation 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