Kaz*_*l13 2 c# unity-game-engine
我正在制作一个我用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;
}
}
Run Code Online (Sandbox Code Playgroud)
并且直言不讳.这有点难看.我真的想避免使用不同的值一遍又一遍地手动调用相同的方法.但是我现在有点难以理解这项工作的优雅方式.我想用刚才的颜色和种类来调用预制件,GeneratePiece()如果没有必要,我不想改变方法.所有关于预制的计算以及在该GenerateBoard()方法中将发生的位置.
建立游戏时,所有棋子都有固定的位置,与国际象棋比较,但我只有两种颜色可供选择.
我想要一些循环,我可以选择两种颜色,然后用正确的预制件生成它们并将它们放在适当的字段上.
我在考虑像双键dictonary这样的东西
Dictionary<string colour, Dictionary<string kindOfPiece, GameObject prefab>>
Run Code Online (Sandbox Code Playgroud)
或者更好的是有这样的二维预制件阵列:
GameObject[numberOfColours, numberOfPieces] prefabs;
Run Code Online (Sandbox Code Playgroud)
然后有一些映射器将颜色"白色"转换为0,"蓝色"转换为1等等,并且对于那种碎片是相同的,所以我可以访问阵列中的特定预制件?
GameObject prefabToGenerate = prefabs[colourPlay1.ColourToNumber(), pieceToBeCreated.PieceToNumber()];
Run Code Online (Sandbox Code Playgroud)
使用枚举来表示颜色:
public enum PieceColor
{
white, blue, red
}
Run Code Online (Sandbox Code Playgroud)
然后使用Dictionaryenum Action作为键和值来表示要调用的函数:
Dictionary<PieceColor, Action> colorToAction = new Dictionary<PieceColor, Action>();
Run Code Online (Sandbox Code Playgroud)
将颜色枚举映射到函数.您应该手动映射它们而不是循环,因为x,y值存在一些不一致.尝试使用for循环映射它将导致在将来阅读代码时出现混乱.
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);
});
}
Run Code Online (Sandbox Code Playgroud)
以下是您的新GenerateBoard功能.每次需要调用一个函数时,获取颜色并使用它TryGetValue来检索相应的函数,然后使用该Invoke函数调用函数.
PieceColor color = PieceColor.white;
private void GenerateBoard()
{
Action action;
if (colorToAction.TryGetValue(color, out action))
action.Invoke();
}
Run Code Online (Sandbox Code Playgroud)