- Home /
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
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();
}
}
HI...!! plz can you tell me for what BASE_WIDTH will be used...?