GuiRatioFixer

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Text replace - "</boo>" to "</syntaxhighlight>")
 
(34 intermediate revisions by 9 users not shown)
Line 1: Line 1:
Author: Jon (Aarku)
+
[[Category: GUI]]
----
+
[[Category: MonoBehaviour]]
Use this on a guiText or guiTexture object to automatically have them adjust their aspect ratio when the game starts.
+
[[Category: C Sharp]]
 +
[[Category: Boo]]
 +
[[Category: JavaScript]]
 +
Author: Jonathan Czeck (aarku)
 +
==Description==
 +
This script will adjust the aspect ratio of a [http://www.otee.dk/Documentation/ScriptReference/GUIText.html GUIText] or [http://www.otee.dk/Documentation/ScriptReference/GUITexture.html GUITexture] object by scaling it horizontally to fit the proportions.
  
==C#==
+
It does not know how to align groups of GUI objects after it adjusts their aspect ratio.
using UnityEngine;
+
 
using System.Collections;
+
==Usage==
 +
Place this script on a GameObject with a GUIText and/or GUITexture component. Then scale the object to the correct screen size in a Game View that has aspect set to 4:3. When you run the game, the object's scale will be changed to match the actual screen ratio.
 +
 
 +
If you prefer working in a different aspect ratio, adjust the Native Ratio value to your native aspect ratio desired.  This is horizontal / vertical.
 +
 
 +
==C# - GuiRatioFixer.cs==
 +
<syntaxhighlight lang="csharp">using UnityEngine;
 +
using System.Collections;
 
   
 
   
// Use this on a guiText or guiTexture object to automatically have them
+
// Use this on a guiText or guiTexture object to automatically have them
// adjust their aspect ratio when the game starts.
+
// adjust their aspect ratio when the game starts.
 
   
 
   
public class GuiRatioFixer : MonoBehaviour
+
public class GuiRatioFixer : MonoBehaviour
{
+
{
public float m_NativeRatio = 1.3333333333333F;
+
    public float m_NativeRatio = 1.3333333333333F;
+
   
void Start ()
+
    void Start ()
{
+
    {
float currentRatio = (float)Screen.width / (float)Screen.height;
+
        float currentRatio = (float)Screen.width / (float)Screen.height;
Vector3 scale = transform.localScale;
+
        Vector3 scale = transform.localScale;
scale.x *= m_NativeRatio / currentRatio;
+
        scale.x *= m_NativeRatio / currentRatio;
transform.localScale = scale;
+
        transform.localScale = scale;
}
+
    }
+
   
}
+
}
==Javascript==
+
</syntaxhighlight>
  var m_NativeRatio = 1.3333333333333F;
+
 
+
==Boo - GuiRatioFixer.boo==
currentRatio = (Screen.width+0.0) / Screen.height;
+
<syntaxhighlight lang="boo">import UnityEngine
transform.localScale.x *= m_NativeRatio / currentRatio;
+
   
 +
# Use this on a guiText or guiTexture object to automatically have them
 +
# adjust their aspect ratio when the game starts.
 +
 
 +
class GuiRatioFixer (MonoBehaviour):
 +
    public m_NativeRatio = 1.3333333333333
 +
   
 +
    def Start ():
 +
        currentRatio = (Screen.width+0.0) / Screen.height
 +
        transform.localScale.x *= m_NativeRatio / currentRatioe
 +
</syntaxhighlight>
 +
 
 +
==JavaScript - GuiRatioFixer.js==
 +
<syntaxhighlight lang="javascript">// Use this on a guiText or guiTexture object to automatically have them
 +
// adjust their aspect ratio when the game starts.
 +
var m_NativeRatio = 1.3333333333333;
 +
   
 +
currentRatio = (Screen.width+0.0) / Screen.height;
 +
transform.localScale.x *= m_NativeRatio / currentRatio;
 +
</syntaxhighlight>

Latest revision as of 20:57, 10 January 2012

Author: Jonathan Czeck (aarku)

Contents

[edit] Description

This script will adjust the aspect ratio of a GUIText or GUITexture object by scaling it horizontally to fit the proportions.

It does not know how to align groups of GUI objects after it adjusts their aspect ratio.

[edit] Usage

Place this script on a GameObject with a GUIText and/or GUITexture component. Then scale the object to the correct screen size in a Game View that has aspect set to 4:3. When you run the game, the object's scale will be changed to match the actual screen ratio.

If you prefer working in a different aspect ratio, adjust the Native Ratio value to your native aspect ratio desired. This is horizontal / vertical.

[edit] C# - GuiRatioFixer.cs

using UnityEngine;
using System.Collections;
 
// Use this on a guiText or guiTexture object to automatically have them
// adjust their aspect ratio when the game starts.
 
public class GuiRatioFixer : MonoBehaviour
{
    public float m_NativeRatio = 1.3333333333333F;
 
    void Start ()
    {
        float currentRatio = (float)Screen.width / (float)Screen.height;
        Vector3 scale = transform.localScale;
        scale.x *= m_NativeRatio / currentRatio;
        transform.localScale = scale;
    }
 
}

[edit] Boo - GuiRatioFixer.boo

import UnityEngine
 
# Use this on a guiText or guiTexture object to automatically have them
# adjust their aspect ratio when the game starts.
 
class GuiRatioFixer (MonoBehaviour):
    public m_NativeRatio = 1.3333333333333
 
    def Start ():
        currentRatio = (Screen.width+0.0) / Screen.height
        transform.localScale.x *= m_NativeRatio / currentRatioe

[edit] JavaScript - GuiRatioFixer.js

// Use this on a guiText or guiTexture object to automatically have them
// adjust their aspect ratio when the game starts.
var m_NativeRatio = 1.3333333333333;
 
currentRatio = (Screen.width+0.0) / Screen.height;
transform.localScale.x *= m_NativeRatio / currentRatio;
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox