2022년 11월 16일 수요일

실수로 git pull 햇을때 로컬 되살리기

 



>[!warning] # 무조건 지켜야 하는 법칙(로컬을 살리기위해,협업을 위해)

> git pull 을 하기전에 

> # **stash** 명령어로 로컬 저장!!

> - 반드시  를 해서 local의 변경사항을 저장하자!!!

> 하지 않는다면 되돌릴수없는 강을 건너게 된다...



2022년 11월 2일 수요일

Unity UI On GameObjectPostion

 




void DrawTest()
    {

        // Offset position above object bbox (in world space)
        float offsetPosY = target.transform.position.y;

        // Final position of marker above GO in world space
        Vector3 offsetPos = new Vector3(target.transform.position.x, offsetPosY, target.transform.position.z);

        // Calculate *screen* position (note, not a canvas/recttransform position)
        Vector2 canvasPos;
        Vector2 screenPoint = Camera.main.WorldToScreenPoint(offsetPos);

        // Convert screen position to Canvas / RectTransform space <- leave camera null if Screen Space Overlay
        RectTransformUtility.ScreenPointToLocalPointInRectangle(MainCanvasRect, screenPoint, null, out canvasPos);

        // Set
        imageRectTransform.localPosition = canvasPos;
    }


DrawLine

 




  // Offset position above object bbox (in world space)
  float offsetPosY = target.transform.position.y + 1.5f;
 
  // Final position of marker above GO in world space
  Vector3 offsetPos = new Vector3(target.transform.position.x, offsetPosY, target.transform.position.z);
 
  // Calculate *screen* position (note, not a canvas/recttransform position)
  Vector2 canvasPos;
  Vector2 screenPoint = Camera.main.WorldToScreenPoint(offsetPos);
 
  // Convert screen position to Canvas / RectTransform space <- leave camera null if Screen Space Overlay
  RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRect, screenPoint, null, out canvasPos);
 
  // Set
  markerRtra.localPosition = canvasPos;



2022년 10월 27일 목요일

ml agent unity 초기 설정방법(anaconda,Window)



1. Unity 설치 (2020.3이상)


  


2. anaconda 설치


  


3.가상환경 만들기


  

- conda create -n MLAgent python =3.7.9


  

  


4. 해당 가상환경에 pytorch설치


## pip3 install torch~=1.7.1 -f https://download.pytorch.org/whl/torch_stable.html


  





![[Pasted image 20221030163156.png]]




5. 해당 가상환경에 mlagent 설치


## python -m pip install mlagents==0.28.0


![[Pasted image 20221030163252.png]]


2022년 10월 26일 수요일

unity 2d sprite animation

 


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

[Serializable]
public struct SpriteInfo
{
    public Image sourceImage;
    public Sprite[] textureArray;
    public float frameRate;
   
}

public class SpriteAnimation : MonoBehaviour
{

    [SerializeField] SpriteInfo s_Info;


    [SerializeField] float frameTimer;
    [SerializeField] int currentFrame;
    [SerializeField] int frameCount;



    // Start is called before the first frame update
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown("1"))
        {
            StartCoroutine(StartSprite(s_Info));
        }
    }



    IEnumerator StartSprite(SpriteInfo info)
    {
        frameTimer = info.frameRate;
        frameCount = info.textureArray.Length;

        //Rect rect = new Rect(0, 0, info.textureArray[0].width, info.textureArray[0].height);

        while (true)
        {
            frameTimer -= Time.deltaTime;
            if (frameTimer <= 0f)
            {
                frameTimer += info.frameRate;
                currentFrame = (currentFrame + 1) % frameCount;
                info.sourceImage.sprite = info.textureArray[currentFrame];
                Debug.Log(info.sourceImage.sprite.name);
                //info.sourceImage.sprite = Sprite.Create(info.textureArray[currentFrame],rect,Vector2.zero);
            }
            yield return new WaitForEndOfFrame();
        }
    }


}






2022년 10월 20일 목요일

TryGetComponent Unity

 



using UnityEngine;

public class TryGetComponentExample : MonoBehaviour
{
    void Start()
    {
        if (TryGetComponent(out HingeJoint hinge))
        {
            hinge.useSpring = false;
        }
    }
}



2022년 10월 19일 수요일

Unity animation use Slider UI

 


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

public class AnimationTruck : MonoBehaviour
{
    public Animation ani;
    public string clip_name;
    public float ani_time;

    [Header("Animation Slider")]
    [SerializeField] Slider AnimationSlider;
    [SerializeField] float speed;

    private void Start()
    {
        Hide();
        if (AnimationSlider != null)
        {
            AnimationSlider.maxValue = ani[clip_name].length;

           

            AnimationSlider.onValueChanged.AddListener(delegate
            { OnChangeAnimationBySlider(AnimationSlider);
            });

        }
    }



    void OnChangeAnimationBySlider(Slider slider)
    {
        if (!ani.isPlaying)
        {
            ani.Play();
            ani[clip_name].speed = 0;
        }

        //ani[clip_name].speed = speed;
        ani[clip_name].time = slider.value;

    }


    public void ToggleAnimation(Toggle m_toggle)
    {
        if (m_toggle.isOn)
        {
            Show();
        }
        else
        {
            Hide();
        }
    }

    private void Update()
    {
        if (AnimationSlider == null)
                return;
        
            if (AnimationSlider.value != ani[clip_name].time)
            {
                AnimationSlider.value =  ani[clip_name].time;
            }
    }

    public void Hide()
    {
        //if (ani_time == ani[clip_name].length)
        //    return;

        ani_time = ani[clip_name].time;

        func_playAni(ani_time, clip_name);

    }
    public void Show()
    {

        if (ani_time == 0)
            ani_time = ani[clip_name].length;
        else
            ani_time = ani[clip_name].time;


        func_reversPlay(ani_time, clip_name);

    }


    public void func_playAni(float time, string aniName)
    {

        ani[aniName].time = time;
        ani[aniName].speed = speed;
        ani.Play();

    }

    public void func_reversPlay(float time, string aniName)
    {
        ani[aniName].time = time;
        ani[aniName].speed = -speed;
        ani.Play();
    }
}





2022년 10월 16일 일요일

searchBox.cs

 



using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using System;
using System.Linq;

public class SearchDropBoxInit : MonoBehaviour
{

    [SerializeField] TMP_Dropdown dr_car;
    [SerializeField] TMP_Dropdown dr_company;
    [SerializeField] TMP_Dropdown dr_product;

    [SerializeField] SearchDropBoxOpen uic_car;
    [SerializeField] SearchDropBoxOpen uic_company;
    [SerializeField] SearchDropBoxOpen uic_product;


    [SerializeField] TextMeshProUGUI txt_result_car;
    [SerializeField] TextMeshProUGUI txt_result_company;
    [SerializeField] TextMeshProUGUI txt_result_product;

    [Header("Scrollview")]
    [SerializeField] CompanyItemTableViewController m_CompanyItemTableViewController;
    [Header("Search")]
    [SerializeField] TMP_InputField m_TMP_InputField;

    List<CompanyUiItem> originTable;
    // Start is called before the first frame update
    void Start()
    {
        StartCoroutine(InitData());
        StartCoroutine(InitScrollView());
    }

    IEnumerator InitData()
    {
        yield return new WaitUntil(() => LoadPartLinksWebGL.Instance.m_xmlLoadDatas.m_dataCarsXml.data.Count != 0);
        SetFunction_UI(dr_car, LoadPartLinksWebGL.Instance.m_xmlLoadDatas.m_dataCarsXml.data, uic_car, txt_result_car);
        SetFunction_UI(dr_company, LoadPartLinksWebGL.Instance.m_xmlLoadDatas.m_dataCompanysXml.data, uic_company, txt_result_company);
        SetFunction_UI(dr_product, LoadPartLinksWebGL.Instance.m_xmlLoadDatas.m_dataProductsXml.data, uic_product, txt_result_product);
    }

    IEnumerator InitScrollView()
    {
        yield return new WaitUntil(() => LoadPartLinksWebGL.Instance.m_AllProgramPath.links.Count != 0);
        SetScrollview();

        //m_TMP_InputField.onEndEdit.AddListener(ValueChanged);
        m_TMP_InputField.onValueChanged.AddListener(ValueChanged);
    }


    void SetScrollview()
    {

        originTable = new List<CompanyUiItem>();

       
        foreach (var x in LoadPartLinksWebGL.Instance.m_AllProgramPath.links)
        {
            CompanyUiItem temp = new CompanyUiItem();

            ///회사이름이 없으면 일반씬이므로 컨티뉴
            if (x.Value.companyName == "")
                continue;

            temp.companyName = x.Value.companyName;
            temp.companyType = x.Value.companyType;
            temp.url = x.Value.url;
            temp.products = x.Value.products;
            temp.cars = x.Value.targetCar;
            originTable.Add(temp);
        }
        m_CompanyItemTableViewController.UpdateData(originTable);

    }


    #region 검색
    public List<CompanyUiItem> SearchTableData(string input)
    {
        if (input.Length == 0)
            return originTable;




        input = input.Replace(",", ""); ///따움표는 없는걸로 체크


        Func<CompanyUiItem,bool> condition_car; //검색 조건
        Func<CompanyUiItem, bool> condition_products; //검색 조건
        Func<CompanyUiItem, bool> condition_companyName; //검색 조건
        Func<CompanyUiItem, bool> condition_companyType; //검색 조건
       
        //Func<CompanyUiItem, object> order; //정렬 기준
        string search = input.ToLower(); //소문자로 변환

        condition_car = item => item.cars.Where(x=> x.ToLower().Contains(search)).Any();
        condition_products = item => item.products.Where(x => x.ToLower().Contains(search)).Any();
        condition_companyName = item => item.companyName.ToLower().Contains(search);
        condition_companyType = item => item.companyType.ToLower().Contains(search);






       
        IEnumerable<CompanyUiItem> result_car;
        IEnumerable<CompanyUiItem> result_products;
        IEnumerable<CompanyUiItem> result_company;
        IEnumerable<CompanyUiItem> result_type;

        result_car = originTable.Where(condition_car);
        result_products = originTable.Where(condition_products);
        result_company = originTable.Where(condition_companyName);
        result_type = originTable.Where(condition_companyType);

        //result = originTable.Where(condition_car).Where(condition_companyName).Where(condition_companyType).Where(condition_products);
        result_car = result_car.Select(x => new CompanyUiItem
        {
            companyName = x.companyName,
            companyType = x.companyType,
            cars = x.cars,
            products = x.products,
            url = x.url,
        });
        result_products = result_products.Select(x => new CompanyUiItem
        {
            companyName = x.companyName,
            companyType = x.companyType,
            cars = x.cars,
            products = x.products,
            url = x.url,
        });
        result_company = result_company.Select(x => new CompanyUiItem
        {
            companyName = x.companyName,
            companyType = x.companyType,
            cars = x.cars,
            products = x.products,
            url = x.url,
        });
        result_type = result_type.Select(x => new CompanyUiItem
        {
            companyName = x.companyName,
            companyType = x.companyType,
            cars = x.cars,
            products = x.products,
            url = x.url,
        });

        IEnumerable<CompanyUiItem> result  =
            new[] { result_car, result_products, result_company, result_type }.
            Where(x=>x!=null).
            SelectMany(x => x).
            Distinct();
        return result.ToList();
    }


    void ValueChanged(string text)
    {

        string[] texts = text.Split(",");


        if (texts.Length == 0)
        {
            m_CompanyItemTableViewController.UpdateData(SearchTableData(text));
        }
        else
        {
            List<CompanyUiItem> result = new List<CompanyUiItem>();
            foreach (var x in texts)
                result.AddRange(SearchTableData(x));

            m_CompanyItemTableViewController.UpdateData(result);
        }
    }

    #endregion




