Skip to content

Instantly share code, notes, and snippets.

@FozerG
Created June 29, 2024 20:29
Show Gist options
  • Save FozerG/ccd74d2932505a0b72a6f4c24e87e75e to your computer and use it in GitHub Desktop.
Save FozerG/ccd74d2932505a0b72a6f4c24e87e75e to your computer and use it in GitHub Desktop.
Unity script to correct scale factor different from 1
// by FozerGames.
// Special thx to ChatGPT ^_^
using UnityEditor;
using UnityEngine;
using UnityEditor.SceneManagement;
using System.Collections.Generic;
public class ScaleFactorAdjuster : EditorWindow
{
private GameObject selectedModel;
private float targetScaleFactor = 1.0f;
[MenuItem("Tools/Scale Factor Adjuster")]
public static void ShowWindow()
{
GetWindow<ScaleFactorAdjuster>("Scale Factor Adjuster");
}
private void OnGUI()
{
GUILayout.Label("Adjust Scale Factor", EditorStyles.boldLabel);
selectedModel = (GameObject)EditorGUILayout.ObjectField("FBX Model", selectedModel, typeof(GameObject), false);
targetScaleFactor = EditorGUILayout.FloatField("Target Scale Factor", targetScaleFactor);
if (GUILayout.Button("Apply Scale Factor"))
{
if (selectedModel != null)
{
AdjustScaleFactor();
}
else
{
Debug.LogWarning("No model selected.");
}
}
}
private void AdjustScaleFactor()
{
string assetPath = AssetDatabase.GetAssetPath(selectedModel);
ModelImporter importer = AssetImporter.GetAtPath(assetPath) as ModelImporter;
if (importer != null)
{
float originalScaleFactor = importer.globalScale;
importer.globalScale = targetScaleFactor;
importer.SaveAndReimport();
float scaleMultiplier = originalScaleFactor / targetScaleFactor;
foreach (EditorBuildSettingsScene scene in EditorBuildSettings.scenes)
{
if (scene.enabled)
{
var sceneAsset = AssetDatabase.LoadAssetAtPath<SceneAsset>(scene.path);
EditorSceneManager.OpenScene(scene.path, OpenSceneMode.Single);
UpdateSceneObjectsScale(scaleMultiplier, scene.path);
EditorSceneManager.SaveScene(EditorSceneManager.GetActiveScene());
}
}
Debug.Log($"Adjusted scale factor for {selectedModel.name}: {originalScaleFactor} to {targetScaleFactor}");
}
else
{
Debug.LogWarning("No ModelImporter found for the selected model.");
}
}
private void UpdateSceneObjectsScale(float scaleMultiplier, string scenePath)
{
MeshFilter[] allMeshFilters = Resources.FindObjectsOfTypeAll<MeshFilter>();
MeshFilter[] selectedMeshFilters = selectedModel.GetComponentsInChildren<MeshFilter>(true);
HashSet<Mesh> selectedMeshes = new HashSet<Mesh>();
foreach (MeshFilter filter in selectedMeshFilters)
{
selectedMeshes.Add(filter.sharedMesh);
}
foreach (MeshFilter meshFilter in allMeshFilters)
{
if (selectedMeshes.Contains(meshFilter.sharedMesh))
{
Vector3 originalScale = meshFilter.transform.localScale;
List<Transform> children = new List<Transform>();
foreach (Transform child in meshFilter.transform)
{
children.Add(child);
}
foreach (Transform child in children)
{
child.parent = null;
}
meshFilter.transform.localScale = originalScale * scaleMultiplier;
Debug.Log($"Scaling {meshFilter.gameObject.name} in {scenePath}: {meshFilter.transform.localScale}. Before scaling: {originalScale}");
foreach (Transform child in children)
{
child.parent = meshFilter.transform;
}
if (PrefabUtility.IsPartOfPrefabInstance(meshFilter.gameObject))
{
PrefabUtility.RecordPrefabInstancePropertyModifications(meshFilter.transform);
}
EditorSceneManager.MarkSceneDirty(meshFilter.gameObject.scene);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment