C# 如何避免多个硬编码方法调用

C# 如何避免多个硬编码方法调用,c#,unity3d,C#,Unity3d,我现在正在做一个我的小游戏,我正在和Unity一起创作。在回答我的问题之前,让我先简要介绍一下情况: 目前有两名球员,他们每人都有相同的一套曲目(五种,但总共十种)。在场景构建之前,每个玩家都会选择一种颜色(这将在稍后等同于某种“比赛”的棋子)。目前有三种颜色,所以我总共有15种不同的预制件,因为假设一个“蓝苹果”将有一个不同的网格,然后是一个“红苹果”。 目前我有这样的想法: using UnityEngine; using System.Collections; public class

我现在正在做一个我的小游戏,我正在和Unity一起创作。在回答我的问题之前,让我先简要介绍一下情况:
目前有两名球员,他们每人都有相同的一套曲目(五种,但总共十种)。在场景构建之前,每个玩家都会选择一种颜色(这将在稍后等同于某种“比赛”的棋子)。目前有三种颜色,所以我总共有15种不同的预制件,因为假设一个“蓝苹果”将有一个不同的网格,然后是一个“红苹果”。
目前我有这样的想法:

using UnityEngine;
using System.Collections;

public class Board : MonoBehavior
{
public Piece[,] pieces = new Piece[5,5];
public GameObject redApplePrefab;
public GameObject redBananaPrefab;
public GameObject redBerryPrefab;

public GameObject blueApplePrefab;
public GameObject blueBananaPrefab;
public GameObject blueBerryPrefab;

public GameObject whiteApplePrefab;
public GameObject whiteBananaPrefab;
public GameObject whiteBerryPrefab;


private void GenerateBoard()
{
    if(white) //don't let the missing declaration of the if statement bother you, just wanted to keep it short here)
    {
        GeneratePiece(whiteApplePrefab, 0, 0);
        GeneratePiece(whiteApplePrefab, 0, 1);
        GeneratePiece(whiteApplePrefab, 0, 2);
        GeneratePiece(whiteBananaPrefab, 1, 0);
        GeneratePiece(whiteBananaPrefab, 1, 1);
        GeneratePiece(whiteBananaPrefab, 2, 0);
        GeneratePiece(whiteBerryPrefab, 2, 2);
    }
    if(blue) //don't let the missing declaration of the if statement bother you, just wanted to keep it short here)
    {
        GeneratePiece(blueApplePrefab, 0, 0);
        GeneratePiece(blueApplePrefab, 0, 1);
        GeneratePiece(blueeApplePrefab, 0, 2);
        GeneratePiece(blueeBananaPrefab, 1, 0);
        GeneratePiece(blueeBananaPrefab, 1, 1);
        GeneratePiece(blueeBananaPrefab, 2, 0);
        GeneratePiece(blueeBerryPrefab, 2, 2);
    }
    if(red) //don't let the missing declaration of the if statement bother you, just wanted to keep it short here)
    {
        GeneratePiece(redApplePrefab, 0, 0);
        GeneratePiece(redApplePrefab, 0, 1);
        GeneratePiece(redApplePrefab, 0, 2);
        GeneratePiece(redBananaPrefab, 1, 0);
        GeneratePiece(redBananaPrefab, 1, 1);
        GeneratePiece(redBananaPrefab, 2, 0);
        GeneratePiece(redBerryPrefab, 2, 2);
    }
}

GeneratePiece(Gameobject prefab, int x, int y)
{
    GameObject go = Instantiate(prefab) as GameObject;
    go.transform.SetParent(transform);
    Piece p = go.getComponent<Piece>();
    pieces[x, y] = p;
}
}
还是最好有这样的二维预制阵列:

GameObject[numberOfColours, numberOfPieces] prefabs;
然后有一种映射器,可以将颜色“白色”转换为0,“蓝色”转换为1,等等,对于这类碎片也是如此,这样我就可以访问阵列中的特定预制件了

GameObject prefabToGenerate = prefabs[colourPlay1.ColourToNumber(), pieceToBeCreated.PieceToNumber()];

您可以尝试以这种方式稍微避免所有的复制和粘贴代码

GenerateBoard
中,你可以有这样的东西,在
appleWithColor
中,其他的你可以有一个
public GameObject[]appleWithColor
并从检查器中填充颜色。其他水果也一样

for(int color = 0;color < MAX_COLORS; color++)
{
    GameObject Apple = appleWithColor[color];//set depeding on color
    GameObject Bannana = bannanaWithColor[color];
    GameObject Berry = berryWithColor[color];

    for(int i = 0; i <3;i++)
    {
        GeneratePiece(Apple, 0,i);
    }
    for(int i = 0; i <2;i++)
    {
        GeneratePiece(Bannana, 1,i);
    }
    GeneratePiece(Bannana, 2,0);        
    GeneratePiece(Berry,2,2);
}
for(int color=0;color。。。
if(红色)//不要让if语句丢失的声明困扰您,只想在这里保持简短)
{
GeneratePieces(redApplePrefab,new List(){new Vector2(0,0),new Vector2(0,1),new Vector2(0,2)});
GeneratePieces(redBananaPrefab,new List(){new Vector2(1,0),new Vector2(1,1),new Vector2(2,0)});
GeneratePiece(Redberry预制件,2,2);
}
}
GeneratePieces(游戏对象预制、列表坐标)
{
foreach(坐标中的矢量2矢量)
{
发电机(预制、矢量X、矢量Y);
}
}
GeneratePiece(游戏对象预制,整数x,整数y)
{
GameObject go=实例化(预制)为GameObject;
go.transform.SetParent(transform);
工件p=go.getComponent();
件[x,y]=p;
}
}

使用枚举表示颜色:

public enum PieceColor
{
    white, blue, red
}
然后使用
Dictionary
enum和
Action
作为键和值来表示要调用的函数:

Dictionary<PieceColor, Action> colorToAction = new Dictionary<PieceColor, Action>();
下面是新的
生成板
功能。每次需要调用函数时,获取颜色并使用
TryGetValue
检索相应的函数,然后使用
Invoke
函数调用函数

PieceColor color = PieceColor.white;

private void GenerateBoard()
{
    Action action;
    if (colorToAction.TryGetValue(color, out action))
        action.Invoke();
}

我们通常会修复bug,而不是重构工作代码(有一个“代码审查”StackExchange站点可以做到这一点),但显然,这里的解决方案是使用某种包含board配置的列表或数组,然后您可以循环生成代码。@ADyson,哦,对不起,我不知道这一点。有没有办法把我的问题转移到StackExchange?方法仍然是硬编码的。区别在于您使用了Vector2
Dictionary<PieceColor, Action> colorToAction = new Dictionary<PieceColor, Action>();
void Init()
{
    colorToAction.Add(PieceColor.white,
    delegate
    {
        GeneratePiece(whiteApplePrefab, 0, 0);
        GeneratePiece(whiteApplePrefab, 0, 1);
        GeneratePiece(whiteApplePrefab, 0, 2);
        GeneratePiece(whiteBananaPrefab, 1, 0);
        GeneratePiece(whiteBananaPrefab, 1, 1);
        GeneratePiece(whiteBananaPrefab, 2, 0);
        GeneratePiece(whiteBerryPrefab, 2, 2);
    });

    colorToAction.Add(PieceColor.blue,
    delegate
    {
        GeneratePiece(blueApplePrefab, 0, 0);
        GeneratePiece(blueApplePrefab, 0, 1);
        GeneratePiece(blueApplePrefab, 0, 2);
        GeneratePiece(blueBananaPrefab, 1, 0);
        GeneratePiece(blueBananaPrefab, 1, 1);
        GeneratePiece(blueBananaPrefab, 2, 0);
        GeneratePiece(blueBerryPrefab, 2, 2);
    });

    colorToAction.Add(PieceColor.red,
    delegate
    {
        GeneratePiece(redApplePrefab, 0, 0);
        GeneratePiece(redApplePrefab, 0, 1);
        GeneratePiece(redApplePrefab, 0, 2);
        GeneratePiece(redBananaPrefab, 1, 0);
        GeneratePiece(redBananaPrefab, 1, 1);
        GeneratePiece(redBananaPrefab, 2, 0);
        GeneratePiece(redBerryPrefab, 2, 2);
    });
}
PieceColor color = PieceColor.white;

private void GenerateBoard()
{
    Action action;
    if (colorToAction.TryGetValue(color, out action))
        action.Invoke();
}