提问者:小点点

如何检查Unity中最接近对象的颜色?


null

公共类破坏对象:单行为{

[SerializeField]
private List<GameObject> objects;

public GameObject[,] game;
public GameObject[] gameObjects;

[SerializeField]
private float timeLeft;

[SerializeField]
GameObject prefab;

[SerializeField]
Material colorOfPlayer;

[SerializeField]
MeshRenderer[,] mesh;

[SerializeField]
private TowerCreator tower;

// Start is called before the first frame update
void Start()
{
    tower = FindObjectOfType<TowerCreator>();
}

// Update is called once per frame
void Update()
{ 
    timeLeft -= Time.deltaTime;
    if (timeLeft < 0)
    {
        timeLeft = 1;
        gameObjects = GameObject.FindGameObjectsWithTag("Object");

        game = new GameObject[tower.pieceCount, tower.levelsOfTowerCount];

        for (int i = 0; i < tower.pieceCount; i++)
        {
            for (int j = 0; j < tower.levelsOfTowerCount; j++)
            {
                game[i, j] = gameObjects[i];
                Debug.Log(game.Length);
                findColors();

            }
        }
    }
    
}

public void findColors()
{
    for (int i = 0; i < tower.pieceCount; i++)
    {
        for (int j = 0; j < tower.levelsOfTowerCount; j++)
        {
            var mesh = game[i, j].GetComponent<MeshRenderer>();
            Debug.Log(mesh.material.color);

        }
    }
}

共1个答案

匿名用户

您不希望检查对象的“物理”颜色(如呈现器所示)。您需要的是一个包含游戏中所有可用颜色的枚举,因为您希望将它们用作标识符:

public enum MyColor {
    Red,
    Green,
    Blue
}

public class Ball : MonoBehaviour {
    public MyColor BallColor;
}

public class Cylinder : MonoBehaviour {
    [SerializeField] private MyColor myColor;
    
    private void Start() {
        // set the renderers color ("physical" color) to a desired rgb value 
        GetComponent<MeshRenderer>().material.color = TranslateEnumToColor(myColor);
    }

    private static Color TranslateEnumToColor(MyColor c) {
        ...
    }

    ...
    
    // call this, when ball hits cylinder
    public void BallHitMe(Ball theHittingBall) {
        if(theHittingBall.BallColor == myColor) {
            Debug.Log("colors do match, destroy me now");
        }
    }
}

null