    #region 드롭다운
    private void SetFunction_UI(TMP_Dropdown dropdown,List<string> data, SearchDropBoxOpen uic,TextMeshProUGUI result)
    {
        //Reset
        ResetFunction_UI(dropdown, data);


        // Set Default Value
        result.text = dropdown.options[dropdown.value].text;


        dropdown.onValueChanged.AddListener(delegate {
            Function_Dropdown(dropdown, uic, result);
        });
    }

    //private void Function_Button()
    //{
    //    string op = dropdown.options[dropdown.value].text;
    //    result.text = op;
    //    result_img.sprite = dropdown.options[dropdown.value].image;
    //    Debug.LogError("Dropdown Result!\n" + op);
    //}
    private void Function_Dropdown(TMP_Dropdown select, SearchDropBoxOpen uic, TextMeshProUGUI result)
    {
        string op = select.options[select.value].text;
        //message.text = op;
        Debug.Log("Dropdown Change!\n" + op);
        result.text = op;
        uic.Hide();
    }

    private void ResetFunction_UI(TMP_Dropdown dropdown,List<string> data)
    {
       
        dropdown.onValueChanged.RemoveAllListeners();
        dropdown.options.Clear();

        for (int i = 0; i < data.Count; i++)
        {
            int idx = i;
            TMP_Dropdown.OptionData newData = new TMP_Dropdown.OptionData();
            newData.text = data[idx];
            //newData.image = sprites[i];
            dropdown.options.Add(newData);
        }
        dropdown.SetValueWithoutNotify(-1);
        dropdown.SetValueWithoutNotify(0);

    }

    #endregion

}





2022년 10월 7일 금요일

Game Event (Unity,ScriptableObject)

 


https://drive.google.com/file/d/1wYwdK6P3bS1nzN_WYtdIq2Bg0gGmYY1o/view?usp=sharing


################### 간단한 INVOKE 코드
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class ClickTheMinimap : MonoBehaviour,IPointerClickHandler
{

    [SerializeField] GameEvent _onClickMinimap;

    public void OnPointerClick(PointerEventData eventData)
    {
        _onClickMinimap.Invoke();
    }

}

################### 간단한 INVOKE 코드










################### 스크립터블 오브젝트
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


[CreateAssetMenu(menuName ="Game Event",fileName="new Game Event")]
public class GameEvent : ScriptableObject
{

    /// <summary>
    /// HashSet 을 사용하면 중복을 피할수 있다
    /// </summary>
    HashSet<GameEventListener> _listeners = new HashSet<GameEventListener>();
    // Start is called before the first frame update
    public void Invoke()
    {
        foreach (var globalEventListener in _listeners)
            globalEventListener.RaiseEvent();
    }

    public void Register(GameEventListener gameEventListener) => _listeners.Add(gameEventListener);

    public void DeRegister(GameEventListener gameEventListener) => _listeners.Remove(gameEventListener);
}

################### 스크립터블 오브젝트





################### GameEventListener
using UnityEngine;
using UnityEngine.Events;

public class GameEventListener : MonoBehaviour
{
    [SerializeField] protected GameEvent _gameEvent;
    [SerializeField] protected UnityEvent _unityEvent;


    private void Awake()
    {
        _gameEvent.Register(gameEventListener: this);

    }
    private void OnDestroy()
    {
        _gameEvent.DeRegister(gameEventListener: this);
    }

    public virtual void RaiseEvent() => _unityEvent.Invoke();
}

################### GameEventListener





################### GameEventListener 변형버전

using System.Collections;
using UnityEngine;
using UnityEngine.Events ;

public class GameEventListenerWithDelay : GameEventListener
{

    [SerializeField] float _delay = 1f;
    [SerializeField] UnityEvent _delayedUnityEvent;

    public override void RaiseEvent()
    {
        _unityEvent.Invoke();
        StartCoroutine(RunDelayedEvent());
    }

    IEnumerator RunDelayedEvent()
    {
        yield return new WaitForSeconds(_delay);
        _delayedUnityEvent.Invoke();
    }

}

################### GameEventListener 변형버전






   GameEvent 생성 -> 스크립터블 오브젝트라 생성해놓고 사용하면 된다.





EventListner 등록해놓고 원하는 GameEvent를 등록


invoke하면 

각자 등록된 UnityEvent를 실행







2022년 9월 28일 수요일

git upload

 



git 업로드

서버와 다를상황(로컬에서 작업했을때)

git add .
git commit -m
git push 에서 에러남

에러가 난다(서버의 변경사항이 있다)
일단 커밋을 취소하고 stash해야함

git reset --soft HEAD^
git stash
git pull
git stash pop

여기서 충돌이 있으면 pop에서도 에러가 남

pop에러가나면
git reset HEAD origin/master

풀어준다음
git add . 아니면 git add [file]

그리고 git status 확인해주고

다시 commit하고 push 해주면 됨
git commit -m "변경이력저장"
git push -u origin master







2022년 9월 27일 화요일

unity mouse on ui

 


public bool IsPointerOverUIElement()
    {
        return IsPointerOverUIElement(GetEventSystemRaycastResults());
    }
    private bool IsPointerOverUIElement(List<RaycastResult> eventSystemRaysastResults)
    {
        for (int index = 0; index < eventSystemRaysastResults.Count; index++)
        {
            RaycastResult curRaysastResult = eventSystemRaysastResults[index];
            if (curRaysastResult.gameObject.layer == 5)
                return true;
        }
        return false;
    }
    static List<RaycastResult> GetEventSystemRaycastResults()
    {
        PointerEventData eventData = new PointerEventData(EventSystem.current);
        eventData.position = Input.mousePosition;
        List<RaycastResult> raysastResults = new List<RaycastResult>();
        EventSystem.current.RaycastAll(eventData, raysastResults);
        return raysastResults;
    }

    void Update()
    {

        if (!PlayerCam.gameObject.activeSelf)
            return;

        if (IsPointerOverUIElement())
        {
            HidePointObject();
            return;
        }


클릭했을때 체크라서 땟을때(MouseUP)는 또 변수가 하나 더 필요하다



 if (IsPointerOverUIElement())
        {
            isOnUi = true;
            HidePointObject();
            return;
        }

       


        if (Input.GetMouseButton(0))
        {
            ///현재 부딛힌 객체 확인하기
            isOnUi = false;
            if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit))
            {


                //Debug.Log("tag:" + hit.transform.tag);
                if (hit.transform.tag == "ClickableFloor")
                {
                    ShowPointObject();
                    if (PointObject.activeSelf)
                    {
                        PointObject.transform.position = new Vector3(hit.point.x, Player.transform.position.y+1.2f, hit.point.z); ;
                    }
                }
                else
                {
                    HidePointObject();
                }
            }
        }



        if (Input.GetMouseButtonUp(0) && !isOnUi)
        {

            if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit))
            {
                if (hit.transform.tag == "ClickableFloor")
                {
                    isOnUi=false;
                    PlayerSmoothMove(new Vector3(PointObject.transform.position.x, Player.transform.position.y, PointObject.transform.position.z));
                }
            }
        }







2022년 9월 25일 일요일

fade 효과

 



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

public class CilckMoveFade : MonoBehaviour
{
    public float animTime = 0.5f;         // Fade 애니메이션 재생 시간 (단위:초).  
    public Image fadeImage;            // UGUI의 Image컴포넌트 참조 변수.  

    void Awake()
    {
        // Image 컴포넌트를 검색해서 참조 변수 값 설정.  
       
    }


    public void FadeIn(float fadeOutTime, System.Action nextEvent = null)
    {
        StartCoroutine(CoFadeIn(fadeOutTime, nextEvent));
    }

    public void FadeOut(System.Action nextEvent = null)
    {
        StartCoroutine(CoFadeOut(animTime, nextEvent));
    }

