GuiRatioFixer

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Usage)
m (Text replace - "</boo>" to "</syntaxhighlight>")
 
(22 intermediate revisions by 7 users not shown)
Line 1: Line 1:
 +
[[Category: GUI]]
 +
[[Category: MonoBehaviour]]
 +
[[Category: C Sharp]]
 +
[[Category: Boo]]
 +
[[Category: JavaScript]]
 
Author: Jonathan Czeck (aarku)
 
Author: Jonathan Czeck (aarku)
 
==Description==
 
==Description==
Line 9: Line 14:
  
 
If you prefer working in a different aspect ratio, adjust the Native Ratio value to your native aspect ratio desired.  This is horizontal / vertical.
 
If you prefer working in a different aspect ratio, adjust the Native Ratio value to your native aspect ratio desired.  This is horizontal / vertical.
 
<math>\frac ab = c</math>
 
  
 
==C# - GuiRatioFixer.cs==
 
==C# - GuiRatioFixer.cs==
using UnityEngine;
+
<syntaxhighlight lang="csharp">using UnityEngine;
using System.Collections;
+
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;
}
+
    }
+
   
}
+
}
 +
</syntaxhighlight>
 +
 
 
==Boo - GuiRatioFixer.boo==
 
==Boo - GuiRatioFixer.boo==
import UnityEngine
+
<syntaxhighlight lang="boo">import UnityEngine
 
   
 
   
# 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.
+
 
class GuiRatioFixer (MonoBehaviour):
+
class GuiRatioFixer (MonoBehaviour):
public m_NativeRatio = 1.3333333333333
+
    public m_NativeRatio = 1.3333333333333
+
   
Start ():
+
    def Start ():
currentRatio = (Screen.width+0.0) / Screen.height
+
        currentRatio = (Screen.width+0.0) / Screen.height
scale = transform.localScale
+
        transform.localScale.x *= m_NativeRatio / currentRatioe
scale.x *= m_NativeRatio / currentRatio
+
</syntaxhighlight>
transform.localScale = scale
+
  
 
==JavaScript - GuiRatioFixer.js==
 
==JavaScript - GuiRatioFixer.js==
// Use this on a guiText or guiTexture object to automatically have them
+
<syntaxhighlight lang="javascript">// 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.
var m_NativeRatio = 1.3333333333333;
+
var m_NativeRatio = 1.3333333333333;
+
   
currentRatio = (Screen.width+0.0) / Screen.height;
+
currentRatio = (Screen.width+0.0) / Screen.height;
transform.localScale.x *= m_NativeRatio / currentRatio;
+
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