Began
How do I use Began
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: InputToEvent.cs
Copy
32 void Update()
33 {
34 if( DetectPointedAtGameObject )
35 {
36 goPointedAt = RaycastObject( Input.mousePosition );
37 }
38
39 if( Input.touchCount > 0 )
40 {
41 Touch touch = Input.GetTouch( 0 );
42 this.currentPos = touch.position;
43
44 if( touch.phase == TouchPhase.Began )
45 {
46 Press( touch.position );
47 }
48 else if( touch.phase == TouchPhase.Ended )
49 {
50 Release( touch.position );
51 }
52
53 return;
54 }
55
56 currentPos = Input.mousePosition;
57 if( Input.GetMouseButtonDown( 0 ) )
58 {
59 Press( Input.mousePosition );
60 }
61 if( Input.GetMouseButtonUp( 0 ) )
62 {
63 Release( Input.mousePosition );
64 }
65
66 if( Input.GetMouseButtonDown( 1 ) )
67 {
68 pressedPosition = Input.mousePosition;
69 lastGo = RaycastObject( pressedPosition );
70 if( lastGo != null )
71 {
72 lastGo.SendMessage( "OnPressRight", SendMessageOptions.DontRequireReceiver );
73 }
74 }
75 }
File name: SwipeListenerScript.cs
Copy
10 void Update () {
11 //transform.guiText.text = "my text";
12 int fingerCount = 0;
13 foreach (Touch touch in Input.touches) {
14 if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
15 fingerCount++;
16
17 if (touch.phase == TouchPhase.Began)
18 {
19 startPosition = touch.position;
20 lastPosition = touch.position;
21 }
22
23 if (touch.phase == TouchPhase.Moved )
24 {
25 lastPosition = touch.position;
26 }
27 if(touch.phase == TouchPhase.Ended)
28 {
29
30 if((startPosition.x - lastPosition.x) > 80) // left swipe
31 {
32 this.Swipe ("x", -1);
33 }
34 else if((startPosition.x - lastPosition.x) < -80) // right swipe
35 {
36 this.Swipe ("x", 1);
37 }
38 else if((startPosition.y - lastPosition.y) < -80 ) // up swipe
39 {
40 this.Swipe ("y", 1);
41 }
42 else if((startPosition.y - lastPosition.y) > 80 ) // down swipe
43 {
44 this.Swipe ("y", -1);
45 }
46
47 }
48 }
49 }
File name: PlayerController.cs
Copy
55 void TouchMovement(){
56 if(Input.touchCount > 0){
57 Touch touch = Input.GetTouch (0);
58
59 if(touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Began){
60 Vector3 fingerPos = touch.position;
61 position.x = fingerPos.x;
62 realPos = Camera.main.ScreenToWorldPoint (new Vector3(position.x, position.y ,position.z));
63 transform.position = Vector3.Lerp (transform.position, new Vector3(Mathf.Clamp(realPos.x, maxLeft + 0.5f, maxRight - 0.5f), transform.position.y, transform.position.z), Time.deltaTime * speed);
64
65 }
66
67 if (touch.phase == TouchPhase.Moved) {
68 Vector3 fingerPos = touch.position;
69 position.x = fingerPos.x;
70 realPos = Camera.main.ScreenToWorldPoint (new Vector3(position.x, position.y ,position.z));
71 transform.position = Vector3.Lerp (transform.position, new Vector3(Mathf.Clamp(realPos.x, maxLeft + 0.5f, maxRight - 0.5f), transform.position.y, transform.position.z), speed);
72 }
73
74 }
75 }
File name: CameraFollow.cs
Copy
91 void TouchMoveCamera(){
92 if(allowToMove){
93
94 /*if(Input.touchCount > 0){
95 Touch touch = Input.GetTouch (0);
96
97 Transform cam = gameObject.transform;
98
99
100 if(touch.phase == TouchPhase.Began){
101 cam = touch.position;
102 }else if(touch.phase == TouchPhase.Moved){
103
104 }
105 }*/
106
107 if(Input.touchCount > 0){
108 Touch touch = Input.GetTouch (0);
109
110 Transform cam = gameObject.transform;
111 if (touch.position.x > 300) {
112 if(touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Stationary){
113 camPos = touch.position;
114 }else if(touch.phase == TouchPhase.Moved){
115 cam.position = cam.position + (new Vector3 ((camPos.x - touch.position.x), 0, 0) * dragSpeed * Time.deltaTime);
116 float camX = cam.position.x;
117 camX = Mathf.Clamp (camX, leftBound.transform.position.x, rightBound.transform.position.x);
118 cam.position = new Vector3 (camX, cam.position.y, cam.position.z);
119 }
120 }
121
122 }
123
124 }
125 }
File name: TemporalAction.cs
Copy
22 public override bool Act(float delta)
23 {
24 if (complete) return true;
25
26 try
27 {
28 if (!began)
29 {
30 begin();
31 began = true;
32 }
33 time += delta;
34 complete = time >= duration;
35 if (complete) percent = 1;
36 else
37 {
38 percent = time / duration;
39 if (interpolationProcess != null) percent = interpolationProcess.apply(percent);
40 }
41 //UpdateAction(reverse ? 1 - percent : percent);
42 UpdateAction(percent);
43 if (complete) {
44 end();
45 }
46 return complete;
47 }
48 finally {
49 //return false;
50 }
51 }
File name: TemporalAction.cs
Copy
69 public override void restart()
70 {
71 time = 0;
72 began = false;
73 complete = false;
74 }
File name: Player.cs
Copy
56 private void Update ()
57 {
58 //If it's not the player's turn, exit the function.
59 if(!GameManager.instance.playersTurn) return;
60
61 int horizontal = 0; //Used to store the horizontal move direction.
62 int vertical = 0; //Used to store the vertical move direction.
63
64 //Check if we are running either in the Unity editor or in a standalone build.
65#if UNITY_STANDALONE || UNITY_WEBPLAYER
66
67 //Get input from the input manager, round it to an integer and store in horizontal to set x axis move direction
68 horizontal = (int) (Input.GetAxisRaw ("Horizontal"));
69
70 //Get input from the input manager, round it to an integer and store in vertical to set y axis move direction
71 vertical = (int) (Input.GetAxisRaw ("Vertical"));
72
73 //Check if moving horizontally, if so set vertical to zero.
74 if(horizontal != 0)
75 {
76 vertical = 0;
77 }
78 //Check if we are running on iOS, Android, Windows Phone 8 or Unity iPhone
79#elif UNITY_IOS || UNITY_ANDROID || UNITY_WP8 || UNITY_IPHONE
80
81 //Check if Input has registered more than zero touches
82 if (Input.touchCount > 0)
83 {
84 //Store the first touch detected.
85 Touch myTouch = Input.touches[0];
86
87 //Check if the phase of that touch equals Began
88 if (myTouch.phase == TouchPhase.Began)
89 {
90 //If so, set touchOrigin to the position of that touch
91 touchOrigin = myTouch.position;
92 }
93
94 //If the touch phase is not Began, and instead is equal to Ended and the x of touchOrigin is greater or equal to zero:
95 else if (myTouch.phase == TouchPhase.Ended && touchOrigin.x >= 0)
96 {
97 //Set touchEnd to equal the position of this touch
98 Vector2 touchEnd = myTouch.position;
99
100 //Calculate the difference between the beginning and end of the touch on the x axis.
101 float x = touchEnd.x - touchOrigin.x;
102
103 //Calculate the difference between the beginning and end of the touch on the y axis.
104 float y = touchEnd.y - touchOrigin.y;
105
106 //Set touchOrigin.x to -1 so that our else if statement will evaluate false and not repeat immediately.
107 touchOrigin.x = -1;
108
109 //Check if the difference along the x axis is greater than the difference along the y axis.
110 if (Mathf.Abs(x) > Mathf.Abs(y))
111 //If x is greater than zero, set horizontal to 1, otherwise set it to -1
112 horizontal = x > 0 ? 1 : -1;
113 else
114 //If y is greater than zero, set horizontal to 1, otherwise set it to -1
115 vertical = y > 0 ? 1 : -1;
116 }
117 }
118
119#endif //End of mobile platform dependendent compilation section started above with #elif
120 //Check if we have a non-zero value for horizontal or vertical
121 if(horizontal != 0 || vertical != 0)
122 {
123 //Call AttemptMove passing in the generic parameter Wall, since that is what Player may interact with if they encounter one (by attacking it)
124 //Pass in horizontal and vertical as parameters to specify the direction to move Player in.
125 AttemptMove
126 }
127 }
Began 132 lượt xem
Gõ tìm kiếm nhanh...