GuiRatioFixer

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
Line 17: Line 17:
 
==C# - GuiRatioFixer.cs==
 
==C# - GuiRatioFixer.cs==
 
<csharp>  
 
<csharp>  
using UnityEngine;
+
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;
 
      
 
      
Line 35: Line 35:
 
     }
 
     }
 
      
 
      
}
+
}
 
</csharp>
 
</csharp>
  

Revision as of 15:32, 2 December 2005

Author: Jonathan Czeck (aarku)

Contents

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.

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

<csharp> 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;
   }
   

} </csharp>

Boo - GuiRatioFixer.boo

<boo> import UnityEngine

  1. Use this on a guiText or guiTexture object to automatically have them
  2. adjust their aspect ratio when the game starts.

class GuiRatioFixer (MonoBehaviour):

   m_NativeRatio = 1.3333333333333
   
   def Start ():
       currentRatio = (Screen.width+0.0) / Screen.height
       transform.localScale.x *= m_NativeRatio / currentRatioe

</boo>

JavaScript - GuiRatioFixer.js

<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;

</javascript>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox