Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Help Wanted SerializedField variables and public ones not showing in the inspector.

Discussion in 'Editor & General Support' started by ISLANDERS, Oct 17, 2020.

  1. ISLANDERS

    ISLANDERS

    Joined:
    Jul 18, 2018
    Posts:
    2
    I was testing the script below found somewhere else, and the script works well. But I came into some trouble with serialization. The first 3 variables with SerializedField attribute attached to don't show up in the inspector, and I tried making them public but nothing changed. Did I missed something?
    The screenshot shows how it looks after attached to a panel.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class PinchableScrollRect : ScrollRect
    7. {
    8.     [SerializeField] private float _minZoom = 1f;
    9.     [SerializeField] float _maxZoom = 2f;
    10.     [SerializeField] float _zoomLerpSpeed = 10f;
    11.  
    12.     float _currentZoom = 1;
    13.     bool _isPinching = false;
    14.     float _startPinchDist;
    15.     float _startPinchZoom;
    16.     Vector2 _startPinchCenterPosition;
    17.     Vector2 _startPinchScreenPosition;
    18.     float _mouseWheelSensitivity = 1;
    19.     bool blockPan = false;
    20.  
    21.     protected override void Awake()
    22.     {
    23.         Input.multiTouchEnabled = true;
    24.         Debug.Log(_minZoom);
    25.         Debug.Log(_maxZoom);
    26.         _minZoom = 1f;
    27.         _maxZoom = 2f;
    28.         Debug.Log(_minZoom);
    29.         Debug.Log(_maxZoom);
    30.     }
    31.  
    32.     private void Update()
    33.     {
    34.         if (Input.touchCount == 2)
    35.         {
    36.             if (!_isPinching)
    37.             {
    38.                 _isPinching = true;
    39.                 OnPinchStart();
    40.             }
    41.             OnPinch();
    42.         }
    43.         else
    44.         {
    45.             _isPinching = false;
    46.             if (Input.touchCount == 0)
    47.             {
    48.                 blockPan = false;
    49.             }
    50.         }
    51.         //pc input
    52.         float scrollWheelInput = Input.GetAxis("Mouse ScrollWheel");
    53.         if (Mathf.Abs(scrollWheelInput) > float.Epsilon)
    54.         {
    55.             _currentZoom *= 1 + scrollWheelInput * _mouseWheelSensitivity;
    56.             _currentZoom = Mathf.Clamp(_currentZoom, _minZoom, _maxZoom);
    57.             _startPinchScreenPosition = (Vector2)Input.mousePosition;
    58.             RectTransformUtility.ScreenPointToLocalPointInRectangle(content, _startPinchScreenPosition, null, out _startPinchCenterPosition);
    59.             Vector2 pivotPosition = new Vector3(content.pivot.x * content.rect.size.x, content.pivot.y * content.rect.size.y);
    60.             Vector2 posFromBottomLeft = pivotPosition + _startPinchCenterPosition;
    61.             SetPivot(content, new Vector2(posFromBottomLeft.x / content.rect.width, posFromBottomLeft.y / content.rect.height));
    62.         }
    63.         //pc input end
    64.  
    65.         if (Mathf.Abs(content.localScale.x - _currentZoom) > 0.001f)
    66.             content.localScale = Vector3.Lerp(content.localScale, Vector3.one * _currentZoom, _zoomLerpSpeed * Time.deltaTime);
    67.  
    68.         content.localScale = new Vector3(Mathf.Clamp(content.localScale.x, _minZoom, _maxZoom),
    69.             Mathf.Clamp(content.localScale.x, _minZoom, _maxZoom),
    70.             1f);
    71.     }
    72.  
    73.     protected override void SetContentAnchoredPosition(Vector2 position)
    74.     {
    75.         if (_isPinching || blockPan) return;
    76.         base.SetContentAnchoredPosition(position);
    77.     }
    78.  
    79.     void OnPinchStart()
    80.     {
    81.         Vector2 pos1 = Input.touches[0].position;
    82.         Vector2 pos2 = Input.touches[1].position;
    83.  
    84.         _startPinchDist = Distance(pos1, pos2) * content.localScale.x;
    85.         _startPinchZoom = _currentZoom;
    86.         _startPinchScreenPosition = (pos1 + pos2) / 2;
    87.         RectTransformUtility.ScreenPointToLocalPointInRectangle(content, _startPinchScreenPosition, null, out _startPinchCenterPosition);
    88.         //Debug.Log(_startPinchCenterPosition);
    89.  
    90.        
    91.         Vector2 pivotPosition = new Vector3(content.pivot.x * content.rect.size.x, content.pivot.y * content.rect.size.y);
    92.  
    93.        
    94.         Vector2 posFromBottomLeft = pivotPosition + _startPinchCenterPosition;
    95.  
    96.         SetPivot(content, new Vector2(posFromBottomLeft.x / content.rect.width, posFromBottomLeft.y / content.rect.height));
    97.         blockPan = true;
    98.     }
    99.  
    100.     void OnPinch()
    101.     {
    102.         float currentPinchDist = Distance(Input.touches[0].position, Input.touches[1].position) * content.localScale.x;
    103.  
    104.        
    105.         _currentZoom = (currentPinchDist / _startPinchDist) * _startPinchZoom;
    106.         _currentZoom = Mathf.Clamp(_currentZoom, _minZoom, _maxZoom);
    107.         Debug.Log(_currentZoom);
    108.     }
    109.  
    110.     float Distance(Vector2 pos1, Vector2 pos2)
    111.     {
    112.         RectTransformUtility.ScreenPointToLocalPointInRectangle(content, pos1, null, out pos1);
    113.         RectTransformUtility.ScreenPointToLocalPointInRectangle(content, pos2, null, out pos2);
    114.         return Vector2.Distance(pos1, pos2);
    115.     }
    116.  
    117.     static void SetPivot(RectTransform rectTransform, Vector2 pivot)
    118.     {
    119.         if (rectTransform == null) return;
    120.  
    121.         Vector2 size = rectTransform.rect.size;
    122.         Vector2 deltaPivot = rectTransform.pivot - pivot;
    123.         Vector3 deltaPosition = new Vector3(deltaPivot.x * size.x, deltaPivot.y * size.y) * rectTransform.localScale.x;
    124.         rectTransform.pivot = pivot;
    125.  
    126.        
    127.         rectTransform.localPosition -= deltaPosition;
    128.     }
    129. }
     

    Attached Files:

  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    3,834
    ISLANDERS likes this.
  3. ISLANDERS

    ISLANDERS

    Joined:
    Jul 18, 2018
    Posts:
    2
unityunity