0
\$\begingroup\$

I'm using C# code to make a responsive GuiText. I was abel to make a responsive GuiTexture, but I'm struggling with doing the same for a GuiText.

Code for the responsive GuiTexture

using UnityEngine;
using System.Collections;

public class ResponsiveGUITexture : MonoBehaviour {
    public float designScreenWidth = 800, designScreenHeight = 480;
    public bool debugMode = false;

    private Rect pixelInset0;

    void Start () {
        pixelInset0 = this.guiTexture.pixelInset;
        this.guiTexture.pixelInset = new Rect(Screen.width / designScreenWidth * pixelInset0.x, Screen.height / designScreenHeight * pixelInset0.y, Screen.width / designScreenWidth * pixelInset0.width, Screen.height / designScreenHeight * pixelInset0.height);
    }

    void Update(){
        if(debugMode){
            this.guiTexture.pixelInset = new Rect(Screen.width / designScreenWidth * pixelInset0.x, Screen.height / designScreenHeight * pixelInset0.y, Screen.width / designScreenWidth * pixelInset0.width, Screen.height / designScreenHeight * pixelInset0.height);
        }
    }
}
\$\endgroup\$
2
  • 2
    \$\begingroup\$ Responsive to what?? Screen resolution? Or something else? I don't understand your question \$\endgroup\$
    – Savlon
    Jun 15 '14 at 11:54
  • \$\begingroup\$ @Mitananda PLEASE SHARE THE SOLUTION IF U HAVE FOUND \$\endgroup\$ Oct 28 '15 at 4:53
-1
\$\begingroup\$

Once I did a little API to do what I wanted to do, and it worked, it's not finished, but, it could help you out. Use your creativity, and make new things to this API ;)

using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;

/*
 * @author: Rafael C. Nunes
 * @email: rafaelnunes737@hotmail.com
 * 
 * @version: 0.1
 * @date: 08/02/2014
 * 
 * 
 * This archive is the screen API, that provide a screen configuration of the PC.
 * By default, all the elements that are instantiated on the screen will be on     the    left.
 * 
 */

[ExecuteInEditMode]
public class _screen : MonoBehaviour
{
//Return the screen width and height
public int getScreenWidth
{ 
    get 
    {
        return Screen.width;
    } 
}
public int getScreenHeight
{
    get
    {
        return Screen.height;
    }
} 

/*
 * helping the programmer/designer to position the elements on the screen, positionLeft is the 
 * position left of the screen, the positionMiddle is the center of the screen positionRight is 
 * the position right of the screen, it is just a draw. 
 */
public int positionLeft 
{
    get 
    {
        return Screen.width - Screen.width;
    }
}
public int positionMiddle
{
    get
    {
        return Screen.width / 2;
    }
}
public int positionRight
{
    get
    {
        return Screen.width;
    }
}

private int screenWidth = Screen.width;
private int screenHeight = Screen.height;

public void setCursor(Texture2D texture, Vector2 hotspot, CursorMode cursor)
{
    UnityEngine.Cursor.SetCursor(texture, hotspot, cursor);
}


public bool createButton(float left, float top, float width, float height, String value) 
{
    return GUI.Button(new Rect((screenWidth-screenWidth)+left, (screenHeight-screenHeight)+top, 
                                                                        width, height), value);
}

public bool createRepeatButton(float left, float top, float width, float height, String value)
{
    return GUI.RepeatButton(new Rect(left, top, width, height), value);
}

public void createLabel(float left, float top, float width, float height, String value) 
{
    GUI.Label(new Rect((screenWidth - screenWidth)+left, (screenHeight - screenHeight)+top, 
                                                                    width, height), value);
}
}
\$\endgroup\$
3
  • \$\begingroup\$ Your comment has no relation to the question. How is your code responsive to what? \$\endgroup\$
    – aggsol
    Dec 12 '14 at 13:33
  • \$\begingroup\$ Responsive to the window of the game. I supposed that the OP was asking for this. \$\endgroup\$
    – ranu
    Dec 12 '14 at 13:36
  • \$\begingroup\$ And it's not one hundred per cent precise. \$\endgroup\$
    – ranu
    Dec 12 '14 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.