Skip to content

Commit 59cd3a3

Browse files
committed
feat(core): add tutorial 39 code
1 parent ef1c31a commit 59cd3a3

8 files changed

Lines changed: 159 additions & 25 deletions

File tree

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using UnityEngine;
2+
using UnityEditor;
3+
4+
public class MinimapCaptureWindow : EditorWindow
5+
{
6+
private Transform _screenshotCameraAnchor;
7+
private Camera _screenshotCamera;
8+
private string _screenshotName = "map";
9+
private float _mapSize = 700;
10+
private float _imgSize = 1080;
11+
12+
[MenuItem("Tools/Minimap Capture")]
13+
static void Init()
14+
{
15+
// get existing open window or make a new one
16+
MinimapCaptureWindow window = (MinimapCaptureWindow)GetWindow(typeof(MinimapCaptureWindow));
17+
// set title and icon
18+
GUIContent titleContent = new GUIContent("Minimap Capture");
19+
window.titleContent = titleContent;
20+
window.Show();
21+
}
22+
23+
private void OnGUI()
24+
{
25+
GUILayout.Label("Minimap Capture", EditorStyles.boldLabel);
26+
27+
_screenshotCameraAnchor = (Transform)EditorGUILayout.ObjectField(
28+
"Camera Anchor", _screenshotCameraAnchor, typeof(Transform), true);
29+
_screenshotCamera = (Camera)EditorGUILayout.ObjectField(
30+
"Camera", _screenshotCamera, typeof(Camera), true);
31+
_screenshotName = EditorGUILayout.TextField("Name", _screenshotName);
32+
_mapSize = EditorGUILayout.FloatField("Map Size", _mapSize);
33+
_imgSize = EditorGUILayout.FloatField("Image Size", _imgSize);
34+
35+
EditorGUILayout.Space();
36+
37+
EditorGUI.BeginDisabledGroup(_screenshotCameraAnchor == null || _screenshotCamera == null);
38+
if (GUILayout.Button("Take Screenshot"))
39+
{
40+
Vector3 prevAnchorPos = _screenshotCameraAnchor.position;
41+
float prevOrthoSize = _screenshotCamera.orthographicSize;
42+
43+
float t = _mapSize / 2;
44+
_screenshotCameraAnchor.position = new Vector3(t, 0, t);
45+
_screenshotCamera.orthographicSize = t;
46+
47+
_screenshotCameraAnchor.position = prevAnchorPos;
48+
_screenshotCamera.orthographicSize = prevOrthoSize;
49+
50+
MinimapCapture.TakeScreenshot(
51+
_screenshotName,
52+
new Vector2Int((int)_imgSize, (int)_imgSize),
53+
_screenshotCamera);
54+
}
55+
EditorGUI.EndDisabledGroup();
56+
}
57+
58+
}

Assets/Editor/MinimapCaptureEditor.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Scripts/FogOfWar/FogRendererToggler.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
public class FogRendererToggler : MonoBehaviour
44
{
5-
public Renderer myRenderer; // reference to the render you want toggled based on the position of this transform
5+
public GameObject mesh; // reference to the render you want toggled based on the position of this transform
66
[Range(0f, 1f)] public float threshold = 0.1f; //the threshold for when this script considers myRenderer should render
77

88
private Camera _camera; //The Camera using the masked render texture
@@ -19,7 +19,14 @@ private void Awake()
1919
this.enabled = false;
2020
return;
2121
}
22-
22+
23+
// disable if no mesh is defined
24+
if (!mesh)
25+
{
26+
this.enabled = false;
27+
return;
28+
}
29+
2330
_camera = GameObject.Find("UnexploredAreasCam").GetComponent<Camera>();
2431
}
2532

@@ -63,12 +70,8 @@ private void Update()
6370

6471
void LateUpdate()
6572
{
66-
if (!myRenderer)
67-
{
68-
this.enabled = false;
69-
return;
70-
}
71-
72-
myRenderer.enabled = GetColorAtPosition().grayscale >= threshold;
73+
bool active = GetColorAtPosition().grayscale >= threshold;
74+
if (mesh.activeSelf != active)
75+
mesh.SetActive(active);
7376
}
7477
}

Assets/Scripts/Managers/CameraManager.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections;
33
using UnityEngine;
44

@@ -10,11 +10,13 @@ public class CameraManager : MonoBehaviour
1010
public float altitude = 40f;
1111

1212
public Transform groundTarget;
13+
public bool autoAdaptAltitude;
1314

1415
private Camera _camera;
1516
private RaycastHit _hit;
1617
private Ray _ray;
1718

