Skip to content

Instantly share code, notes, and snippets.

@ertanturan
Last active February 9, 2022 15:37
Show Gist options
  • Save ertanturan/7adc015ee6b850e8372c816d3cac8382 to your computer and use it in GitHub Desktop.
Save ertanturan/7adc015ee6b850e8372c816d3cac8382 to your computer and use it in GitHub Desktop.
Real-Time Unity UV Traffic Animation
using UnityEngine;
using System.Collections;
public class UVAnimation : MonoBehaviour {
public int uvTileY = 4; // texture sheet columns
public int uvTileX = 4; // texture sheet rows
public int fps = 30;
private int index;
private Vector2 size;
private Vector2 offset;
private Renderer renderer;
void Start()
{
renderer = GetComponent<Renderer>();
}
void Update () {
//calculate the index
index = (int)(Time.time * fps);
//repeat when when exhausting all frames
index = index % (uvTileY * uvTileX);
//size of each tile
size = new Vector2(1.0f / uvTileY, 1.0f / uvTileX);
//split into horizontal and vertical indexes
var uIndex = index % uvTileX;
var vIndex = index / uvTileX;
//build the offset
//v coordinate is at the bottom of the image in openGL, so we invert it
offset = new Vector2(uIndex * size.x, 1.0f - size.y - vIndex * size.y);
renderer.material.SetTextureOffset ("_MainTex", offset);
renderer.material.SetTextureScale ("_MainTex", size);
}
}
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!21 &2100000
Material:
serializedVersion: 6
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_Name: Trafic
m_Shader: {fileID: 4800000, guid: 5cd052ce7ea5a2f438f599e02de6a390, type: 3}
m_ShaderKeywords:
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
m_CustomRenderQueue: -1
stringTagMap: {}
disabledShaderPasses: []
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _BumpMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailAlbedoMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailMask:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailNormalMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _EmissionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MainTex:
m_Texture: {fileID: 2800000, guid: 5ce322a465dcd8e4ab31f72a4d58accc, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MetallicGlossMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _OcclusionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _ParallaxMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Floats:
- _BumpScale: 1
- _Cutoff: 0.5
- _DetailNormalMapScale: 1
- _DstBlend: 0
- _GlossMapScale: 1
- _Glossiness: 0.5
- _GlossyReflections: 1
- _InvFade: 3
- _Metallic: 0
- _Mode: 0
- _OcclusionStrength: 1
- _Parallax: 0.02
- _SmoothnessTextureChannel: 0
- _SpecularHighlights: 1
- _SrcBlend: 1
- _UVSec: 0
- _ZWrite: 1
m_Colors:
- _Color: {r: 1, g: 1, b: 1, a: 1}
- _EmisColor: {r: 0.2, g: 0.2, b: 0.2, a: 0}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
- _TintColor: {r: 1, g: 0.817742, b: 0.6556604, a: 1}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment