본문 바로가기

프로그래밍/Unity

미니맵 참고 사항

미니맵 코드  정리 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using App;
using UnityEngine.SceneManagement;

namespace RedDot
{
    public partial class MiniMap : GuiObject //무기 주워서 변경
    {
        GameObject m_StartPoint = null;
        GameObject m_EndPoint = null;

        public GameObject [] m_Players;
        Image m_Image = null;
        RectTransform m_RectTranceFrom = null;
        RectTransform m_RectPlayerIcon = null;
        RectTransform m_RectPlayerBack = null;

        float m_fScale;
        Vector3 m_vPointLocal;
        Vector3 m_angles;
        Vector2 m_Point2D;
        Vector3 m_Mapsize;

        enum UI
        {
            SampleMap,
            MiniMap,
            PlayerBack,
            local,
        }
        void Awake()
        {
            GameObject[UI.SampleMap].SetActive(true);
            GameObject[UI.PlayerBack].SetActive(true);

            m_Image = GameObject[UI.MiniMap].GetComponent<Image>();
            m_RectTranceFrom = GameObject[UI.MiniMap].GetComponent<RectTransform>();



            //m_RectPlayerIcon = GameObject[UI.local].GetComponent<RectTransform>();
            //m_RectPlayerBack = GameObject[UI.PlayerBack].GetComponent<RectTransform>();
            m_Players = new GameObject[16];

            GameObject _Player = GameObject[UI.PlayerBack];
            m_RectPlayerBack = _Player.GetComponent<RectTransform>();
            m_RectPlayerIcon = _Player.transform.Find("local").GetComponent<RectTransform>();

            m_Players[0] = _Player;

            for (int i = 1; i < 16; i++)
            {
                m_Players[i] = AssetObject.CreateGameObject(_Player, GameObject[UI.SampleMap].transform);
                m_Players[i].SetActive(false);
            }

            m_fScale = 0.7f;
        }

        public Vector3 UpdateMinMaxPos(BasePlayer pPlayer)
        {
            Transform LocalTrance = new GameObject().GetComponent<Transform>();
            float degress = PlayerManager.Instance.localPlayer.transform.eulerAngles.y;
            Vector3 TargetPos = PlayerManager.Instance.localPlayer.transform.position;
            Vector3 LocalPos = pPlayer.transform.position;
            LocalTrance.position = LocalPos - TargetPos;
            Vector2 pos2d = new Vector2(LocalTrance.position.x, LocalTrance.position.z);
            pos2d = Quaternion.Euler(0, 0, degress) * pos2d;
            Consoles.LogFormat("pos2d : {0}", pos2d);

            if (pPlayer.data.team == TeamTypes.RedTeam)
            {
                Vector2 vMaxPos = new Vector2(40, 40);
                Vector2 vMinPos = new Vector2(-40, -40);
                pos2d.x = Mathf.Clamp(pos2d.x, vMinPos.x, vMaxPos.x);
                pos2d.y = Mathf.Clamp(pos2d.y, vMinPos.y, vMaxPos.y);
            }
            else
            {
                Vector2 vMaxPos = new Vector2(400, 400);
                Vector2 vMinPos = new Vector2(-400, -400);
                pos2d.x = Mathf.Clamp(pos2d.x, vMinPos.x, vMaxPos.x);
                pos2d.y = Mathf.Clamp(pos2d.y, vMinPos.y, vMaxPos.y);
            }

            //pos2d = Quaternion.Euler(0, 0, degress) * pos2d;
            LocalPos.x = pos2d.x;
            LocalPos.z = pos2d.y;

            return LocalPos;
        }

