using System.Collections; using System.Collections.Generic; using UnityEngine; /// /// Add this component to an object with a non-uniform scale (not 1,1,1) to make it 1,1,1 and move / resize any box collider / mesh renderers to match /// public class FixNonUniformScale : MonoBehaviour { bool running = false; // Only call on selected void OnDrawGizmosSelected() { if(running) { return; } running = true; MakeUniform(); DestroyImmediate(this); } public void MakeUniform() { Vector3 originalScale = transform.localScale; // Nothing to scale here if(originalScale == Vector3.one) { return; } // Scale to where we should be transform.localScale = Vector3.one; MeshRenderer ren = GetComponent(); MeshFilter filter = GetComponent(); if (ren != null) { Transform renObject = new GameObject("Renderer").transform; if(filter) { MeshFilter mf = renObject.gameObject.AddComponent(); mf.sharedMesh = filter.sharedMesh; DestroyImmediate(filter); } MeshRenderer newRenderer = renObject.gameObject.AddComponent(); newRenderer.sharedMaterial = ren.sharedMaterial; renObject.parent = transform; renObject.localPosition = Vector3.zero; renObject.localRotation = Quaternion.identity; renObject.localScale = originalScale; DestroyImmediate(ren); } BoxCollider col = GetComponent(); if(col != null) { // Need to resize box collider col.center = Vector3.zero; col.size = originalScale; } } }