using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.UI.Extensions;
public class MapManager2 : MonoBehaviour
{
public Dictionary<int, Map> dicMap;
public List<int> mapIds;
public Map startMap;
public Map endMap;
public GameObject btnPrefab;
public GameObject linePrefab;
public GameObject Canvas;
public int x = 5;
public int y = 7;
private void Awake()
{
this.dicMap = new Dictionary<int, Map>();
this.startMap = new Map(this.x / 2, 0);
this.endMap = new Map(this.x / 2, this.y + 1);
this.Canvas.GetComponent<RectTransform>().offsetMax = new Vector2(276.6f * this.y, 0);
}
// Start is called before the first frame update
void Start()
{
this.mapInit(this.startMap);
this.startMap.Init(0, this.y);
this.endMap.Init(4, this.y);
this.dicMap.Add(this.startMap.id, this.startMap);
this.dicMap.Add(this.endMap.id, this.endMap);
Debug.Log(this.dicMap.Count);
this.RandomMapSet(Random.Range(1,(this.mapIds.Count / 10) + 2) , Random.Range(1, (this.mapIds.Count / 10) + 2) , Random.Range(1, (this.mapIds.Count / 5) + 1));
foreach (var a in this.dicMap)
{
var btn = Instantiate(this.btnPrefab, this.Canvas.transform);
switch ((int)a.Value.mapKind)
{
case 1:
{
btn.GetComponent<Image>().color = new Color(0, 1, 0, 1);
break;
}
case 2:
{
btn.GetComponent<Image>().color = new Color(0, 0, 1, 1);
break;
}
case 3:
{
btn.GetComponent<Image>().color = new Color(0, 0, 0, 1);
break;
}
case 4:
{
btn.GetComponent<Image>().color = new Color(1, 0, 0, 1);
break;
}
default:
{
btn.GetComponent<Image>().color = new Color(1, 1, 1, 1);
break;
}
}
btn.GetComponent<RectTransform>().localPosition = new Vector3(-this.Canvas.GetComponent<RectTransform>().offsetMax.x + (138.3f * this.y) + (a.Value.y * 276.6f), (-180 * (this.x / 2)) + (180 * a.Value.x), 0);
btn.GetComponent<Button>().onClick.AddListener(() => {
Debug.LogFormat("id : {0} , kind : {1}",a.Value.id,a.Value.mapKind);
foreach(var id in a.Value.nextMapIds)
{
Debug.Log(id);
}
});
}
}
// Update is called once per frame
void Update()
{
}
public void mapInit(Map map)
{
Debug.Log(map.id);
map.next = (x, y, count) => {
switch (count)
{
case 1:
{
var ran = Random.Range(-1, 2);
this.MapCreateOneLine(x, y, ran, map);
break;
}
case 2:
{
List<int> nextXs = new List<int>();
var ran = Random.Range(-1, 2);
switch (ran)
{
case -1:
{
nextXs.Add(0);
nextXs.Add(1);
break;
}
case 0:
{
nextXs.Add(-1);
nextXs.Add(1);
break;
}
case 1:
{
nextXs.Add(-1);
nextXs.Add(0);
break;
}
}
foreach (int a in nextXs)
{
this.MapCreate(x, y, a, map);
}
break;
}
case 3:
{
for (int i = -1; i < 2; i++)
{
this.MapCreate(x, y, i, map);
}
break;
}
}
};
map.nextBoss = (x,y) => {
map.nextMapIds.Add(this.endMap.id);
var line = Instantiate(this.linePrefab, this.Canvas.transform);
line.GetComponent<UILineRenderer>().Points[0] = new Vector2(-this.Canvas.GetComponent<RectTransform>().offsetMax.x + (138.3f * this.y) + (y * 276.6f), (-180 * (this.x / 2)) + (180 * x));
line.GetComponent<UILineRenderer>().Points[1] = new Vector2(-this.Canvas.GetComponent<RectTransform>().offsetMax.x + (138.3f * this.y) + ((this.y + 1) * 276.6f), (-180 * (this.x / 2)) + (180 * (this.x / 2)));
};
}
public void MapCreate(int x, int y, int a , Map map)
{
if (!this.dicMap.ContainsKey(10000 + (100 * (x + a)) + y + 1))
{
if (!((x + a) < 0 || (x + a) > this.x - 1))
{
var newMap = new Map(x + a, y + 1);
this.mapInit(newMap);
newMap.Init(0 , this.y);
dicMap.Add(newMap.id, newMap);
this.mapIds.Add(newMap.id);
}
}
if (!((x + a) < 0 || (x + a) > this.x- 1))
{
//var pos = GameObject.Find("map_" + map.id.ToString());
var line = Instantiate(this.linePrefab, this.Canvas.transform);
line.GetComponent<UILineRenderer>().Points[0] = new Vector2(-this.Canvas.GetComponent<RectTransform>().offsetMax.x + (138.3f * this.y) + (y * 276.6f), (-180 * (this.x / 2)) + (180 * x));
line.GetComponent<UILineRenderer>().Points[1] = new Vector2(-this.Canvas.GetComponent<RectTransform>().offsetMax.x + (138.3f * this.y) + ((y + 1) * 276.6f), (-180 * (this.x / 2)) + (180 * (x + a)));
map.nextMapIds.Add(10000 + (100 * (x + a)) + y + 1);
}
}
public void MapCreateOneLine(int x, int y, int a, Map map)
{
int next = a;
if(((x + next) < 0 || (x + next) > this.x - 1))
{
next *= -1;
}
if (!this.dicMap.ContainsKey(10000 + (100 * (x + next)) + y + 1))
{
if (!((x + next) < 0 || (x + next) > this.x - 1))
{
var newMap = new Map(x + next, y + 1);
this.mapInit(newMap);
newMap.Init(0, this.y);
dicMap.Add(newMap.id, newMap);
this.mapIds.Add(newMap.id);
}
}
if (!((x + next) < 0 || (x + next) > this.x - 1))
{
//var pos = GameObject.Find("map_" + map.id.ToString());
var line = Instantiate(this.linePrefab, this.Canvas.transform);
line.GetComponent<UILineRenderer>().Points[0] = new Vector2(-this.Canvas.GetComponent<RectTransform>().offsetMax.x + (138.3f * this.y) + (y * 276.6f), (-180 * (this.x / 2)) + (180 * x));
line.GetComponent<UILineRenderer>().Points[1] = new Vector2(-this.Canvas.GetComponent<RectTransform>().offsetMax.x + (138.3f * this.y) + ((y + 1) * 276.6f), (-180 * (this.x / 2)) + (180 * (x + next)));
map.nextMapIds.Add(10000 + (100 * (x + next)) + y + 1);
}
}
public int RandomKind()
{
var ran = Random.Range(1, 100);
int kind = 0;
if (ran >= 1 && ran < 65)
{
kind = 0;
}
else if (ran >= 65 && ran < 80)
{
kind = 1;
}
else if (ran >= 80 && ran < 90)
{
kind = 2;
}
else if (ran >= 90 && ran < 100)
{
kind = 3;
}
return kind;
}
public void RandomMapSet(int eliteCnt, int shopCnt, int QuesCnt)
{
for(int i = 0; i < eliteCnt; i++)
{
var ran = Random.Range(0, this.mapIds.Count);
this.dicMap[this.mapIds[ran]].mapKind = eMapKind.elite;
this.mapIds.Remove(this.mapIds[ran]);
}
for (int i = 0; i < shopCnt; i++)
{
var ran = Random.Range(0, this.mapIds.Count);
this.dicMap[this.mapIds[ran]].mapKind = eMapKind.shop;
this.mapIds.Remove(this.mapIds[ran]);
}
for (int i = 0; i < QuesCnt; i++)
{
var ran = Random.Range(0, this.mapIds.Count);
this.dicMap[this.mapIds[ran]].mapKind = eMapKind.question;
this.mapIds.Remove(this.mapIds[ran]);
}
}
}
스크롤뷰를 만들면 중앙이 0이다.

-8298
-7688
-4645.4 x가 30 일때
-3652.6
-4149
-3539
-2709.2 x가 15 일때
-1439.8
어느정도 위치가 애초에 어긋나 있던걸 생각하면, 저 한칸 이동할때 크기인 276.6의 절반인 138.3정도의 비율로 차이가 난다는걸 알 수 있었다.
x 크기에 따라 276.6을 곱해서 스크롤뷰 크기를 동적 생성시키고, 그 동적 생성된 스크롤뷰의 x 길이에 x크기만큼 138.3을 곱해서 위치를 왼쪽으로 땡겨두면 된다.
첫 생성위치를 옮기고 싶을땐 스크롤뷰의 scroll

을 옮기고 싶은만큼 왼쪽으로 "늘린다"
그냥 옮기면 비어있는 부분에선 스크롤이 되지않는다.
'C# > 문제해결' 카테고리의 다른 글
| 0920 타일맵 데이터만들기 (1) | 2022.09.20 |
|---|---|
| 0916 발표용 (0) | 2022.09.16 |
| 0906 DoTween (0) | 2022.09.06 |
| 0830 머신러닝 Ray (0) | 2022.08.30 |
| 0821 등급관리 프로그램 (1) | 2022.08.21 |