rt.position = m_final; hi this part change position of other buttons to 110000 instead 110 .. any help?
Try this: The code is not properly formatted, but you can search for _canvasRect.localScale.
only 3 parts are modified to make the logic work in different canvas scale
Let me know if you still have any issues
Thanks
void SetupCells(bool instantUpdate)
{
for (int i = 0; i < m_cellContainer.Count; i++)
{
RectTransform rt = m_cellContainer[i].GetComponent<recttransform>();
if (rt == null)
continue;
m_newPos = Vector3.zero;
m_newScale = new Vector3(1, 1, 1);
m_newRot = Vector3.zero;
m_offset = (_panel.position - m_dragStartPos);
m_offsetIndex = i - m_centerCellIndex;
m_isCircularMovment = false;
// Consider the canvas scale
m_width = (rt.rect.width + _cellGap) * _canvasRect.localScale.x;
switch (_carouselType)
{
case CarouselConstants.iCarouselType.iCarouselTypeLinear:
{
m_newPos.x = m_width * m_offsetIndex;
}
break;
case CarouselConstants.iCarouselType.iCarouselTypeScaledLinear:
{
m_newPos.x = m_width * m_offsetIndex;
float dis = Vector3.Distance(rt.position, _center.position);
// Make sure it is not too small nor not too big
float scaleRatio = Mathf.Clamp(1 - Mathf.Abs(dis / (_scrollRT.rect.width / 2)), 0.1f, 100);
m_newScale = new Vector3(scaleRatio * _scaleRatio.x, scaleRatio * _scaleRatio.y, scaleRatio * _scaleRatio.z);
}
break;
case CarouselConstants.iCarouselType.iCarouselTypeCoverFlow:
{
m_newPos.x = m_width * m_offsetIndex;
if (m_offsetIndex < 0)
{
m_newRot = -_coverflowAngles;
}
else if (m_offsetIndex > 0)
{
m_newRot = _coverflowAngles;
}
}
break;
case CarouselConstants.iCarouselType.iCarouselTypeScaledCoverFlow:
{
m_newPos.x = m_width * m_offsetIndex;
float dis = Vector3.Distance(rt.position, _center.position);
// Make sure it is not too small nor not too big
float scaleRatio = Mathf.Clamp(1 - Mathf.Abs(dis / (_scrollRT.rect.width / 2)), 0.1f, 100);
m_newScale = new Vector3(scaleRatio * _scaleRatio.x, scaleRatio * _scaleRatio.y, scaleRatio * _scaleRatio.z);
if (m_offsetIndex < 0)
{
m_newRot = -_coverflowAngles;
}
else if (m_offsetIndex > 0)
{
m_newRot = _coverflowAngles;
}
}
break;
}
// Only allow one direction at a time
if (!_isHorizontal)
{
m_newPos.y = m_newPos.x;
m_newPos.x = 0;
m_newRot.x = m_newRot.y;
m_newRot.y = 0;
}
m_final = (_center.position + m_newPos + m_offset);
if (instantUpdate || m_isCircularMovment)
{
rt.position = m_final;
rt.localScale = m_newScale;
rt.localRotation = Quaternion.Euler(m_newRot);
_panel.ForceUpdateRectTransforms();
}
else
{
rt.position = Vector3.Lerp(rt.position, m_final, Time.deltaTime * _moveSpeed);
rt.localScale = Vector3.Lerp(rt.localScale, m_newScale, Time.deltaTime * _scaleSpeed);
rt.localRotation = Quaternion.Lerp(rt.localRotation, Quaternion.Euler(m_newRot), Time.deltaTime * _rotateSpeed);
}
}
}
void CheckBoundary()
{
if (_cell == null)
return;
if (!_shouldLoop)
return;
if (m_cellContainer.Count == 0)
return;
RectTransform rt = _cell.GetComponent<recttransform>();
if (rt == null)
return;
float cellWidth = (rt.rect.width + _cellGap) * _canvasRect.localScale.x;
float cellHeight = (rt.rect.height + _cellGap) * _canvasRect.localScale.y;
// Calculate the boundaries
m_boundary.x = m_cellContainer.Count * cellWidth;
m_boundary.y = m_cellContainer.Count * cellHeight;
float leftBoundary = (_center.position.x - m_boundary.x / 2);
float rightBoundary = (_center.position.x + m_boundary.x / 2);
float upBoundary = (_center.position.y + m_boundary.y / 2);
float downBoundary = (_center.position.y - m_boundary.y / 2);
// We only check the right most or left most
if (_isHorizontal)
{
//if ((_panel.position - m_dragDir).x < 0)
{
GameObject go = m_cellContainer[0];
if (go.transform.position.x < leftBoundary)
{
//Debug.Log("left");
UpdateBoundaryCell(go, new Vector3(rightBoundary, go.transform.position.y, go.transform.position.z), false);
}
}
//else if ((_panel.position - m_dragDir).x > 0)
{
GameObject go = m_cellContainer[m_cellContainer.Count - 1];
if (go.transform.position.x > rightBoundary)
{
//Debug.Log("right");
UpdateBoundaryCell(go, new Vector3(leftBoundary, go.transform.position.y, go.transform.position.z), true);
}
}
}
else
{
//if ((_panel.position - m_dragDir).y < 0)
{
GameObject go = m_cellContainer[0];
if (go.transform.position.y < downBoundary)
{
//Debug.Log("down");
UpdateBoundaryCell(go, new Vector3(go.transform.position.x, upBoundary, go.transform.position.z), false);
}
}
//else if ((_panel.position - m_dragDir).y > 0)
{
GameObject go = m_cellContainer[m_cellContainer.Count - 1];
if (go.transform.position.y > upBoundary)
{
//Debug.Log("up");
UpdateBoundaryCell(go, new Vector3(go.transform.position.x, downBoundary, go.transform.position.z), true);
}
}
}
}m_width = (rt.rect.width + _cellGap) * _canvasRect.localScale.x;
UnassignedReferenceException: The variable _canvasRect of CarouselController has not been assigned.
You probably need to assign the _canvasRect variable of the CarouselController script in the inspector.
UnityEngine.Transform.get_localScale () (at <7559bf9767e74ff5906f18401f66cd57>:0)
CarouselController.SetupCells (System.Boolean instantUpdate) (at Assets/CarouselMenu/Scripts/CarouselController.cs:251)