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();
    }
}





git rejected error(feat. cherry-pick)

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