quantum-space-buddies/QSB/PlayerBodySetup/Remote/FixMaterialsInAllChildren.cs

90 lines
2.4 KiB
C#
Raw Normal View History

2022-02-03 09:20:14 +00:00
using QSB.Utility;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace QSB.PlayerBodySetup.Remote
{
public static class FixMaterialsInAllChildren
{
private static readonly List<(string MaterialName, Material ReplacementMaterial)> _materialDefinitions = new();
2022-02-03 22:53:20 +00:00
private static readonly List<string> _materialNames = new()
{
"Traveller_HEA_Player_Skin_mat",
"Traveller_HEA_Player_Clothes_mat",
"Traveller_HEA_PlayerSuit_mat",
"Props_HEA_Jetpack_mat",
"Effects_HEA_MarshmallowFlames_mat",
"Effects_HEA_Smoke_mat",
"Props_HEA_RoastingStick_mat",
"Effects_HEA_ScannerLightVolume_mat",
"Effects_HEA_ScannerProjector_mat",
"Props_HEA_Lightbulb_mat",
"Props_HEA_PlayerTools_mat",
2022-02-12 22:07:01 +00:00
"Structure_HEA_PlayerShip_Screens_mat",
"Effects_RecallWhiteHole_mat",
"Effects_HEA_Vapor_mat",
2022-02-13 15:30:35 +00:00
"Props_HEA_Lightbulb_OFF_mat",
"Props_HEA_PlayerProbe_mat",
"Props_HEA_PlayerProbeLightbulb_mat",
2022-02-13 21:01:54 +00:00
"Effects_RecallBlackHole_mat",
"Effects_HEA_Vapor_Player_mat",
"Effects_HEA_ThrusterFlames_mat"
2022-02-03 22:53:20 +00:00
};
private static void ReplaceMaterial(Renderer renderer, int index, Material mat)
{
var mats = renderer.materials;
mats[index] = mat;
renderer.materials = mats;
}
private static void ReplaceMaterials(Renderer renderer, string materialName, Material replacementMaterial)
{
for (var i = 0; i < renderer.materials.Length; i++)
{
2022-02-03 11:55:41 +00:00
if (renderer.materials[i].name.Trim() == $"REM_{materialName} (Instance)")
{
ReplaceMaterial(renderer, i, replacementMaterial);
}
}
}
private static void GenerateMaterialDefinitions()
{
2022-02-03 09:20:14 +00:00
var allMaterials = (Material[])Resources.FindObjectsOfTypeAll(typeof(Material));
2022-02-03 22:53:20 +00:00
foreach (var name in _materialNames)
2022-02-03 09:20:14 +00:00
{
2022-02-03 11:55:41 +00:00
var matchingMaterial = allMaterials.FirstOrDefault(x => x.name == name);
2022-02-03 09:20:14 +00:00
if (matchingMaterial == default)
{
DebugLog.ToConsole($"Error - could not find material with the name {name}!", OWML.Common.MessageType.Error);
return;
}
2022-02-03 22:53:20 +00:00
_materialDefinitions.Add((name, matchingMaterial));
}
}
public static void ReplaceMaterials(Transform rootObject)
{
2022-02-17 15:44:21 +00:00
/*
if (_materialDefinitions.Count == 0)
{
GenerateMaterialDefinitions();
}
foreach (var renderer in rootObject.GetComponentsInChildren<Renderer>(true))
{
foreach (var (materialName, replacementMaterial) in _materialDefinitions)
{
ReplaceMaterials(renderer, materialName, replacementMaterial);
}
2022-02-03 09:20:14 +00:00
}
2022-02-17 15:44:21 +00:00
*/
}
}
}