gpt-4-0314· openai
Suzhen/CodeChat
помоги пожалуйста с рефакторингом данного кода namespace Pedestrians {…
- User
помоги пожалуйста с рефакторингом данного кода namespace Pedestrians { [RequireComponent(typeof(Renderer))] public class BotFashionRandomization : MonoBehaviour { private const int quantityMaterialsBot = 2; private const int MinimumRandomnessValue = 0;
[SerializeField] private Renderer lodRendererLevel1; [SerializeField] private Renderer lodRendererLevel2; [SerializeField] private Renderer lodRendererLevel3; [SerializeField] private int MaximumRandomnessValue = 7; [SerializeField] private string CharacterTopColorName = "_CharacterTopColor"; [SerializeField] private string CharacterBottomColorName = "_CharacterBottomColor"; [SerializeField] private string CharacterHairColorName = "_CharacterHairColor"; private Material characterClothingMaterial; private Material characterHairMaterial; private void Start() { var objectRenderer = gameObject.GetComponent<Renderer>(); var randomTopClothingValue = Random.Range(MinimumRandomnessValue, MaximumRandomnessValue); var randomBottomClothingValue = Random.Range(MinimumRandomnessValue, MaximumRandomnessValue); var randomHairValue = Random.Range(MinimumRandomnessValue, MaximumRandomnessValue); MaterialHandlingLod(objectRenderer, randomTopClothingValue, randomBottomClothingValue, randomHairValue); MaterialHandlingLod(lodRendererLevel1, randomTopClothingValue, randomBottomClothingValue, randomHairValue); MaterialHandlingLod(lodRendererLevel2, randomTopClothingValue, randomBottomClothingValue, randomHairValue); MaterialHandlingLod(lodRendererLevel3, randomTopClothingValue, randomBottomClothingValue, randomHairValue); } private void InstallBotFashion(Material[] materials, int randomTopClothingValue, int randomBottomClothingValue, int randomHairValue) { characterClothingMaterial = materials[0]; characterHairMaterial = materials[1]; characterClothingMaterial.SetFloat(CharacterTopColorName, randomTopClothingValue); characterClothingMaterial.SetFloat(CharacterBottomColorName, randomBottomClothingValue); characterHairMaterial.SetFloat(CharacterHairColorName, randomHairValue); } private void InstallBotClothes(Material[] materials, int randomTopClothingValue, int randomBottomClothingValue) { characterClothingMaterial = materials[0]; characterClothingMaterial.SetFloat(CharacterTopColorName, randomTopClothingValue); characterClothingMaterial.SetFloat(CharacterBottomColorName, randomBottomClothingValue); } private void MaterialHandlingLod(Renderer renderer, int randomTopClothingValue, int randomBottomClothingValue, int randomHairValue) { if (renderer != null) { var materials = renderer.materials; if (materials.Length >= quantityMaterialsBot) { InstallBotFashion(materials, randomTopClothingValue, randomBottomClothingValue, randomHairValue); } else { InstallBotClothes(materials, randomTopClothingValue, randomBottomClothingValue); } } else { Debug.LogError("Renderer not found" + renderer.name); } } }}
- Assistant
using UnityEngine; namespace Pedestrians { [RequireComponent(typeof(Renderer))] public class BotFashionRandomization : MonoBehaviour { private const int QuantityMaterialsBot = 2; private const int MinimumRandomnessValue = 0; [SerializeField] private Renderer[] lodRenderers; [SerializeField] private int maximumRandomnessValue = 7; [SerializeField] private string characterTopColorName = "_CharacterTopColor"; [SerializeField] private string characterBottomColorName = "_CharacterBottomColor"; [SerializeField] private string characterHairColorName = "_CharacterHairColor"; private Material characterClothingMaterial; private Material characterHairMaterial; private void Start() { int randomTopClothingValue = Random.Range(MinimumRandomnessValue, maximumRandomnessValue); int randomBottomClothingValue = Random.Range(MinimumRandomnessValue, maximumRandomnessValue); int randomHairValue = Random.Range(MinimumRandomnessValue, maximumRandomnessValue); foreach(Renderer renderer in lodRenderers) { ApplyRandomMaterialColor(renderer, randomTopClothingValue, randomBottomClothingValue, randomHairValue); } } private void ApplyRandomMaterialColor(Renderer renderer, int randomTopClothingValue, int randomBottomClothingValue, int randomHairValue) { if (renderer == null) { Debug.LogError("Renderer not found."); return; } Material[] materials = renderer.materials; if (materials.Length < QuantityMaterialsBot) { characterClothingMaterial = materials[0]; characterClothingMaterial.SetFloat(characterTopColorName, randomTopClothingValue); characterClothingMaterial.SetFloat(characterBottomColorName, randomBottomClothingValue); return; } characterClothingMaterial = materials[0]; characterHairMaterial = materials[1]; characterClothingMaterial.SetFloat(characterTopColorName, randomTopClothingValue); characterClothingMaterial.SetFloat(characterBottomColorName, randomBottomClothingValue); characterHairMaterial.SetFloat(characterHairColorName, randomHairValue); } } }Here's the refactored code with the following changes:
- Replaced
lodRendererLevel1,lodRendererLevel2, andlodRendererLevel3with an array calledlodRenderersto make the code more scalable and easier to manage. - Removed the
InstallBotFashionandInstallBotClothesmethods and combined them into one method calledApplyRandomMaterialColoras their functionality was very similar. - Simplified and shortened the code in
ApplyRandomMaterialColormethod. - Changed variable and method names to adhere to the
camelCasenaming convention.
- Replaced
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.