using UnityEngine;
using System.Collections;
public class GenerateTiles : MonoBehaviour {
public int board_size_x_;
public int board_size_z_;
public GameObject tile_prefab_;
public Color colorStart = Color.red;
public Color colorEnd = Color.green;
public GameObject[,] tileObj = new GameObject[10 , 10];
public int[,] tileData = new int[10, 10];
// Use this for initialization
void Start () {
for (int x = 0; x < board_size_x_; x++) {
for (int z = 0; z < board_size_z_; z++) {
tileObj[x, z] = Instantiate(tile_prefab_,new Vector3(x * 10.5f,0,z * 10.5f),Quaternion.identity) as GameObject;
tileData[x, z] = 0;
}
}
}
public bool CheckOccupied(GameObject obj, int x, int z) {
int ix = (int) obj.transform.position.x / 10;
int iz = (int) obj.transform.position.z / 10;
for(int i = ix; i < ix + x; i++)
{
for(int j = iz; j < iz + z; j++)
{
if(i >= 10 || j >= 10)
return true;
if(tileData[i, j] == 1)
return true;
}
}
return false;
}
public void SetOccupied(GameObject obj, int x, int z) {
int ix = (int) obj.transform.position.x / 10;
int iz = (int) obj.transform.position.z / 10;
for(int i = ix; i < ix + x; i++)
{
for(int j = iz; j < iz + z; j++)
{
tileData[i, j] = 1;
//tile_prefab_.renderer.material.color = Color.blue;
}
}
}
public void OnMouseCheck(GameObject obj, int x, int z){
int ix = (int) obj.transform.position.x / 10;
int iz = (int) obj.transform.position.z / 10;
for(int i = ix; i < ix + x; i++)
{
for(int j = iz; j < iz + z; j++)
{
OnMouseEnter();
}
}
}
void OnMouseEnter()
{
tile_prefab_.renderer.material.color = Color.green;
}
void OnMouseExit () {
}
}
This is my whole code of the generate tile script that was attached to the camera to print out the tiles(10 by 10). But right now, I want to make like where if user scroll the mouse cursor to the portion of the tile map, the tile map (2 by 2 size) would change color into green instead of the original color. And when the user move the mouse cursor away from the tile map, then the color would return back to normal. But, right now the changing color is not working. What should I do , help.
it would make more sense to handle the mouse moving over a tile in a script attached to the tile rather than the one creating the tiles to start with.
I’ve not played with materials just yet, but the tile_prefab_ is the template transform that creates the instantiated tile objects. I don’t think you want to be changing the template (it’ll either change nothing or all of them… not sure which )… you want to change the colour used by the individual instantiated tile.
Do the same in your OnMouseExitFuction. But I think there is somethings missing in your script. Your script doesn’t actually check a block unless you have another script attached to the blocks that handle when the mouse is over them.
Your code is not working as the OnMouseenter cant take in parameter.
Anyway, the SetOccupied function and the CheckOccupied is actually working when the program is checking whether the tiles is occupied, so now I am trying to apply by changing color when user scroll the mouse cursor to the particular tile, then it would change color. If i try to do it like that, renderer.material.color = Color.green;, the program also never shown anything.
public void OnMouseCheck(GameObject obj, int x, int z){
int ix = (int) obj.transform.position.x / 10;
int iz = (int) obj.transform.position.z / 10;
for(int i = ix; i < ix + x; i++)
{
for(int j = iz; j < iz + z; j++)
{
tileObj[i, j].renderer.material.color = colorStart;
}
}
}
But it would only change color when user click the tile area, but for my case I want the color to eb change when the user move using the mouse cursor around the tile map. But the problem is to do that, i need to create a unity OnMouseEnter() function to do that, but the function would not accept other parameters. As for my case, i need to use those parameters but due to the OnMouseEnter() function not being able to that, thewn how should I place those parameters or using the OnMouseEnter() function.
OnMouseEnter is used per instance. You have to have a script attached to the object and have it use OnMouseEnter. Your going to have to make a script that has your OnMouseEnter function on each cube you have. Then you can get/set information on your cube from your manager class.
Anyway to scoll and change color, we use OnMouseEnter() function, but the problem is that this function cant even take parameter, how shhould I do now because if I cant get the parameter, there is no way to change color when the mouse cursor is pointing to the tile map.