유 니 티 카드 뒤 집기 효과 구현
사실 이것 은 프로젝트 가 필요 합 니 다.제 가 고 친 코드 입 니 다.사실은 유 니 티 의 기본 속성 을 이용 하여 그 효 과 를 실현 하 는 것 입 니 다.더 이상 말 하지 않 겠 습 니 다.먼저 원 코드 를 올 리 세 요.
/// Credit Mrs. YakaYocha
/// Sourced from - https://www.youtube.com/channel/UCHp8LZ_0-iCvl-5pjHATsgw
/// Please donate: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=RJ8D9FRFQF9VS
using UnityEngine.Events;
namespace UnityEngine.UI.Extensions
{
[RequireComponent(typeof(ScrollRect))]
[AddComponentMenu("Layout/Extensions/Vertical Scroller")]
public class UIVerticalScroller : MonoBehaviour
{
[Tooltip("Scrollable area (content of desired ScrollRect)")]
public RectTransform _scrollingPanel;
[Tooltip("Elements to populate inside the scroller")]
public GameObject[] _arrayOfElements;
[Tooltip("Center display area (position of zoomed content)")]
public RectTransform _center;
[Tooltip("Select the item to be in center on start. (optional)")]
public int StartingIndex = -1;
[Tooltip("Button to go to the next page. (optional)")]
public GameObject ScrollUpButton;
[Tooltip("Button to go to the previous page. (optional)")]
public GameObject ScrollDownButton;
[Tooltip("Event fired when a specific item is clicked, exposes index number of item. (optional)")]
public UnityEvent<int> ButtonClicked;
private float[] distReposition;
private float[] distance;
//private int elementsDistance;
private int minElementsNum;
private int elementLength;
//private int elementHalfLength;
private float deltaY;
private string result;
public UIVerticalScroller() { }
public UIVerticalScroller(RectTransform scrollingPanel, GameObject[] arrayOfElements, RectTransform center)
{
_scrollingPanel = scrollingPanel;
_arrayOfElements = arrayOfElements;
_center = center;
}
public void Awake()
{
var scrollRect = GetComponent<ScrollRect>();
if (!_scrollingPanel)
{
_scrollingPanel = scrollRect.content;
}
if (!_center)
{
Debug.LogError("Please define the RectTransform for the Center viewport of the scrollable area");
}
if (_arrayOfElements == null || _arrayOfElements.Length == 0)
{
var childCount = scrollRect.content.childCount;
if (childCount > 0)
{
_arrayOfElements = new GameObject[childCount];
for (int i = 0; i < childCount; i++)
{
_arrayOfElements[i] = scrollRect.content.GetChild(i).gameObject;
}
}
}
}
public void Start()
{
if (_arrayOfElements.Length < 1)
{
Debug.Log("No child content found, exiting..");
return;
}
elementLength = _arrayOfElements.Length;
distance = new float[elementLength];
distReposition = new float[elementLength];
//get distance between buttons
//elementsDistance = (int)Mathf.Abs(_arrayOfElements[1].GetComponent<RectTransform>().anchoredPosition.y - _arrayOfElements[0].GetComponent<RectTransform>().anchoredPosition.y);
deltaY = _arrayOfElements[0].GetComponent<RectTransform>().rect.height * elementLength / 3 * 2;
Vector2 startPosition = new Vector2(_scrollingPanel.anchoredPosition.x, -deltaY);
_scrollingPanel.anchoredPosition = startPosition;
for (var i = 0; i < _arrayOfElements.Length; i++)
{
AddListener(_arrayOfElements[i], i);
}
if (ScrollUpButton)
ScrollUpButton.GetComponent<Button>().onClick.AddListener(() => { ScrollUp(); });
if (ScrollDownButton)
ScrollDownButton.GetComponent<Button>().onClick.AddListener(() => { ScrollDown(); });
if (StartingIndex > -1)
{
StartingIndex = StartingIndex > _arrayOfElements.Length ? _arrayOfElements.Length - 1 : StartingIndex;
SnapToElement(StartingIndex);
}
}
private void AddListener(GameObject button, int index)
{
button.GetComponent<Button>().onClick.AddListener(() => DoSomething(index));
}
private void DoSomething(int index)
{
if (ButtonClicked != null)
{
ButtonClicked.Invoke(index);
}
}
public void Update()
{
if (_arrayOfElements.Length < 1)
{
return;
}
for (var i = 0; i < elementLength; i++)
{
distReposition[i] = _center.GetComponent<RectTransform>().position.y - _arrayOfElements[i].GetComponent<RectTransform>().position.y;
distance[i] = Mathf.Abs(distReposition[i]);
//Magnifying effect
float scale = Mathf.Max(0.7f, 1 / (1 + distance[i] / 200));
_arrayOfElements[i].GetComponent<RectTransform>().transform.localScale = new Vector3(scale, scale, 1f);
}
float minDistance = Mathf.Min(distance);
for (var i = 0; i < elementLength; i++)
{
_arrayOfElements[i].GetComponent<CanvasGroup>().interactable = false;
if (minDistance == distance[i])
{
minElementsNum = i;
_arrayOfElements[i].GetComponent<CanvasGroup>().interactable = true;
result = _arrayOfElements[i].GetComponentInChildren<Text>().text;
}
}
ScrollingElements(-_arrayOfElements[minElementsNum].GetComponent<RectTransform>().anchoredPosition.y);
}
private void ScrollingElements(float position)
{
float newY = Mathf.Lerp(_scrollingPanel.anchoredPosition.y, position, Time.deltaTime * 1f);
Vector2 newPosition = new Vector2(_scrollingPanel.anchoredPosition.x, newY);
_scrollingPanel.anchoredPosition = newPosition;
}
public string GetResults()
{
return result;
}
public void SnapToElement(int element)
{
float deltaElementPositionY = _arrayOfElements[0].GetComponent<RectTransform>().rect.height * element;
Vector2 newPosition = new Vector2(_scrollingPanel.anchoredPosition.x, -deltaElementPositionY);
_scrollingPanel.anchoredPosition = newPosition;
}
public void ScrollUp()
{
float deltaUp = _arrayOfElements[0].GetComponent<RectTransform>().rect.height / 1.2f;
Vector2 newPositionUp = new Vector2(_scrollingPanel.anchoredPosition.x, _scrollingPanel.anchoredPosition.y - deltaUp);
_scrollingPanel.anchoredPosition = Vector2.Lerp(_scrollingPanel.anchoredPosition, newPositionUp, 1);
}
public void ScrollDown()
{
float deltaDown = _arrayOfElements[0].GetComponent<RectTransform>().rect.height / 1.2f;
Vector2 newPositionDown = new Vector2(_scrollingPanel.anchoredPosition.x, _scrollingPanel.anchoredPosition.y + deltaDown);
_scrollingPanel.anchoredPosition = newPositionDown;
}
}
}
소스 코드 는 상하 로 미 끄 러 졌 고 내 가 고 친 코드 는 좌우 로 미 끄 러 졌 다.
/// Credit Mrs. YakaYocha
/// Sourced from - https://www.youtube.com/channel/UCHp8LZ_0-iCvl-5pjHATsgw
/// Please donate: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=RJ8D9FRFQF9VS
using UnityEngine.Events;
namespace UnityEngine.UI.Extensions
{
[RequireComponent(typeof(ScrollRect))]
[AddComponentMenu("Layout/Extensions/Vertical Scroller")]
public class UIVerticalScrollerMove : MonoBehaviour
{
[Tooltip("Scrollable area (content of desired ScrollRect)")]
public RectTransform _scrollingPanel;//
[Tooltip("Elements to populate inside the scroller")]
public GameObject[] _arrayOfElements;//
[Tooltip("Center display area (position of zoomed content)")]
public RectTransform _center;//
[Tooltip("Select the item to be in center on start. (optional)")]
public int StartingIndex = -1;// ( )
[Tooltip("Button to go to the next page. (optional)")]
public GameObject ScrollLeftButton;//
[Tooltip("Button to go to the previous page. (optional)")]
public GameObject ScrollRightButton;//
[Tooltip("Event fired when a specific item is clicked, exposes index number of item. (optional)")]
public UnityEvent<int> ButtonClicked;//
private float[] distReposition;//
private float[] distance;//
//private int elementsDistance;
private int minElementsNum;//
private int elementLength;//
//private int elementHalfLength;
private float deltaX;// x
private string result;//
public UIVerticalScrollerMove() { }//
public UIVerticalScrollerMove(RectTransform scrollingPanel, GameObject[] arrayOfElements, RectTransform center)
{
_scrollingPanel = scrollingPanel;
_arrayOfElements = arrayOfElements;
_center = center;
}
//
public void Awake()
{
var scrollRect = GetComponent<ScrollRect>();//
if (!_scrollingPanel)
{
_scrollingPanel = scrollRect.content;// ,
}
if (!_center)// ,
{
Debug.LogError("Please define the RectTransform for the Center viewport of the scrollable area");
}
if (_arrayOfElements == null || _arrayOfElements.Length == 0)
{
var childCount = scrollRect.content.childCount;
if (childCount > 0)
{
_arrayOfElements = new GameObject[childCount];
for (int i = 0; i < childCount; i++)
{
_arrayOfElements[i] = scrollRect.content.GetChild(i).gameObject;
}
}
}//
}
//
public void Start()
{
if (_arrayOfElements.Length < 1)
{
Debug.Log("No child content found, exiting..");
return;
}// ,
elementLength = _arrayOfElements.Length;
distance = new float[elementLength];
distReposition = new float[elementLength];//
//get distance between buttons
//elementsDistance = (int)Mathf.Abs(_arrayOfElements[1].GetComponent<RectTransform>().anchoredPosition.y - _arrayOfElements[0].GetComponent<RectTransform>().anchoredPosition.y);
deltaX = _arrayOfElements[0].GetComponent<RectTransform>().rect.width * elementLength / 3 * 2;
Vector2 startPosition = new Vector2( -deltaX,_scrollingPanel.anchoredPosition.y);
_scrollingPanel.anchoredPosition = startPosition;//
for (var i = 0; i < _arrayOfElements.Length; i++)
{
AddListener(_arrayOfElements[i], i);
}//
// ,
if (ScrollLeftButton)
ScrollLeftButton.GetComponent<Button>().onClick.AddListener(() => { ScrollLeft(); });
if (ScrollRightButton)
ScrollRightButton.GetComponent<Button>().onClick.AddListener(() => { ScrollRight(); });
//
if (StartingIndex > -1)
{
StartingIndex = StartingIndex > _arrayOfElements.Length ? _arrayOfElements.Length - 1 : StartingIndex;
SnapToElement(StartingIndex);
}
}
//
private void AddListener(GameObject button, int index)
{
button.GetComponent<Button>().onClick.AddListener(() => DoSomething(index));
}
//index
private void DoSomething(int index)
{
if (ButtonClicked != null)
{
ButtonClicked.Invoke(index);
}
}
//
public void Update()
{
if (_arrayOfElements.Length < 1)
{
return;
}//
for (var i = 0; i < elementLength; i++)
{
distReposition[i] = _center.GetComponent<RectTransform>().position.x - _arrayOfElements[i].GetComponent<RectTransform>().position.x;
distance[i] = Mathf.Abs(distReposition[i]);
//Magnifying effect
float scale = Mathf.Max(0.7f, 1 / (1 + distance[i] / 200));
_arrayOfElements[i].GetComponent<RectTransform>().transform.localScale = new Vector3(scale, scale, 1f);
}//
float minDistance = Mathf.Min(distance);//
for (var i = 0; i < elementLength; i++)
{
_arrayOfElements[i].GetComponent<CanvasGroup>().interactable = false;
if (minDistance == distance[i])
{
minElementsNum = i;
_arrayOfElements[i].GetComponent<CanvasGroup>().interactable = true;
result = _arrayOfElements[i].GetComponentInChildren<Text>().text;
}
}// ,
ScrollingElements(-_arrayOfElements[minElementsNum].GetComponent<RectTransform>().anchoredPosition.x);//
}
// ,
private void ScrollingElements(float position)
{
float newX= Mathf.Lerp(_scrollingPanel.anchoredPosition.x, position, Time.deltaTime * 1f);
Vector2 newPosition = new Vector2(newX,_scrollingPanel.anchoredPosition.y);
_scrollingPanel.anchoredPosition = newPosition;
}
public string GetResults()
{
return result;
}
//
public void SnapToElement(int element)
{
float deltaElementPositionX = _arrayOfElements[0].GetComponent<RectTransform>().rect.width * element;
Vector2 newPosition = new Vector2(-deltaElementPositionX,_scrollingPanel.anchoredPosition.y);
_scrollingPanel.anchoredPosition = newPosition;
}
//
public void ScrollLeft()
{
float deltaLeft = _arrayOfElements[0].GetComponent<RectTransform>().rect.width / 1.2f;
Vector2 newPositionLeft = new Vector2(_scrollingPanel.anchoredPosition.x-deltaLeft, _scrollingPanel.anchoredPosition.y);
_scrollingPanel.anchoredPosition = Vector2.Lerp(_scrollingPanel.anchoredPosition,newPositionLeft, 1);
}
public void ScrollRight()
{
float deltaRight = _arrayOfElements[0].GetComponent<RectTransform>().rect.width / 1.2f;
Vector2 newPositionRight = new Vector2(_scrollingPanel.anchoredPosition.x+deltaRight, _scrollingPanel.anchoredPosition.y);
_scrollingPanel.anchoredPosition = newPositionRight;
}
}
}
이것 은 플러그 인 에 있 는 라 이브 러 리 이지 만 핵심 논 리 는 Unity 로 다시 쓸 수 있 습 니 다.위 에 설명 이 있 습 니 다.마지막 으로 인용 방법:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.UI.Extensions;
public class ScrollingCalendarTest : MonoBehaviour {
public RectTransform monthsScrollingPanel;
public GameObject monthsButtonPrefab;
private GameObject[] monthsButtons;
public RectTransform monthCenter;
private int monthsSet;
UIVerticalScrollerMove monthsVerticalScroller;
//Initialize Months
//
private void InitializeMonths()
{
int[] months = new int[12];
monthsButtons = new GameObject[months.Length];
for (int i = 0; i < months.Length; i++)
{
string month = "";
months[i] = i;
GameObject clone = (GameObject)Instantiate(monthsButtonPrefab, new Vector3(i * 380,0, 0), Quaternion.Euler(new Vector3(0, 0, 0))) as GameObject;
clone.transform.SetParent(monthsScrollingPanel, false);
clone.transform.localScale = new Vector3(1, 1, 1);
month = ""+i;
clone.GetComponentInChildren<Text>().text = month;
clone.name = "Month_" + months[i];
clone.AddComponent<CanvasGroup>();
monthsButtons[i] = clone;
}
}
// Use this for initialization
public void Awake()
{
InitializeMonths();
//Yes Unity complains about this but it doesn't matter in this case.
monthsVerticalScroller = new UIVerticalScrollerMove(monthsScrollingPanel, monthsButtons, monthCenter);
monthsVerticalScroller.Start();
}
public void SetDate()
{
// monthsSet = int.Parse(inputFieldMonths.text) - 1;
monthsVerticalScroller.SnapToElement(monthsSet);
}
void Update()
{
monthsVerticalScroller.Update();
string monthString = monthsVerticalScroller.GetResults();
}
public void MonthsScrollUp()
{
monthsVerticalScroller.ScrollLeft();
}
public void MonthsScrollDown()
{
monthsVerticalScroller.ScrollRight();
}
}
효과 와 참조:이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
photonnetwork.instantiate에서 gamepobject 유형을 생성 한 다음 상태 및 값을 참조하는 방법주로 마지막 기사에서 일어난 일의 수정입니다. 지난번↓ 그럼 주제입니다. (타이틀이 정리되어 없어서 죄송합니다) 우선 전회의 Illegal view ID:0입니다만 photonnetwork.instantiate를 사...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.