Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class AdjustUV : MonoBehaviour {
- public Vector2 tiles = Vector2.one;
- public int index = -1;
- public bool adjusted;
- void Awake() {
- index = -1;
- adjusted = false;
- }
- void Update() {
- if (!adjusted) {
- Adjust();
- adjusted = true;
- }
- }
- void Adjust () {
- if (index == -1)
- {
- Car car = GetComponent<Car>();
- if (car != null)
- {
- index = car.id;
- }
- }
- int tileX = (index % (int)tiles.x);
- int tileY = Mathf.FloorToInt(index / tiles.x);
- float shiftX = (1 / tiles.x) * tileX;
- float shiftY = (1 / tiles.y) * tileY;
- Vector2 shift = new Vector2( shiftX, shiftY );
- var meshes = new List<Mesh>();
- foreach (Transform t in GetComponentsInChildren<Transform>())
- {
- MeshFilter mf = t.GetComponent<MeshFilter>();
- if (mf != null)
- {
- Vector2[] uv = mf.mesh.uv;
- for (int n=0; n<uv.Length; ++n)
- {
- uv[n] += shift;
- }
- mf.sharedMesh.uv = uv;
- meshes.Add(mf.sharedMesh);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment