Skip to content

Commit

Permalink
Updates minimap snapshooting and preparing for autoload
Browse files Browse the repository at this point in the history
+ some comments
  • Loading branch information
stefanahman committed Aug 20, 2014
1 parent 0d88b6d commit 8566cc8
Show file tree
Hide file tree
Showing 31 changed files with 568 additions and 882 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
sysinfo.txt

# OS X
/*.DS_Store
**/*.DS_Store

# Windows
/*.db
**/*.db
68 changes: 41 additions & 27 deletions Assets/Editor/MinimapTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,89 +7,103 @@ public class Utils : MonoBehaviour {

[MenuItem("Minimap/Create Map Planes From Selection")]
private static void CreateMapPlanesFromSelection() {
var mapSettings = new MyMinimap.MapSettings ("Assets/Game/HUD/Code/Minimap/MinimapSettings.txt");

// Get minimap data from file
var settingsData = AssetDatabase.LoadMainAssetAtPath("Assets/Game/HUD/Code/Minimap/MinimapSettings.txt") as TextAsset;
var mapSettings = new MapSettings(settingsData.text);

var length = mapSettings.length;
var width = mapSettings.width;

var mesh = CreatePlanesMesh (length, width);
var mesh = CreatePlanesMesh(length, width);

// Create missing folders
EditorHelper.CreateAssetFolderIfNotExists("Game/HUD/Models/Minimap");
EditorHelper.CreateAssetFolderIfNotExists("Game/HUD/Materials/Minimap");
EditorHelper.CreateAssetFolderIfNotExists("Game/HUD/Prefabs/Minimap");

EditorHelper.CreateAssetFolderIfNotExists ("Game/HUD/Models/Minimap");
AssetDatabase.CreateAsset (mesh, "Assets/Game/HUD/Models/Minimap/MinimapSegment.mesh");
// Create minimap plane mesh
AssetDatabase.CreateAsset(mesh, "Assets/Game/HUD/Models/Minimap/MinimapSegment.mesh");

var selection = Selection.GetFiltered (typeof(Texture), SelectionMode.DeepAssets);
// Get all files from selection
var selection = Selection.GetFiltered(typeof(Texture), SelectionMode.DeepAssets);
if (selection != null) {
for (int i = 0; i < selection.Length; i++) {
// Parse texture
var texture = selection[i] as Texture;
if(texture != null) {
if (texture != null) {
var mat = CreateMaterial(texture, texture.name);

EditorHelper.CreateAssetFolderIfNotExists ("Game/HUD/Materials/Minimap");
AssetDatabase.CreateAsset (mat, string.Format("Assets/Game/HUD/Materials/Minimap/{0}.mat", texture.name));

EditorHelper.CreateAssetFolderIfNotExists ("Game/HUD/Prefabs/Minimap");
var prefab = PrefabUtility.CreatePrefab(string.Format("Assets/Game/HUD/Prefabs/Minimap/{0}.prefab", texture.name), CreatePrefab (mesh, texture.name));
// Create minimap plane material
AssetDatabase.CreateAsset(mat, string.Format("Assets/Game/HUD/Materials/Minimap/{0}.mat", texture.name));

// Create minimap prefab
var prefab = PrefabUtility.CreatePrefab(string.Format("Assets/Game/HUD/Prefabs/Minimap/{0}.prefab", texture.name), CreatePrefab(mesh, texture.name));

// Set prefab properties
prefab.renderer.sharedMaterial = mat;
prefab.GetComponent<MeshFilter>().sharedMesh = mesh;
prefab.GetComponent<MeshRenderer>().castShadows = false;
prefab.GetComponent<MeshRenderer>().receiveShadows = false;

}
}
AssetDatabase.Refresh();
}

}

private static GameObject CreatePrefab (Mesh mesh, string name)
// Creates prefab
private static GameObject CreatePrefab(Mesh mesh, string name)
{
var go = new GameObject (name);
go.AddComponent ("MeshFilter");
go.AddComponent ("MeshRenderer");
go.transform.Rotate (new Vector3 (1, 0, 0), 90);
return go;
var gameObject = new GameObject(name);
gameObject.AddComponent("MeshFilter");
gameObject.AddComponent("MeshRenderer");
gameObject.transform.Rotate(new Vector3(1, 0, 0), 90);
return gameObject;
}

private static Material CreateMaterial (Texture texture, string name)

// Creates material
private static Material CreateMaterial(Texture texture, string name)
{
var material = new Material (Shader.Find ("Unlit/Texture"));

var material = new Material(Shader.Find("Unlit/Texture"));
material.mainTexture = texture;
material.name = name;

return material;
}


// Creates a low-ploy plane mesh
private static Mesh CreatePlanesMesh(int length, int width) {
var mesh = new Mesh ();
var mesh = new Mesh();
mesh.name = "low-poly-mesh";

mesh.vertices = new Vector3[6] {
new Vector3 (0, 0, 0),
new Vector3 (0, width, 0),
new Vector3 (length, width),
new Vector3 (length, width),
new Vector3 (length, 0, 0),
new Vector3 (0, 0, 0)
};
};
mesh.uv = new Vector2[6] {
new Vector2 (0, 0),
new Vector2 (0, 1),
new Vector2 (1, 1),
new Vector2 (1, 1),
new Vector2 (1, 0),
new Vector2 (0, 0)
};
};
mesh.triangles = new int[6] {0, 1, 2, 3, 4, 5};
return mesh;
}

