1

I made a small 2D game by using unity3D(5.0.2f1). I dragged in some textures to use as the background in a scene. It worked well in the Unity Editor, but not when I built and ran it in a different window size. It didn't look as what I had seen in Unity Editor.

What should I do to change the texture size automatically when window size changes? Here is a screenshot of how the image looks in the editor and the build, as well as my build settings:

Screenshots

Serlite
  • 12,130
  • 5
  • 38
  • 49

1 Answers1

1

Maybe these will help!

//file ExtensionsHandy.cs
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public static class ExtensionsHandy
{
public static float OrthoScreenWidth(this Camera c)
    {
    return
        c.ViewportToWorldPoint(new Vector3(1,1,10)).x
        - c.ViewportToWorldPoint(new Vector3(0,1,10)).x;
    }

public static float OrthoScreenHeight(this Camera c)
    {
    return
        c.ViewportToWorldPoint(new Vector3(0,1,10)).y
        - c.ViewportToWorldPoint(new Vector3(0,0,10)).y;
    }

}

try like this

   void Start()
     {
     float w = Camera.main.OrthoScreenWidth();
     Debug.Log("w is " +w.ToString("f4");
     }

Note. If not familiar with "extensions": quick tutorial.

Here are more very handy extensions for you.

These will actually move an object to the screen points!

For example,

 transform.ScreenRight();
 transform.ScreenTop();

the object, is now at the top-right of the screen!!!

And consider this ......

void Update()
  {
  transform.ScreenRight();
  }

In that example: even if the user changes the orientation of the device, even if the user resizes the screen (Mac or Windows), the object is ALWAYS on the right of the screen!!

public static Vector3 WP( this Camera c, float x, float y, float z)
    {
    return c.ViewportToWorldPoint(new Vector3(x,y,z));
    }

public static void ScreenLeft(this GameObject moveme)
    {
    Vector3 rr = moveme.transform.position;

    Vector3 leftTop = Camera.main.WP(0f,1f,0f);
    float leftX = leftTop.x;

    rr.x = leftX;

    moveme.transform.position = rr;
    }

public static void ScreenRight(this GameObject moveme)
    {
    Vector3 rr = moveme.transform.position;

    Vector3 rightTop = Camera.main.WP(1f,1f,0f);
    float rightX = rightTop.x;

    rr.x = rightX;

    moveme.transform.position = rr;
    }

public static void ScreenBottom(this GameObject moveme)
    {
    Vector3 rr = moveme.transform.position;

    Vector3 bottomLeft = Camera.main.WP(0f,0f,0f);
    float bottomY = bottomLeft.y;

    rr.y = bottomY;

    moveme.transform.position = rr;
    }

public static void ScreenTop(this GameObject moveme)
    {
    Vector3 rr = moveme.transform.position;

    Vector3 topLeft = Camera.main.WP(0f,1f,0f);
    float topY = topLeft.y;

    rr.y = topY;

    moveme.transform.position = rr;
    }

Hope it helps.....

Community
  • 1
  • 1
Fattie
  • 27,874
  • 70
  • 431
  • 719
  • Why ExtensionHandy needn't to inherit from MonoBehaviour? – 爱笑的笔迹 Apr 06 '16 at 02:48
  • How to get MainCamera in static class ExtensionHandy?I am a fresh man in unity3D.so...Would you please help me more?Thank you very much. – 爱笑的笔迹 Apr 06 '16 at 02:57
  • yeah! I found that your code extended Camera.main. I had tried.But what does the method of WP means? – 爱笑的笔迹 Apr 06 '16 at 10:26
  • heh no problem. please write new questions. "reactive" layout ("size of screen") is very difficult issue ...... extremely difficult – Fattie Apr 06 '16 at 14:18
  • 1
    Stack over flow doesn't allow me to ask question any more!Because I asked a question not long before!I am a fresh man in StackOverFlow..so sorry... English is very useful for coder .But My English is bad.. – 爱笑的笔迹 Apr 06 '16 at 14:29