How can I make my texure line up right with the default cube
So I have a square texture (300 X 300) and I would assume that it would align right with the default cube all around but on some faces it flips the image upside down.
On three of the four faces the texture is fine and is the right side up but it flips it on one side and I can't figure out why.
I added a T to the top of the texture to help illustrate whats wrong. Any help would be great.
Sarper is correct. Unity isnt always easy to work around textures, so you cant essentially drag/drop onto a object and expect perfect results... You have to mess around with shaders or import the object as a mesh and UV the texture onto it, and sometimes a combination of both (which from my experience, importing UV's is essentially making Unity create a custom shader with default settings)
I've tried using a basic cube I imported from blender with UV's that I set up and it works. I'd just really prefer to use the built in cube for the sake of easier access and an overall cleaner environment. The weird thing is that some other textures of the same proportions line up just fine while others don't.
Answer by RumbleStrut · Nov 06, 2017 at 04:10 PM
using UnityEngine;
public class FILENAME : MonoBehaviour {
//Flips the UV on the backside of the cube so it matches the front and sides
void OnValidate () {
Vector2[] uvs = GetComponent<MeshFilter> ().sharedMesh.uv;
uvs [6] = new Vector2 (0, 0);
uvs [7] = new Vector2 (1, 0);
uvs [10] = new Vector2 (0, 1);
uvs [11] = new Vector2 (1, 1);
GetComponent<MeshFilter> ().sharedMesh.uv = uvs;
}
}
Was struggling with the same (I'm an absolute beginner) and this solved it immediately.
Some additional questions.
How often is the OnValidate() executed ? Only in Editor, and only after change of GameObject?
Does the script need to remain attached to the cube or prefab once it has executed? (I expect it to have updated the uvs of the cube (in the prefab) and does not need to remain attached).
Thanks.
// you are correct, here's another example
// first script
using UnityEngine;
public class MyGameObjectClass : MonoBehavior {
public GameObject cuber;
void Start () {
MyStaticClass.FlipCubeFace(cuber);
}
}
//second script
using UnityEngine;
public static class MyStaticClass {
public static void FlipCubeFace (GameObject cubee) {
Vector2[] uvs = cubee.GetComponent<MeshFilter> ().sharedMesh.uv;
uvs [6] = new Vector2 (0, 0);
uvs [7] = new Vector2 (1, 0);
uvs [10] = new Vector2 (0, 1);
uvs [11] = new Vector2 (1, 1);
cubee.GetComponent<MeshFilter> ().sharedMesh.uv = uvs;
}
}
Your answer
Follow this Question
Related Questions
orthographic sphere projection 0 Answers
instantiate gameobject with the uv 0 Answers
part of my cube is textured but not bumpmapped 0 Answers
Texture UV in custom mesh 0 Answers