2021년 2월 13일 토요일

[Unity3d] UGUI 조이스틱 인터페이스

 UGUI로 간단하게 만들수 있다. 

6.(scene 전환내용에 이어서)외곽의 원, 그밑으로 핸들을 UI -> Image로 만들고 색상과 Source Image(Knob)으로 만들어둔다.

https://github.com/sugoigroup/unity_study_1/commit/f5c7892cc8faf6236ceef1a49dbcf03a376f64ac

스크립트는 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public class Joypad : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler
{
    [SerializeField] private RectTransform rectBack;
    [SerializeField] private RectTransform recFront;
    private float radius;

    public void OnDrag(PointerEventData eventData)
    {
        Vector2 value = eventData.position - (Vector2)rectBack.position;
        value = Vector2.ClampMagnitude(value, radius);
        recFront.localPosition = value;
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        //throw new System.NotImplementedException();
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        recFront.localPosition = Vector3.zero;
    }

    // Start is called before the first frame update
    void Start()
    {
        radius = rectBack.rect.width * 0.5F;

    }

    // Update is called once per frame
    void Update()
    {
        
    }

Vector3에대해선.

http://blog.naver.com/PostView.nhn?blogId=dus531400&logNo=140207486483&parentCategoryNo=&categoryNo=&viewDate=&isShowPopularPosts=false&from=postView


7. 캐릭터도 이동시켜보자.

https://github.com/sugoigroup/unity_study_1/commit/f373d12d2bcefe370e8d824473d64649890df7c4

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
public class MoveCharactor : MonoBehaviour
{
    public float speed;
    private Joypad joypad;

    // Awake는 무조건 실행되는 스크립트이다. start는 스크립트가 활성화 상태에서만 실행된다. 
    private void Awake()
    {
        joypad = GameObject.FindObjectOfType<Joypad>();
    }

    private void FixedUpdate()
    {
        if (joypad.Horizontal != 0 || joypad.Vertical != 0)
        {
            MoveControl();
        }
    }

    private void MoveControl()
    {

        Vector3 upMovement = Vector3.up * speed *  Time.deltaTime*  joypad.Vertical; 
        Vector3 rightMovement = Vector3.right * speed * Time.deltaTime  *  joypad.Horizontal;
        transform.position += upMovement;
        transform.position += rightMovement;
    }

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

    // Update is called once per frame
    void Update()
    {
        
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
public class Joypad : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler
{
    [SerializeField] private RectTransform rectBack;
    [SerializeField] private RectTransform recFront;
    private float radius;

    private Vector3 input = Vector3.zero;
    public float Horizontal { get { return input.x; } }
    public float Vertical { get { return input.y; } }

    public void OnDrag(PointerEventData eventData)
    {
        input = Vector2.ClampMagnitude(
            eventData.position - (Vector2)rectBack.position,
            radius);
        recFront.localPosition = input;
    }

    public void OnPointerDown(PointerEventData eventData)
    {
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        recFront.localPosition = input = Vector3.zero;
    }

    // Start is called before the first frame update
    void Start()
    {
        radius = rectBack.rect.width * 0.5F;

    }

    // Update is called once per frame
    void Update()
    {
        
    }
}



















Related Posts:

  • [Unity] 실전 게임 만들기 강의 /한국/ 좀비슈터, 미니RPG, 등 강좌 몇개 정리해놈. 리듬게임(케이디)의 강좌도 정리해놈.정리잘해논게 맘에듬 https://ansohxxn.github.io/categories/unity-lesson-1… Read More
  • [Unity] Firebase 6.15 를 사용한 프로젝트는 unity 2018 버젼을 사용해서 android export해야 되더라 Firebase 6.15 를 사용한 프로젝트를 최신버젼에서 가져와서 돌려보려 했더니 안되더라,  unity 2018 버젼을 사용해서 android export해야 되더라그리고 혹시모르니 external dependency manager 에서 android Resolver (먼제 Delete Resolved Libraries 하고) 해주면 좋음… Read More
  • Unity3d 에서 Android plugin 불러내기-예제1 참고:https://twnkls.com/blogs/howto-native-android-plugins-for-unity3d/1.안드로이드에서 라이브러리생성1.일단 빈 프로젝트 생성2.안드로이드어플속성의 빈프로젝트가 생성되었지만 이를 그냥 안드로이드 모듈 형태로 바꾸어 보자.- manifests/AndroidManifest.xml 을 아래처럼 깔끔히 정리하자<manifest xmlns:… Read More
  • [Unity] 비행기 슈팅 강좌 / 일본어 / 3000 고급팁 잘되어있음.유니티관련자료가 3500 개 이다... 엄청난 놈이다.https://baba-s.hatenablog.com/entry/2018/04/01/190000第0回 完成プロジェクト第1回 Unity プロジェクトの準備第2回 プレイヤーと背景の配置第3回 プレイヤーの移動第4回 プレイヤーの移動範囲の制限第5回 プレイヤーをマウスカーソルの方に向ける第6回 背景のスクロール第7回 弾の作成と発射第8回 敵の作成と出現第9回 弾と敵の当た… Read More
  • [unity] VisualGraph 3d  https://m.blog.naver.com/PostView.nhn?blogId=hana100494&logNo=222223197400&targetKeyword=&targetRecommendationCode=1잘정리되어 있다.… Read More

0 comments:

댓글 쓰기