Canvas









How do I use Canvas
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


File name: EnemyController.cs Copy
88  void DisplayScore(){
89   GameObject scoreText = Instantiate (Resources.Load ("Score Text Canvas"), new Vector3(transform.position.x, transform.position.y + 1f), Quaternion.identity) as GameObject;
90   scoreText.transform.GetChild (0).transform.GetComponent ().text = maxScore.ToString ();
91   Destroy (scoreText, 2f);
92  }
File name: Structure.cs Copy
82  void Destroyed(){
83   Destroy (gameObject);
84   GameObject newDisassemble = Instantiate (disassemble, transform.position, Quaternion.identity) as GameObject;
85   Destroy (newDisassemble, 3f);
86   if(GameController.instance != null){
87    GameController.instance.score += maxScore;
88   }
89
90   GameObject scoreText = Instantiate (Resources.Load("Score Text Canvas"), new Vector3 (transform.position.x, transform.position.y + 1f, transform.position.z), Quaternion.identity) as GameObject;
91   scoreText.transform.GetChild (0).transform.GetComponent ().text = maxScore.ToString();
92   Destroy (scoreText, 2f);
93  }
File name: UIVerticalScroller.cs Copy
122         public void Update()
123         {
124             if (_arrayOfElements.Length < 1)
125             {
126                 return;
127             }
128
129             for (var i = 0; i < elementLength; i++)
130             {
131                 distReposition[i] = _center.GetComponent().position.y - _arrayOfElements[i].GetComponent().position.y;
132                 distance[i] = Mathf.Abs(distReposition[i]);
133
134                 //Magnifying effect
135                 float scale = Mathf.Max(0.7f, 1 / (1 + distance[i] / 200));
136                 _arrayOfElements[i].GetComponent().transform.localScale = new Vector3(scale, scale, 1f);
137             }
138             float minDistance = Mathf.Min(distance);
139
140             for (var i = 0; i < elementLength; i++)
141             {
142                 _arrayOfElements[i].GetComponent().interactable = false;
143                 if (minDistance == distance[i])
144                 {
145                     minElementsNum = i;
146                     _arrayOfElements[i].GetComponent().interactable = true;
147                     result = _arrayOfElements[i].GetComponentInChildren().text;
148                 }
149             }
150
151             ScrollingElements(-_arrayOfElements[minElementsNum].GetComponent().anchoredPosition.y);
152         }
File name: ScrollSnap.cs Copy
33  protected override void Awake() {
34   base.Awake();
35   actualIndex = startingIndex;
36   cellIndex = startingIndex;
37   this.onLerpComplete = new OnLerpCompleteEvent();
38   this.onRelease = new OnReleaseEvent();
39   this.scrollRect = GetComponent();
40   this.canvasGroup = GetComponent();
41   this.content = scrollRect.content;
42   this.cellSize = content.GetComponent().cellSize;
43   content.anchoredPosition = new Vector2(-cellSize.x * cellIndex, content.anchoredPosition.y);
44   int count = LayoutElementCount();
45   SetContentSize(count);
46
47   if(startingIndex < count) {
48    MoveToIndex(startingIndex);
49   }
50  }
File name: ScrollSnap.cs Copy
52  void LateUpdate() {
53   if(isLerping) {
54    LerpToElement();
55    if(ShouldStopLerping()) {
56     isLerping = false;
57     canvasGroup.blocksRaycasts = true;
58     onLerpComplete.Invoke();
59     onLerpComplete.RemoveListener(WrapElementAround);
60    }
61   }
62  }
File name: ScrollSnap.cs Copy
164  void StartLerping() {
165   releasedPosition = content.anchoredPosition;
166   targetPosition = CalculateTargetPoisition(cellIndex);
167   lerpStartedAt = DateTime.Now;
168   canvasGroup.blocksRaycasts = false;
169   isLerping = true;
170  }

Download file with original file name:Canvas

Canvas 105 lượt xem

Gõ tìm kiếm nhanh...