    // 투명 -> 불투명
    IEnumerator CoFadeIn(float fadeOutTime, System.Action nextEvent = null)
    {
       
        Color tempColor = fadeImage.color;
        while (tempColor.a < 1f)
        {
            tempColor.a += Time.deltaTime / fadeOutTime;
            fadeImage.color = tempColor;

            if (tempColor.a >= 1f) tempColor.a = 1f;

            yield return null;
        }

        fadeImage.color = tempColor;
        if (nextEvent != null) nextEvent();
    }

    // 불투명 -> 투명
    IEnumerator CoFadeOut(float fadeOutTime, System.Action nextEvent = null)
    {
       
        Color tempColor = fadeImage.color;
        tempColor.a = 1;///불투명으로 설정
        while (tempColor.a > 0f)
        {
            tempColor.a -= Time.deltaTime / fadeOutTime;
            fadeImage.color = tempColor;

            if (tempColor.a <= 0f) tempColor.a = 0f;

            yield return null;
        }
        fadeImage.color = tempColor;
        if (nextEvent != null) nextEvent();
    }


}







2022년 9월 21일 수요일

unity tts

 




using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class tts : MonoBehaviour
{
    public AudioSource audioSource;

    // Start is called before the first frame update
    void Start()
    {
        audioSource = gameObject.GetComponent<AudioSource>();
        StartCoroutine(DownloadTheAudio());
    }

    IEnumerator DownloadTheAudio()
    {
// SampleText 에 원하는 글 넣으면 됨
// En-gb 한국어로 하고싶으면 Ko-gb 하니까 됨

        string url = "https://translate.google.com/translate_tts?ie=UTF-8&total=1&idx=0&textlen=32&client=tw-ob&q=SampleText&tl=En-gb";
        WWW www = new WWW(url);
        yield return www;

        audioSource.clip = www.GetAudioClip(false,true, AudioType.MPEG);
        audioSource.Play();
    }
}


unity array get next prev index

 


Clamp를 사용하면 원하는 값을 딱 얻을 수 있네


    설명
Clamps a value between a minimum float and maximum float value.

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    void Update() {
        transform.position = new Vector3(Mathf.Clamp(Time.time, 1.0F, 3.0F), 0, 0);
    }
}
public static int Clamp (int value, int min, int max);
설명
Clamps value between min and max and returns value.

// Clamps the value 10 to be between 1 and 3.
// prints 3 to the console

Debug.Log(Mathf.Clamp(10, 1, 3));


무한루프


 public void Next_Color()
    {
        if (currnetIndex + 1 == color_materials.Length)
            currnetIndex = 0;
        else
            currnetIndex = Mathf.Clamp(currnetIndex + 1, 0, color_materials.Length- 1);


        SetObjectColor(color_materials[currnetIndex]);
    }
    public void Prev_Color()
    {
        if (currnetIndex-1 == -1)
            currnetIndex = color_materials.Length - 1;
        else
            currnetIndex = Mathf.Clamp(currnetIndex - 1, 0, color_materials.Length - 1);

        SetObjectColor(color_materials[currnetIndex]);
    }


예제


public void Prev_Help()
    {

        currnetIdx = Mathf.Clamp(currnetIdx - 1, 0, helpDatas.helperInfos.Count-1);
        tts.Instance.SpeechStop();

        HelperInfo result = helpDatas.helperInfos[currnetIdx];

        StartDroneMove(result.move, result.rotate);
        tts.Instance.Speech(result.txt_tts);

       
    }

    public void Next_Help()
    {

        currnetIdx = Mathf.Clamp(currnetIdx + 1, 0, helpDatas.helperInfos.Count-1);

       
        tts.Instance.SpeechStop();

        HelperInfo result = helpDatas.helperInfos[currnetIdx];
        StartDroneMove(result.move,result.rotate);
        tts.Instance.Speech(result.txt_tts);

       
       
    }

git rejected error(feat. cherry-pick)

 문제 아무 생각 없이 pull을 받지않고 로컬에서 작업! 커밋, 푸시 진행을 해버렷다. push에선 remote와 다르니 당연히 pull을 진행해라고 하지만 로컬에서 작업한 내용을 백업하지 않고 진행하기에는 부담스럽다(로컬작업 유실 가능성) 해결하려...