        public void PlayerUpdate(int index, BasePlayer pPlayer)
        {
            if (pPlayer != null)
            {
                Image _Image = m_Players[index].transform.Find("local").GetComponent<Image>();
                RectTransform _RectPlayerBack = m_Players[index].GetComponent<RectTransform>();
                RectTransform _RectPlayerIcon = m_Players[index].transform.Find("local").GetComponent<RectTransform>();
                _RectPlayerBack.pivot = new Vector2(0.5f, 0.5f);

                float Targetdegress = PlayerManager.Instance.localPlayer.transform.eulerAngles.y;
                float Playerdegress = pPlayer.transform.eulerAngles.y;
                Vector3 _angles = _RectPlayerIcon.eulerAngles;
                _angles.z = Targetdegress - Playerdegress + 90f;
                _RectPlayerIcon.eulerAngles = _angles;

                Vector3 WorldPos = UpdateMinMaxPos(pPlayer);
                Vector3 MiniMapPos = _RectPlayerIcon.transform.localPosition;
                MiniMapPos.x = WorldPos.x / m_Mapsize.x * 512;
                MiniMapPos.y = WorldPos.z / m_Mapsize.z * 512;
                _RectPlayerIcon.transform.localPosition = MiniMapPos;

                m_Players[index].SetActive(true);
                _RectPlayerIcon.transform.localScale = new Vector3(m_fScale, m_fScale, m_fScale);
                _RectPlayerBack.transform.localScale = new Vector3(m_fScale, m_fScale, m_fScale);

                //if (pPlayer.data.team == TeamTypes.RedTeam)
                //{
                //    Icons.Instance.LoadSprite(_Image, "Assets/RedDot/AssetBundles/Source/GUI/common/common_icon_put.png");
                //}
                //else
                //    Icons.Instance.LoadSprite(_Image, "Assets/RedDot/AssetBundles/Source/GUI/common/common_icon_put_arrow.png");
            }
        }

        public void UpdateMiniMap()
        {
            Scene scene = SceneManager.GetSceneAt(1);
            if (scene.isLoaded == false)
                return;

            foreach (GameObject root in scene.GetRootGameObjects())
            {
                if (root.name == "StartPoint")
                {
                    m_StartPoint = root;
                }
                if (root.name == "EndPoint")
                {
                    m_EndPoint = root;
                }
            }

            if (m_StartPoint != null)
            {
                Vector3 LocalPos = PlayerManager.Instance.localPlayer.transform.position;

                m_Mapsize = m_StartPoint.transform.position - m_EndPoint.transform.position;
                Vector3 CurPoint = m_StartPoint.transform.position - LocalPos;
                m_Point2D.x = CurPoint.x / m_Mapsize.x;
                m_Point2D.y = CurPoint.z / m_Mapsize.z;

                m_angles = m_RectTranceFrom.eulerAngles;
                m_angles.z = PlayerManager.Instance.localPlayer.transform.eulerAngles.y + 180;

                m_RectTranceFrom.pivot = m_Point2D;
                m_RectTranceFrom.eulerAngles = m_angles;
                m_RectTranceFrom.transform.localScale = new Vector3(m_fScale, m_fScale, m_fScale);

                int iCount = 0;
                PlayerUpdate(iCount++, PlayerManager.Instance.localPlayer);
                for (int i = 0; i < PlayerManager.Instance.remotePlayers.Count; ++i)
                {
                    if(iCount < 16)
                    {
                        PlayerUpdate(iCount++, PlayerManager.Instance.remotePlayers[i]);
                    }
                }
            }
        }
    }
}

 

다소 정리 되지 않은 테스트 코드 

 

void Awake()

 게임 오브젝트 생성및 필요 이미지 변수들 활당 

 

public Vector3 UpdateMinMaxPos(BasePlayer pPlayer)

  월드 포스를 얻는 함수  현재 자기 자신으로 부터 위치 포지션으로 변경

 

public void PlayerUpdate(int index, BasePlayer pPlayer)

  인덱스 와 베이스 캐릭터데이터를 가지고 비율 계산과 각도 계산한 월드 포스를 가지고 

  미니맵 로컬 포지션으로 변경하여 위치 변경

 

public void UpdateMiniMap()

  각종 함수에서 미니맵 데이터 변경 점이 있을경우 위치 포지션을 업데이트 해줌(이동, 죽음등)