如何通过使用相同的纹理将纹理设置为自动匹配 window 大小的变化
How to set texture to automatically match with a change in window size by using same texture
我用unity3D(5.0.2f1)做了一个2D小游戏。我拖入了一些纹理以用作场景中的背景。它在 Unity Editor 中运行良好,但当我以不同的 window 大小构建和 运行 时却不行。它看起来不像我在 Unity Editor 中看到的那样。
当window大小改变时,如何自动改变纹理大小?这是图像在编辑器和构建中的外观以及我的构建设置的屏幕截图:
也许这些会有所帮助!
//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;
}
}
这样试试
void Start()
{
float w = Camera.main.OrthoScreenWidth();
Debug.Log("w is " +w.ToString("f4");
}
注意。如果不熟悉"extensions":
这里有更多非常方便的扩展。
这些实际上会移动一个对象到屏幕点!
例如,
transform.ScreenRight();
transform.ScreenTop();
对象,现在在屏幕的右上角!!!
考虑一下这个......
void Update()
{
transform.ScreenRight();
}
在该示例中:即使用户更改设备的方向,即使用户调整屏幕大小(Mac 或 Windows),对象始终位于屏幕!!
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;
}
希望对您有所帮助......
我用unity3D(5.0.2f1)做了一个2D小游戏。我拖入了一些纹理以用作场景中的背景。它在 Unity Editor 中运行良好,但当我以不同的 window 大小构建和 运行 时却不行。它看起来不像我在 Unity Editor 中看到的那样。
当window大小改变时,如何自动改变纹理大小?这是图像在编辑器和构建中的外观以及我的构建设置的屏幕截图:
也许这些会有所帮助!
//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;
}
}
这样试试
void Start()
{
float w = Camera.main.OrthoScreenWidth();
Debug.Log("w is " +w.ToString("f4");
}
注意。如果不熟悉"extensions":
这里有更多非常方便的扩展。
这些实际上会移动一个对象到屏幕点!
例如,
transform.ScreenRight();
transform.ScreenTop();
对象,现在在屏幕的右上角!!!
考虑一下这个......
void Update()
{
transform.ScreenRight();
}
在该示例中:即使用户更改设备的方向,即使用户调整屏幕大小(Mac 或 Windows),对象始终位于屏幕!!
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;
}
希望对您有所帮助......