ngui joystick

Unity3D 2021. 11. 4. 14:48
반응형

https://www.tasharen.com/forum/index.php?topic=55.15 

 

NGUI Virtual Joystick

unfortunately, I don't see the problem in your video.  The video doesn't show where your physical touch so you can't really tell how the joystick is reacting to the touch.  Try adding a debug sprite to the screen that follows the physical touch (make thi

www.tasharen.com

https://m.blog.naver.com/PostView.naver?isHttpsRedirect=true&blogId=ateliersera&logNo=220358443904 

 

NGUI용 Joystick

사용법(프리펩 구조) JoyStickLeft // 요건 배경용 바탕 이미지 - 스틱(여기다가 스크립트 붙이세요) 실...

blog.naver.com

 

덜덜거리는거 수정전 

 

 

 

덜덜거리는거 수정 후 

 

 

 

//변형제작 : 세라프 (ateliersera) 
//원본 출처 : https://gist.github.com/   에  UIJoystick
//목적에 맞게 사용하시면 됩니다.  NGUI 3.8.X 이상에서도 잘 돌아갑니다. 유니티 버젼 4.6.X 이상 문제 없습니다. 
// MonoBehaviour만 사용해서 작동하도록 변경한 코드 입니다.

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

public class UIJoystick : MonoBehaviour
{

	public enum DragEffect
	{
		None,
		Momentum,
		MomentumAndSpring,
	}

	public float joyStickPosX;
	public float joyStickPosY;
	private float posDivision;

	public Transform CenterObject;
	public float ClampRadius = 100.0f;


	private Vector3 scale = Vector3.one;
	public float scrollWheelFactor = 0f;
	public bool restrictWithinPanel = false;
	Plane mPlane;
	Vector3 mLastPos;
	UIPanel mPanel;
	bool mPressed = false;
	Vector3 mMomentum = Vector3.zero;
	float mScroll = 0f;
	Bounds mBounds;
	Coroutine routine;

	public UnityEvent<Vector2> onDragEvent = new UnityEvent<Vector2>();

	
	void Awake ()
	{
		posDivision = 1 / ClampRadius;
	}

	void Update()
	{

	}
	void FindPanel ()
	{
		mPanel = (CenterObject != null) ? UIPanel.Find(CenterObject.transform, false) : null;
		if (mPanel == null) restrictWithinPanel = false;
	}
	void OnPress (bool pressed)
	{
		if (enabled && NGUITools.GetActive(gameObject) && CenterObject != null)
		{
			mPressed = pressed;

			if (this.routine != null)
				this.StopCoroutine(this.routine);

			if (pressed)
			{
				if (restrictWithinPanel && mPanel == null) FindPanel();
				
	
				if (restrictWithinPanel) mBounds = NGUIMath.CalculateRelativeWidgetBounds(mPanel.cachedTransform, CenterObject);
				

				mMomentum = Vector3.zero;
				mScroll = 0f;
				

				SpringPosition sp = CenterObject.GetComponent<SpringPosition>();
				if (sp != null) sp.enabled = false;
				

				mLastPos = UICamera.lastHit.point;
				

				Transform trans = UICamera.currentCamera.transform;
				mPlane = new Plane((mPanel != null ? mPanel.cachedTransform.rotation : trans.rotation) * Vector3.back, mLastPos);
			}
			else if (restrictWithinPanel && mPanel.clipping != UIDrawCall.Clipping.None)
			{
				mPanel.ConstrainTargetToBounds(CenterObject, ref mBounds, false);
			}
			if (!pressed)
			{
                if (this.routine != null) StopCoroutine(this.routine);
                this.routine = StartCoroutine("SpringPositionUpdate");

            }
		}
	}


	void OnDrag (Vector2 delta)
	{

		if (enabled && NGUITools.GetActive(gameObject) && CenterObject != null)
		{
			UICamera.currentTouch.clickNotification = UICamera.ClickNotification.BasedOnDelta;
			
			Ray ray = UICamera.currentCamera.ScreenPointToRay(UICamera.currentTouch.pos);
			float dist = 0f;
			
			if (mPlane.Raycast(ray, out dist))
			{

				Vector3 currentPos = ray.GetPoint(dist);
				mLastPos = currentPos;
				

				CenterObject.position = currentPos;			

				CenterObject.transform.localPosition = new Vector3(Vector3.ClampMagnitude(CenterObject.transform.localPosition, ClampRadius).x, Vector3.ClampMagnitude(CenterObject.transform.localPosition, ClampRadius).y, 0);
	
				joyStickPosX = CenterObject.transform.localPosition.x * posDivision;
				joyStickPosY = CenterObject.transform.localPosition.y * posDivision;

				this.onDragEvent.Invoke(new Vector2(joyStickPosX, joyStickPosY));
			}
		}
	}


	void LateUpdate ()
	{
		float delta = Time.deltaTime;
		if (CenterObject == null) return;
		
		if (mPressed)
		{

			SpringPosition sp = CenterObject.GetComponent<SpringPosition>();
			if (sp != null) sp.enabled = false;
			mScroll = 0f;
		}
		else
		{
			mMomentum += scale * (-mScroll * 0.05f);
			mScroll = NGUIMath.SpringLerp(mScroll, 0f, 20f, delta);
			
			if (mMomentum.magnitude > 0.0001f)
			{

				if (mPanel == null) FindPanel();
				
				if (mPanel != null)
				{
					CenterObject.position += NGUIMath.SpringDampen(ref mMomentum, 9f, delta);
					
					if (restrictWithinPanel && mPanel.clipping != UIDrawCall.Clipping.None)
					{
						mBounds = NGUIMath.CalculateRelativeWidgetBounds(mPanel.cachedTransform, CenterObject);
						
						
					}
					return;
				}
			}
			else mScroll = 0f;
		}
		

		NGUIMath.SpringDampen(ref mMomentum, 9f, delta);
	}



	void OnScroll (float delta)
	{
		if (enabled && NGUITools.GetActive(gameObject))
		{
			if (Mathf.Sign(mScroll) != Mathf.Sign(delta)) mScroll = 0f;
			mScroll += delta * scrollWheelFactor;
		}
	}	

	private Vector3 targetPosition = Vector3.zero;
	public float strength = 10f;
	float mThreshold = 0f;
	IEnumerator SpringPositionUpdate ()
	{
		while (true)
		{
			float delta = Time.deltaTime;
			if (mThreshold == 0f) mThreshold = (targetPosition - CenterObject.localPosition).magnitude * 0.001f;
			CenterObject.localPosition = NGUIMath.SpringLerp(CenterObject.localPosition, targetPosition, strength, delta);
			
			if (mThreshold >= (targetPosition - CenterObject.localPosition).magnitude)
			{
				CenterObject.localPosition = targetPosition;
				Debug.Log("원점으로 돌아옴");
				break;
			}
			yield return 0;
		}
	}

}

UIJoystick.cs
0.00MB

반응형

'Unity3D' 카테고리의 다른 글

Day - 07. UI Toolkit 살펴 보기  (0) 2021.11.05
Day - 06. UI Toolkit 살펴 보기  (0) 2021.11.04
Day - 05. UI Toolkit 살펴 보기  (0) 2021.11.03
Day - 04. UI Toolkit 살펴 보기  (0) 2021.11.03
Day - 03. UI Toolkit 살펴 보기  (0) 2021.11.03
: