Search Unity

How to match automatically the texture proportion with a plane proportion scale?

Discussion in 'Editor & General Support' started by BenoitFreslon, Jan 16, 2014.

  1. BenoitFreslon

    BenoitFreslon

    Joined:
    Jan 16, 2013
    Posts:
    166
    Hello,

    I'm new with Unity.

    I would like to match the texture size with a plane size. But actually the texture is stretched by default.

    I have a 400x300 texture in a Material (4/3 proportion). I want to set the material in my plane.
    So I have to set manually the scale according the texture proportion : ScaleX = 4 and ScaleY = 3.

    But it's really boring to set manually all this.

    Thanks.
     
  2. Graham-Dunnett

    Graham-Dunnett

    Administrator

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    Write an editor script to do it for you.
     
  3. BenoitFreslon

    BenoitFreslon

    Joined:
    Jan 16, 2013
    Posts:
    166
    I tried to write an editor script but I can't get the native texture file size.
    I got the texture size compressed with Unity.

    Here my editor script :

    Code (csharp):
    1. #if UNITY_EDITOR
    2.  
    3. using UnityEditor;
    4. using UnityEngine;
    5.  
    6.  
    7. class ProportionalSizeWithTexture {
    8.    
    9.     [MenuItem("Tools/Benoit/Propotional Scale With Texture")]
    10.    
    11.     static void UpgradeSolutions() {
    12.         //Debug.Log(Selection.activeGameObject);
    13.         if (Selection.activeGameObject) {
    14.             GameObject go = Selection.activeGameObject;
    15.             Debug.Log(go.renderer.material.mainTexture.width + " " + go.renderer.material.mainTexture.height);
    16.             float scaleX = go.renderer.material.mainTexture.width / 100f;
    17.             float scaleZ = go.renderer.material.mainTexture.height / 100f;
    18.             go.transform.localScale = new Vector3(scaleX, 1f, scaleZ);
    19.         }
    20.  
    21.     }
    22. }
    23. #endif
    24.  

    The go.renderer.material.mainTexture.width value returns : a power of 2 size.
    Even if I set a Non Power Of Two texture I got the same result.
     
    Last edited: Jan 30, 2014