River Jiang
2020-10-27 1f5eda1c9d22a3676298751c7282a5874f13bed0
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
namespace MoreMountains.NiceVibrations
{
    public class WobbleDemoManager : DemoManager
    {
        public Camera ButtonCamera;
        public RectTransform ContentZone;
        public WobbleButton WobbleButtonPrefab;
        public Vector2 PrefabSize = new Vector2(200f, 200f);
        public float Margin = 20f;
        public float Padding = 20f;
 
        protected List<WobbleButton> Buttons;
        protected Canvas _canvas;
        protected Vector3 _position = Vector3.zero;
 
        protected virtual void Start()
        {
            _canvas = GetComponentInParent<Canvas>();
 
            float horizontalF = (ContentZone.rect.width - 2 * Padding) / (PrefabSize.x + Margin);
            float verticalF = (ContentZone.rect.height - 2 * Padding) / (PrefabSize.y + Margin);            
            int horizontal = Mathf.FloorToInt(horizontalF);
            int vertical = Mathf.FloorToInt(verticalF);
 
            float centerH = (ContentZone.rect.width - (Padding * 2) - (horizontal * PrefabSize.x) - (horizontal-1)*Margin ) / 2f;
            float centerV = (ContentZone.rect.height - (Padding * 2) - (vertical * PrefabSize.x) - (vertical - 1) * Margin) / 2f;
 
            Buttons = new List<WobbleButton>();
 
            for (int i = 0; i < horizontal; i++)
            {
                for (int j = 0; j < vertical; j++)
                {
                    _position.x = centerH + Padding + PrefabSize.x/2f + i * (PrefabSize.x + Margin);
                    _position.y = centerV + Padding + PrefabSize.y/2f + j * (PrefabSize.y + Margin);
                    _position.z = 0f;
 
                    WobbleButton button = Instantiate(WobbleButtonPrefab);
                    button.transform.SetParent(ContentZone.transform);
                    Buttons.Add(button);
 
                    RectTransform rectTransform = button.GetComponent<RectTransform>();
                    rectTransform.anchorMin = Vector2.zero;
                    rectTransform.anchorMax = Vector2.zero;
                    button.name = "WobbleButton" + i + j;
                    button.transform.localScale = Vector3.one;
                    
                    rectTransform.anchoredPosition3D = _position;
                    button.TargetCamera = ButtonCamera;
                    button.Initialization();
 
                }
            }
 
            int counter = 0;
            foreach(WobbleButton wbutton in Buttons)
            {
                float newPitch = NiceVibrationsDemoHelpers.Remap(counter, 0f, Buttons.Count, 0.3f, 1f);
                wbutton.SetPitch(newPitch);
                counter++;
            }
 
        }
    }
}