[MenuItem("Minimap/Export Selection to Map Asset")]
private static void SaveSelectionToMapAsset() {
// Get all files from selection
var selection = Selection.GetFiltered (typeof(GameObject), SelectionMode.DeepAssets);
if (selection != null) {
var path = EditorUtility.SaveFilePanel("Save Asset", "", "New Data", "dat");
if(!string.IsNullOrEmpty(path)) {
// Get minimap data from file
var settingsAsset = (TextAsset) AssetDatabase.LoadAssetAtPath("Assets/Game/HUD/Code/Minimap/MinimapSettings.txt", typeof(TextAsset));
if(settingsAsset == null) {
Debug.LogError("Settings file not found");
Expand Down
5 changes: 5 additions & 0 deletions Assets/Game/Common.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Assets/Game/Common/Code.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Assets/Game/Common/Code/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using UnityEngine;
using System.Collections;

public class Constants {

// Paths
public static string PATH_GAME = "Assets/Game/";

}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 0 additions & 30 deletions Assets/Game/GameModes/Code/GameModeConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,5 @@

public class GameModeConstants {

//
//Keys
public static string KEY_CAMERA = "Camera";
public static string KEY_TAB = "Tab";
public static string KEY_MAP = "Map";
public static string KEY_SKILL_ONE = "Skill1";
public static string KEY_SKILL_TWO = "Skill2";
public static string KEY_SKILL_THREE = "Skill3";
public static string KEY_SKILL_ULTIMATE = "Skill4";
public static string KEY_MAIN_FIRE = "Fire1";


public static string OBJECT_SESSION_NAME = "Session";

public static string TAG_GAME_MANAGER = "GameManager";
public static string TAG_FLAG = "Flag";
public static string TAG_MINIMAP_FLAG_NAVIGATION = "MinimapFlagNavigation";
public static string TAG_FLAG_PLATFORM = "FlagPlatform";
public static string TAG_MAIN_CAMERA = "MainCamera";
public static string TAG_SECONDARY_CAMERA = "SecondaryCamera";
public static string TAG_MINIMAP_CAMERA = "MinimapCamera";
public static string TAG_MINIMAP_MASK = "MinimapMask";
public static string TAG_SELF = "Self";
public static string TAG_SPAWN_POINT = "SpawnPoints";


// Room properties (Photon)
public static string RedScore = "PropertyRedScore";
public static string BlueScore = "PropertyBlueScore";
public static string StartTime = "PropertyStartTime";

}
12 changes: 12 additions & 0 deletions Assets/Game/HUD/Code/HUDConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,16 @@ public class HUDConstants {
public static string TAG_MINIMAP_CAMERA = "MinimapCamera";
public static string TAG_MINIMAP_MASK = "MinimapMask";

public static string PATH_HUD = Constants.PATH_GAME + "/HUD";

// Paths
public static string PATH_MINIMAP_SETTINGS = PATH_HUD + "/Code/Minimap/MinimapSettings.txt";
public static string PATH_MINIMAP_MATERIALS = PATH_HUD + "/Materials/Minimap";
public static string PATH_MINIMAP_MODELS = PATH_HUD + "/Models/Minimap";
public static string PATH_MINIMAP_PREFABS = PATH_HUD + "/Prefabs/Minimap";
public static string PATH_MINIMAP_TEXTURES = PATH_HUD + "/Textures/Minimap";

// Minimap
public static string MINIMAP_SEGMENT_PATTERN = "{0}-{1}.{2}";

}
108 changes: 74 additions & 34 deletions Assets/Game/HUD/Code/Minimap/AutoScreenshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,80 +4,120 @@

public class AutoScreenshot : MonoBehaviour {

public float SnapDelay = 1.0f;
public float SnapDelay = 0.1f;
public string SegmentName = "segment";
public float Aspect = 1.0f;
public int SuperSize = 2;
private float mapMaxX;
private float mapMaxZ;
private float xInc;
private float zInc;

IEnumerator Start () {
IEnumerator Start() {
if (SnapDelay < 0.1f) {
this.SnapDelay = 1.0f;
Debug.LogWarning("Snap delay cannot be lower than 1 second.");
}

camera.enabled = true;
// Make sure that the game is not active
GameObject game = GameObject.FindGameObjectWithTag("Game");
game.SetActive(false);

// Make sure all other cameras are disabled
var cameras = Camera.allCameras;
var activeCameras = 0;
for (int i = 0; i < cameras.Length; i++) {
if(cameras[i].enabled) activeCameras++;
if(activeCameras > 1) {
Debug.LogError("Disable all other cameras before running AutoSnapshot");
yield break;
if(cameras[i].enabled) {
camera.enabled = false;
//Debug.LogError("Disable all other cameras before running AutoSnapshot");
//yield break;
}
}

// TODO: Disable all unwanted environment

// Create destination folder if not exists
EditorHelper.CreateAssetFolderIfNotExists("Game/HUD/Textures/Minimap");

// Setup minimap camera
camera.enabled = true;
camera.aspect = Aspect;

var xHalfUnit = camera.orthographicSize * Aspect;
var zHalfUnit = camera.orthographicSize;

this.moveCamera(xHalfUnit, zHalfUnit);
var xInc = xHalfUnit * 2;
var zInc = zHalfUnit * 2;
xInc = xHalfUnit * 2;
zInc = zHalfUnit * 2;

// Center 0,0,0
var xTerrainMax = 50;//Terrain.activeTerrain.terrainData.size.x/2;
var zTerrainMax = 50;//Terrain.activeTerrain.terrainData.size.z/2;
Debug.Log ("(" + xTerrainMax + "," + zTerrainMax + ")");
TerrainData terrainData = Terrain.activeTerrain.terrainData;
mapMaxX = terrainData.size.x / 2;
mapMaxZ = terrainData.size.z / 2;

EditorHelper.CreateAssetFolderIfNotExists("Game/HUD/Textures/Minimap");
int xImage = 0;
int zImage;
for (float x = xTerrainMax*-1; x < xTerrainMax + xHalfUnit; x += xInc) {
zImage = 0;
for (float z = xTerrainMax*-1; z < zTerrainMax + zHalfUnit; z += zInc) {
for (float x = mapMaxX * -1; x < mapMaxX + xHalfUnit; x += xInc) {
for (float z = mapMaxZ * -1; z < mapMaxZ + zHalfUnit; z += zInc) {
this.moveCamera(x, z);
Application.CaptureScreenshot(string.Format("Assets/Game/HUD/Textures/Minimap/{0}-{1}.{2}.png", x, z, SegmentName));
//var coords = GetSegmentCoorsForPosition(x, z);
//Debug.Log(coords);
Application.CaptureScreenshot(string.Format(HUDConstants.PATH_MINIMAP_TEXTURES + "/" + HUDConstants.MINIMAP_SEGMENT_PATTERN + ".png", x, z, SegmentName));
yield return new WaitForSeconds(SnapDelay);
zImage++;
}
xImage++;
}

using (var writer = new StreamWriter("Assets/Game/HUD/Code/Minimap/MinimapSettings.txt")) {
// Write map data
using (var writer = new StreamWriter(HUDConstants.PATH_MINIMAP_SETTINGS)) {
writer.WriteLine(string.Format("name={0}", SegmentName));
writer.WriteLine(string.Format("length={0}", xInc));
writer.WriteLine(string.Format("width={0}", zInc));
writer.WriteLine(string.Format("xMin={0}", xTerrainMax*-1));
writer.WriteLine(string.Format("xMax={0}", xTerrainMax));
writer.WriteLine(string.Format("zMin={0}", zTerrainMax*-1));
writer.WriteLine(string.Format("zMax={0}", zTerrainMax));
writer.WriteLine(string.Format("xMin={0}", mapMaxX*-1));
writer.WriteLine(string.Format("xMax={0}", mapMaxX));
writer.WriteLine(string.Format("zMin={0}", mapMaxZ*-1));
writer.WriteLine(string.Format("zMax={0}", mapMaxZ));
}

// Disable camera
camera.enabled = false;
}

void Update () {

// Gets the "upper" corner-coordinate of the closets segment
private Vector2 GetSegmentCoorsForPosition(float x, float z)
{
// Get distance from top, xMin = -xMax
var distOrigoBottom = mapMaxX + x;

// Get distance from left, zMin = -zMax
var distOrigoLeft = mapMaxZ + z;


//Debug.Log("GetSegmentCoorsForPosition: (" + x + "," + z + ")");
//Debug.Log("Origo coords: (" + distOrigoBottom + "," + distOrigoLeft + ")");
/*float offX = mapOffset.x, offZ = mapOffset.z;
if (x < 0)
offX *= -1;
if (z < 0)
offZ *= -1;
x = Mathf.Clamp(x + offX, mapSettings.xMin, mapSettings.xMax);
z = Mathf.Clamp(z + offZ, mapSettings.xMin, mapSettings.xMax);
Debug.Log("GetSegmentCoorsForPosition: (" + x + "," + z + ")");
*/
var segmentX = (int) (distOrigoBottom / xInc);
var segmentZ = (int) (distOrigoLeft / zInc);
var newX = segmentX * xInc;
var newZ = segmentZ * zInc;
//Debug.Log("New-GetSegmentCoorsForPosition: (" + newX + "," + newZ + ")");
return new Vector2(newX, newZ);
}

// Move camera to (x, z)
private void moveCamera(float x, float z) {
this.transform.position = new Vector3(RoundToNearestPixel(x, camera), this.transform.position.y, RoundToNearestPixel(z, camera));
}


// Round a position to not overlap in pixels (TODO: Not working)
private float RoundToNearestPixel(float unityUnits, Camera viewingCamera)
{
float valueInPixels = (Screen.height / (viewingCamera.orthographicSize * Aspect)) * unityUnits;
float valueInPixels = (Screen.height / (viewingCamera.orthographicSize * 2)) * unityUnits;
valueInPixels = Mathf.Round(valueInPixels);
float adjustedUnityUnits = valueInPixels / (Screen.height / (viewingCamera.orthographicSize * Aspect));
float adjustedUnityUnits = valueInPixels / (Screen.height / (viewingCamera.orthographicSize * 2));
return adjustedUnityUnits;
}
}
2 changes: 1 addition & 1 deletion Assets/Game/HUD/Code/Minimap/EditorHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public static class EditorHelper
{
public static void CreateAssetFolderIfNotExists(string path) {
var fullPath = string.Format ("{0}/{1}", Application.dataPath, path);
if (!Directory.Exists (fullPath)) {
if (!Directory.Exists(fullPath)) {
Directory.CreateDirectory(fullPath);
}
}
Expand Down
12 changes: 0 additions & 12 deletions Assets/Game/HUD/Code/Minimap/Helper.cs

This file was deleted.

Loading

0 comments on commit 8566cc8

Please sign in to comment.