c# - 如何在 Unity 中更改 Sprite uv?

标签 c# unity3d sprite uv-mapping

我写了下面的脚本:

public class SpriteUV : MonoBehaviour 
{
    private SpriteRenderer _spriteRenderer;
    private Sprite _sprite;

    [SerializeField] public Vector2[] _uv = new Vector2 [4]
    {
        new Vector2(0.4f, 0.5f),
        new Vector2(0.6f, 0.5f),
        new Vector2(0.4f, 0.35f),
        new Vector2(0.6f, 0.35f)
    }; 

    void Start ()
    {
        _spriteRenderer = GetComponent<SpriteRenderer>();
        _sprite = _spriteRenderer.sprite;
    }

    // Update is called once per frame
    void Update ()
    {
        _sprite.uv = _uv;
    }
}

但这有一个错误,它表明 Sprite.uv 没有 setter (从 the documentation 看不明显)我如何更改 Sprite 以映射纹理的不同部分?

最佳答案

这是一个与 SpriteRenderer 一起使用的解决方案,用于至少选择要显示的 Sprite 的矩形部分。 (如果您需要完全不同的映射,程序员如何在评论中正确地说明这不能“变形”您的 UV。)

  1. 创建一个新的着色器并将其命名为BlendVertexColorWithUV

  2. 在 VisualStudio(或任何文本编辑器)中打开它并在下面的代码中输入
    Source

     // unlit, vertex color, alpha blended, offset uv's
     // cull off
    
     Shader "BlendVertexColorWithUV" 
     {
         Properties 
         {
             _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
         }
    
         SubShader
         {
             Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
             ZWrite Off Lighting Off Cull Off Fog { Mode Off } Blend SrcAlpha OneMinusSrcAlpha
             LOD 110
    
             Pass 
             {
                 CGPROGRAM
                 #pragma vertex vert_vct
                 #pragma fragment frag_mult 
                 #pragma fragmentoption ARB_precision_hint_fastest
                 #include "UnityCG.cginc"
    
                 sampler2D _MainTex;
                 float4 _MainTex_ST;
    
                 struct vin_vct 
                 {
                     float4 vertex : POSITION;
                     float4 color : COLOR;
                     float2 texcoord : TEXCOORD0;
                 };
    
                 struct v2f_vct
                 {
                     float4 vertex : POSITION;
                     fixed4 color : COLOR;
                     float2 texcoord : TEXCOORD0;
                 };
    
                 v2f_vct vert_vct(vin_vct v)
                 {
                     v2f_vct o;
                     o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
                     o.color = v.color;
                     o.texcoord = TRANSFORM_TEX (v.texcoord, _MainTex);;
                     return o;
                 }
    
                 fixed4 frag_mult(v2f_vct i) : COLOR
                 {
                     fixed4 col = tex2D(_MainTex, i.texcoord) * i.color;
                     return col;
                 }
    
                 ENDCG
             } 
         }
    
         SubShader
         {
             Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
             ZWrite Off Blend SrcAlpha OneMinusSrcAlpha Cull Off Fog { Mode Off }
             LOD 100
    
             BindChannels 
             {
                 Bind "Vertex", vertex
                 Bind "TexCoord", texcoord
                 Bind "Color", color
             }
    
             Pass 
             {
                 Lighting Off
                 SetTexture [_MainTex] { combine texture * primary } 
             }
         }
     }
    
  3. 创建新 Material

  4. BlendVertexColorWithUV 拖到该 Material 上

  5. 将此 Material 分配给使用 SpriteRenderer

  6. 的对象
  7. SpriteRendererDrawMode设置为Tiled

  8. TileMode设置为Continous

enter image description here

注意:我实际上在抓取时犯了一个错误:您将 Sprite 分配给了 SpriteRenderer 而不是 Material !您实际上可以保留 Material Blanc,只需调整 TilingOffset 值即可。

现在您可以调整 Sprite 在 Material 中的偏移量,例如通过使用脚本

public Material textureToAnimate;
public Vector2 uvOffset;

....

textureToAnimate.mainTextureOffset = uvOffset;

关于c# - 如何在 Unity 中更改 Sprite uv?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52389547/

相关文章:

animation - 调整 CCAnimation 的 Sprite 大小

java - 当相机平移时, Sprite 不会跟随 box2d 主体,但如果相机在 libgdx 中是静态的,则效果良好

c# - 绑定(bind) C#、ASP.net 后在 gridview 中添加新行

c# - ListBox 和 Checkbox 中存在大量数据的奇怪行为 [提供的最小项目]

c# - xUnit 进行 Json 文件读写测试

c# - 如何在 unity c# 脚本中将 Material 设置为游戏对象?

c# - EF 3.x 中最简单的 Group By 失败, "Client side GroupBy is not supported"

c# - 一天中某个时间的 Sun Script RenderProbe

visual-studio - 我只能在 HoloLens 上部署 1 个 Unity 应用程序

Python 动画时序