private void RefreshStateText(object eventData = null) { if (cardObject.GetIsDeployed())…
- User
private void RefreshStateText(object eventData = null) { if (cardObject.GetIsDeployed()) { m_stateText.gameObject.SetActive(false); } else { GameTimeSpan deployTimer = cardObject.DeployTimer.TimeLeftTillNextTick; m_stateText.text = $"{deployTimer.Value.Num + 1}"; }
m_costText.text = $"{cardObject.GetCost().Value.Value.Num}"; m_nameText.text = m_nameTextTrap.text = $"{cardObject.GetName()}"; m_descriptionText.text = $"{cardObject.GetDescription()}"; if (cardObject.GetAvatar() != null) { UpdateCardImage(cardObject.GetAvatar().name); } UpdateCardFaceImage(cardObject.GetCardType(),cardObject.GetFaceRankType()); m_btnActiveTrap.gameObject.SetActive(cardObject.GetIsDeployed() && IsInTrapField && IsLocalPlayer); m_nameTextTrap.gameObject.SetActive(IsLocalPlayer && cardObject.GetCardPosition().CardPositionType != CardPositionTypes.CPChain); } 帮忙优化一下这个代码 - Assistant
为了优化你的代码,我们可以考虑一些通用的最佳实践,比如减少重复代码、提升可读性、抽象和封装相似功能的代码等。下面是一个优化后的代码示例:
private void RefreshStateText(object eventData = null) { ToggleStateTextVisibility(); UpdateCostText(); UpdateNameText(); UpdateDescriptionText(); UpdateAvatarImage(); UpdateCardFaceImage(); ToggleActiveTrapButton(); } private void ToggleStateTextVisibility() { bool isDeployed = cardObject.GetIsDeployed(); m_stateText.gameObject.SetActive(!isDeployed); if (!isDeployed) { GameTimeSpan deployTimer = cardObject.DeployTimer.TimeLeftTillNextTick; m_stateText.text = $"{deployTimer.Value.Num + 1}"; } } private void UpdateCostText() { m_costText.text = $"{cardObject.GetCost().Value.Value.Num}"; } private void UpdateNameText() { string cardName = cardObject.GetName(); m_nameText.text = cardName; m_nameTextTrap.text = cardName; } private void UpdateDescriptionText() { m_descriptionText.text = $"{cardObject.GetDescription()}"; } private void UpdateAvatarImage() { Avatar avatar = cardObject.GetAvatar(); if (avatar != null) { UpdateCardImage(avatar.name); } } private void UpdateCardFaceImage() { UpdateCardFaceImage(cardObject.GetCardType(), cardObject.GetFaceRankType()); } private void ToggleActiveTrapButton() { bool shouldShowButton = cardObject.GetIsDeployed() && IsInTrapField && IsLocalPlayer; m_btnActiveTrap.gameObject.SetActive(shouldShowButton); bool shouldShowTrapText = IsLocalPlayer && cardObject.GetCardPosition().CardPositionType != CardPositionTypes.CPChain; m_nameTextTrap.gameObject.SetActive(shouldShowTrapText); }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.