19+
private float _distance = 100f;
1820
private Vector3 _forwardDir;
1921
private Coroutine _mouseOnScreenCoroutine;
2022
private int _mouseOnScreenBorder;
@@ -114,10 +116,10 @@ private void _OnMoveCamera(object data)
114116
pos.z -= indicatorH / 2f;
115117
Vector3 off = transform.position - Utils.MiddleOfScreenPointToWorld();
116118
Vector3 newPos = pos + off;
117-
newPos.y = 100f;
118119
transform.position = newPos;
119120

120-
_FixAltitude();
121+
if (autoAdaptAltitude)
122+
_FixAltitude();
121123
_ComputeMinimapIndicator(false);
122124
}
123125

@@ -132,7 +134,8 @@ private void _TranslateCamera(int dir)
132134
else if (dir == 3) // left
133135
transform.Translate(-transform.right * Time.deltaTime * translationSpeed);
134136

135-
_FixAltitude();
137+
if (autoAdaptAltitude)
138+
_FixAltitude();
136139
_ComputeMinimapIndicator(false);
137140
}
138141

@@ -225,4 +228,10 @@ private void _ComputeMinimapIndicator(bool zooming)
225228
// move the game object at the center of the main camera screen
226229
_minimapIndicator.position = middle;
227230
}
231+
232+
public void SetPosition(Vector3 pos)
233+
{
234+
transform.position = pos - _distance * transform.forward;
235+
_ComputeMinimapIndicator(false);
236+
}
228237
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using UnityEngine;
2+
3+
public static class MinimapCapture
4+
{
5+
public static void TakeScreenshot(string name, Vector2Int size, Camera camera)
6+
{
7+
RenderTexture prevRt = camera.targetTexture;
8+
9+
// prepare render
10+
RenderTexture rt = new RenderTexture(size.x, size.y, 24, RenderTextureFormat.ARGB32);
11+
rt.antiAliasing = 4;
12+
13+
camera.targetTexture = rt;
14+
camera.Render();
15+
16+
// transfer to Texture2D and bytes
17+
Texture2D output = new Texture2D(size.x, size.y, TextureFormat.RGB24, false);
18+
19+
RenderTexture.active = rt;
20+
output.ReadPixels(new Rect(0, 0, size.x, size.y), 0, 0, false);
21+
22+
byte[] bytes = output.EncodeToJPG(90);
23+
Object.DestroyImmediate(output);
24+
25+
// For testing purposes, also write to a file in the project folder
26+
string filePath = Application.dataPath + $"/{name}.jpg";
27+
System.IO.File.WriteAllBytes(filePath, bytes);
28+
29+
// clean
30+
RenderTexture.active = null;
31+
camera.targetTexture = prevRt;
32+
rt.DiscardContents();
33+
34+
Debug.Log($"Took screenshot: {filePath}");
35+
}
36+
}

Assets/Scripts/Tools/MinimapCapture.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Scripts/Units/Buildings/BuildingPlacer.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@ private void Start()
1818
{
1919
instance = this;
2020

21-
// instantiate our headquarters
22-
SpawnBuilding(
23-
GameManager.instance.gameGlobalParameters.initialBuilding,
24-
GameManager.instance.gamePlayersParameters.myPlayerId,
25-
GameManager.instance.startPosition
26-
);
27-
//// instantiate our headquarters
28-
//SpawnBuilding(
29-
// GameManager.instance.gameGlobalParameters.initialBuilding,
30-
// 1 - GameManager.instance.gamePlayersParameters.myPlayerId,
31-
// GameManager.instance.startPosition - Vector3.right * 32f
32-
//);
21+
Transform spawnpoints = GameObject.Find("Spawnpoints").transform;
22+
23+
BuildingData initialBuilding = GameManager.instance.gameGlobalParameters.initialBuilding;
24+
GamePlayersParameters p = GameManager.instance.gamePlayersParameters;
25+
Vector3 pos;
26+
for (int i = 0; i < p.players.Length; i++)
27+
{
28+
pos = spawnpoints.GetChild(i).position;
29+
SpawnBuilding(initialBuilding, i, pos);
30+
if (i == p.myPlayerId)
31+
Camera.main.GetComponent<CameraManager>().SetPosition(pos);
32+
}
3333
}
3434

3535
void Update()

Assets/Scripts/Units/Unit.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ public Unit(UnitData data, int owner, List<ResourceValue> production)
6969

7070
_transform.GetComponent<UnitManager>().Initialize(this);
7171

72+
// setup minimap icon color with owner color
73+
GamePlayersParameters p = GameManager.instance.gamePlayersParameters;
74+
Color c = p.players[owner].color;
75+
Transform minimapIcon = _transform.Find("Mesh/MinimapIcon");
76+
minimapIcon.GetComponent<Renderer>().material.color = c;
77+
7278
// prepare data for upgrade to next level
7379
_levelUpData = _GetLevelUpData();
7480
}

0 commit comments

Comments
 (0)