Нашел туториал по созданию миникарты - http://www.jakel168.com/2010/09/how-to- ... ne-in.html. Работает все хорошо, но мне захотелось добавить к существующей миникарте туман войны Решил поверх текстуры с самой миникартой наложить еще текстуру с туманом войны черного цвета. Идея заключается в том, чтобы с помощью Texture2D.setPixel(...) менять цвет черной текстуры на Color.clear в тех местах, где игрок находится, тем самым туман войны становится прозрачным, где нам надо и под ним становится видна текстура с миникартой. Texture2D.Apply() я делаю раз в 2-3 секунды, так что проблем с производительностью не возникает. Вот код, из примера выше, который я немного изменил:
Синтаксис:
Используется csharp
public class MiniMapScript : MonoBehaviour
{
//For placing the image of the mini map.
public Texture2D MiniMap;
public Texture2D FogOfWar;
//Two transform variables, one for the player's and the enemy's,
public Transform Player;
public Transform Enemy;
//Icon images for the player and enemy(s) on the map.
public Texture2D PlayerIcon;
public Texture2D EnemyIcon;
//Offset variables (X and Y) - where you want to place your map on screen.
public float MapOffSetX = 0;
public float MapOffSetY = 0;
//The width and height of your map as it'll appear on screen,
public float MapWidth = 200;
public float MapHeight = 200;
//Width and Height of your scene, or the resolution of your terrain.
public float SceneWidth = 500;
public float SceneHeight = 500;
//The size of your player's and enemy's icon on the map.
public float IconSize = 10;
private float _iconHalfSize;
private float _playerMapX;
private float _playerMapZ;
void Awake ()
{
_iconHalfSize = IconSize / 2;
}
void Start ()
{
StartCoroutine (RedrawFog ());
}
void OnGUI ()
{
GUI.Box (new Rect (MapOffSetX, MapOffSetY, MapWidth, MapHeight), MiniMap);
// Текстура с "туманом войны" над текстурой с "миникартой"
GUI.Box (new Rect (MapOffSetX, MapOffSetY, MapWidth, MapHeight), FogOfWar);
float pX = GetMapPos (transform.position.x, MapWidth, SceneWidth);
float pZ = GetMapPos (transform.position.z, MapHeight, SceneHeight);
_playerMapX = pX - _iconHalfSize;
_playerMapZ = ((pZ * -1) - _iconHalfSize) + MapHeight;
// Здесь рисуется иконка игрока на миникарте
GUI.Box (new Rect (_playerMapX, _playerMapZ, IconSize, IconSize), PlayerIcon);
// Здесь я пытаюсь сделать прозрачной текстуру с "туманом войны"
SetPixelsInAria(_playerMapX, _playerMapZ);
}
// Это для того, чтобы закрашивать не один пиксель, а сразу группу
private void SetPixelsInAria(float x, float y) {
for (int i=-25; i<=20; i++) {
//FogOfWar.SetPixel(x+i, y, Color.clear);
for(int j =-25; j<=25; j++) {
FogOfWar.SetPixel((int)(x+i), (int)(y+j), Color.clear);
}
}
}
private float GetMapPos (float pos, float mapSize, float sceneSize)
{
return pos * mapSize / sceneSize;
}
// Применяю изменения к текстуре раз в 3 секунды, чтобы не тормозило сильно
private IEnumerator RedrawFog ()
{
yield return new WaitForSeconds(3);
print ("X: " + (int)_playerMapX + " Z: " + (int)_playerMapZ);
FogOfWar.Apply();
StartCoroutine (RedrawFog ());
}
}
{
//For placing the image of the mini map.
public Texture2D MiniMap;
public Texture2D FogOfWar;
//Two transform variables, one for the player's and the enemy's,
public Transform Player;
public Transform Enemy;
//Icon images for the player and enemy(s) on the map.
public Texture2D PlayerIcon;
public Texture2D EnemyIcon;
//Offset variables (X and Y) - where you want to place your map on screen.
public float MapOffSetX = 0;
public float MapOffSetY = 0;
//The width and height of your map as it'll appear on screen,
public float MapWidth = 200;
public float MapHeight = 200;
//Width and Height of your scene, or the resolution of your terrain.
public float SceneWidth = 500;
public float SceneHeight = 500;
//The size of your player's and enemy's icon on the map.
public float IconSize = 10;
private float _iconHalfSize;
private float _playerMapX;
private float _playerMapZ;
void Awake ()
{
_iconHalfSize = IconSize / 2;
}
void Start ()
{
StartCoroutine (RedrawFog ());
}
void OnGUI ()
{
GUI.Box (new Rect (MapOffSetX, MapOffSetY, MapWidth, MapHeight), MiniMap);
// Текстура с "туманом войны" над текстурой с "миникартой"
GUI.Box (new Rect (MapOffSetX, MapOffSetY, MapWidth, MapHeight), FogOfWar);
float pX = GetMapPos (transform.position.x, MapWidth, SceneWidth);
float pZ = GetMapPos (transform.position.z, MapHeight, SceneHeight);
_playerMapX = pX - _iconHalfSize;
_playerMapZ = ((pZ * -1) - _iconHalfSize) + MapHeight;
// Здесь рисуется иконка игрока на миникарте
GUI.Box (new Rect (_playerMapX, _playerMapZ, IconSize, IconSize), PlayerIcon);
// Здесь я пытаюсь сделать прозрачной текстуру с "туманом войны"
SetPixelsInAria(_playerMapX, _playerMapZ);
}
// Это для того, чтобы закрашивать не один пиксель, а сразу группу
private void SetPixelsInAria(float x, float y) {
for (int i=-25; i<=20; i++) {
//FogOfWar.SetPixel(x+i, y, Color.clear);
for(int j =-25; j<=25; j++) {
FogOfWar.SetPixel((int)(x+i), (int)(y+j), Color.clear);
}
}
}
private float GetMapPos (float pos, float mapSize, float sceneSize)
{
return pos * mapSize / sceneSize;
}
// Применяю изменения к текстуре раз в 3 секунды, чтобы не тормозило сильно
private IEnumerator RedrawFog ()
{
yield return new WaitForSeconds(3);
print ("X: " + (int)_playerMapX + " Z: " + (int)_playerMapZ);
FogOfWar.Apply();
StartCoroutine (RedrawFog ());
}
}
Проблема заключается в том, когда я пытаюсь сделать прозрачной область на миникарте под игроком, то закрашивается совсем другая область на миникарте. Такое ощущение, что эта область как бы раза в 4 меньше самой миникарты и инвертирована по вертикали и горизонтали... В общем, посмотрите на код, может сразу определите, в чем проблема, иначе могу выложить исходники проекта.
Спасибо!