Center
How do I use Center
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: FormXemDiem.cs
Copy
85 private void btnXuatRaExcel_Click(object sender, EventArgs e)
86 {
87 try
88 {
89
90 Excel.Application objExcelApp = new Excel.Application();
91 Excel.Workbook objExcelWorkbook = objExcelApp.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
92 Excel.Worksheet objSheet = (Excel.Worksheet)objExcelWorkbook.Worksheets[1];
93 //objSheet.Cells.Interior.Color.ToString() = 'blue';
94 objSheet = (Excel.Worksheet)objExcelWorkbook.Sheets[1];
95 objSheet.Cells.HorizontalAlignment = Excel.Constants.xlCenter;
96 objSheet.Cells[2, 5] = "BẢNG ĐIỂM CỦA SINH VIÊN";
97 objSheet.Cells[5, 3] = "Mã Học Phần";
98 objSheet.Cells[5, 4] = "Mã Nhóm Học Phần";
99 objSheet.Cells[5, 5] = "Tên Học Phần";
100 objSheet.Cells[5, 6] = "Điểm Thi";
101 objSheet.Cells[5, 7] = "Điểm Quá Trình";
102 objSheet.Cells[5, 8] = "Điểm Xếp Loại";
103 objSheet.Cells[3, 3] = "Mã Học Kỳ : "+cmbMaHK.SelectedValue.ToString()+"";
104 objSheet.Cells[4, 3] = "Mã SV : " + txtMaSV.Text + "";
105 objSheet.Cells[9,10] = "ĐIỂM TỔNG KẾT";
106 objSheet.Cells[10,10] = "Hệ 10 : "+txtHe10.Text+"";
107 objSheet.Cells[11,10] = "Hệ 4 : "+txtHe4.Text+"";
108 for (int i = 0; i < dgvKetQua.Rows.Count ; i++)
109 {
110 for (int j = 0; j < dgvKetQua.Columns.GetColumnCount(DataGridViewElementStates.Displayed); j++)
111 {
112 objSheet.Cells[i + 6, j + 3] = dgvKetQua.Rows[i].Cells[j].Value.ToString();
113 objSheet.Columns.AutoFit();
114 }
115 }
116 objExcelApp.Visible = true;
117 }
118 catch (Exception ex)
119 {
120 MessageBox.Show(ex.Message);
121 }
122 }
File name: FormXemDiemLop.cs
Copy
77 private void btnXuatRaExcel_Click(object sender, EventArgs e)
78 {
79 try
80 {
81
82 Excel.Application objExcelApp = new Excel.Application();
83 Excel.Workbook objExcelWorkbook = objExcelApp.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
84 Excel.Worksheet objSheet = (Excel.Worksheet)objExcelWorkbook.Worksheets[1];
85 //objSheet.Cells.Interior.Color.ToString() = 'blue';
86 objSheet = (Excel.Worksheet)objExcelWorkbook.Sheets[1];
87 objSheet.Cells.HorizontalAlignment = Excel.Constants.xlCenter;
88 objSheet.Cells[2, 5] = "BẢNG ĐIỂM CỦA LỚP";
89 objSheet.Cells[3, 3] = "Mã Học Kỳ : "+cmbMaHK.SelectedValue.ToString()+"";
90 objSheet.Cells[4, 3] = "Mã Lớp : " + cmbMaLop.SelectedValue.ToString() + "";
91 objSheet.Cells[5, 4] = "Mã Sinh Viên";
92 objSheet.Cells[5, 5] = "Tên Sinh Viên";
93 objSheet.Cells[5, 6] = "Điểm Trung Bình";
94 //select sv.MaSV,TenSV,TrungBinhCuoiKyHe10
95
96 for (int i = 0; i < dgvKetQua.Rows.Count ; i++)
97 {
98 for (int j = 0; j < dgvKetQua.Columns.GetColumnCount(DataGridViewElementStates.Displayed); j++)
99 {
100 objSheet.Cells[i + 6, j + 4] = dgvKetQua.Rows[i].Cells[j].Value.ToString();
101 objSheet.Columns.AutoFit();
102 }
103 }
104 objExcelApp.Visible = true;
105 }
106 catch (Exception ex)
107 {
108 MessageBox.Show(ex.Message);
109 }
110 }
File name: NamePickGui.cs
Copy
15 public void Awake()
16 {
17 this.guiCenteredRect = new Rect(Screen.width/2-GuiSize.x/2, Screen.height/2-GuiSize.y/4, GuiSize.x, GuiSize.y);
18 this.chatComponent = this.GetComponent
19
20 if (this.chatComponent != null && chatComponent.enabled)
21 {
22 Debug.LogWarning("When using the NamePickGui, ChatGui should be disabled initially.");
23
24 if (this.chatComponent.chatClient != null)
25 {
26 this.chatComponent.chatClient.Disconnect();
27 }
28 this.chatComponent.enabled = false;
29 }
30
31 string prefsName = PlayerPrefs.GetString(NamePickGui.UserNamePlayerPref);
32 if (!string.IsNullOrEmpty(prefsName))
33 {
34 this.InputLine = prefsName;
35 }
36 }
File name: NamePickGui.cs
Copy
38 public void OnGUI()
39 {
40 // Enter-Key handling:
41 if (Event.current.type == EventType.KeyDown && (Event.current.keyCode == KeyCode.KeypadEnter || Event.current.keyCode == KeyCode.Return))
42 {
43 if (!string.IsNullOrEmpty(this.InputLine))
44 {
45 this.StartChat();
46 return;
47 }
48 }
49
50
51 GUI.skin.label.wordWrap = true;
52 GUILayout.BeginArea(guiCenteredRect);
53
54
55 if (this.chatComponent != null && string.IsNullOrEmpty(this.chatComponent.ChatAppId))
56 {
57 GUILayout.Label("To continue, configure your Chat AppId.\nIt's listed in the Chat Dashboard (online).\nStop play-mode and edit:\nScripts/ChatGUI in the Hierarchy.");
58 if (GUILayout.Button("Open Chat Dashboard"))
59 {
60 Application.OpenURL("https://www.exitgames.com/en/Chat/Dashboard");
61 }
62 GUILayout.EndArea();
63 return;
64 }
65
66 GUILayout.Label(this.helpText);
67
68 GUILayout.BeginHorizontal();
69 GUI.SetNextControlName("NameInput");
70 this.InputLine = GUILayout.TextField(this.InputLine);
71 if (GUILayout.Button("Connect", GUILayout.ExpandWidth(false)))
72 {
73 this.StartChat();
74 }
75 GUILayout.EndHorizontal();
76
77 GUILayout.EndArea();
78
79
80 GUI.FocusControl("NameInput");
81 }
File name: PickupCamera.cs
Copy
41 void OnEnable()
42 {
43 if( this.photonView != null && !this.photonView.isMine )
44 {
45 this.enabled = false;
46 return;
47 }
48
49 if( !cameraTransform && Camera.main )
50 cameraTransform = Camera.main.transform;
51 if( !cameraTransform )
52 {
53 Debug.Log( "Please assign a camera to the ThirdPersonCamera script." );
54 enabled = false;
55 }
56
57 m_CameraTransformCamera = cameraTransform.GetComponent
58
59
60 _target = transform;
61 if( _target )
62 {
63 controller = _target.GetComponent
64 }
65
66 if( controller )
67 {
68 CharacterController characterController = (CharacterController)_target.GetComponent
69 centerOffset = characterController.bounds.center - _target.position;
70 headOffset = centerOffset;
71 headOffset.y = characterController.bounds.max.y - _target.position.y;
72 }
73 else
74 Debug.Log( "Please assign a target to the camera that has a ThirdPersonController script attached." );
75
76
77 Cut( _target, centerOffset );
78 }
File name: PickupCamera.cs
Copy
94 void Apply( Transform dummyTarget, Vector3 dummyCenter )
95 {
96 // Early out if we don't have a target
97 if( !controller )
98 return;
99
100 Vector3 targetCenter = _target.position + centerOffset;
101 Vector3 targetHead = _target.position + headOffset;
102
103 // DebugDrawStuff();
104
105 // Calculate the current & target rotation angles
106 float originalTargetAngle = _target.eulerAngles.y;
107 float currentAngle = cameraTransform.eulerAngles.y;
108
109 // Adjust real target angle when camera is locked
110 float targetAngle = originalTargetAngle;
111
112 // When pressing Fire2 (alt) the camera will snap to the target direction real quick.
113 // It will stop snapping when it reaches the target
114 if( Input.GetButton( "Fire2" ) )
115 snap = true;
116
117 if( snap )
118 {
119 // We are close to the target, so we can stop snapping now!
120 if( AngleDistance( currentAngle, originalTargetAngle ) < 3.0f )
121 snap = false;
122
123 currentAngle = Mathf.SmoothDampAngle( currentAngle, targetAngle, ref angleVelocity, snapSmoothLag, snapMaxSpeed );
124 }
125 // Normal camera motion
126 else
127 {
128 if( controller.GetLockCameraTimer() < lockCameraTimeout )
129 {
130 targetAngle = currentAngle;
131 }
132
133 // Lock the camera when moving backwards!
134 // * It is really confusing to do 180 degree spins when turning around.
135 if( AngleDistance( currentAngle, targetAngle ) > 160 && controller.IsMovingBackwards() )
136 targetAngle += 180;
137
138 currentAngle = Mathf.SmoothDampAngle( currentAngle, targetAngle, ref angleVelocity, angularSmoothLag, angularMaxSpeed );
139 }
140
141
142 // When jumping don't move camera upwards but only down!
143 if( controller.IsJumping() )
144 {
145 // We'd be moving the camera upwards, do that only if it's really high
146 float newTargetHeight = targetCenter.y + height;
147 if( newTargetHeight < targetHeight || newTargetHeight - targetHeight > 5 )
148 targetHeight = targetCenter.y + height;
149 }
150 // When walking always update the target height
151 else
152 {
153 targetHeight = targetCenter.y + height;
154 }
155
156 // Damp the height
157 float currentHeight = cameraTransform.position.y;
158 currentHeight = Mathf.SmoothDamp( currentHeight, targetHeight, ref heightVelocity, heightSmoothLag );
159
160 // Convert the angle into a rotation, by which we then reposition the camera
161 Quaternion currentRotation = Quaternion.Euler( 0, currentAngle, 0 );
162
163 // Set the position of the camera on the x-z plane to:
164 // distance meters behind the target
165 cameraTransform.position = targetCenter;
166 cameraTransform.position += currentRotation * Vector3.back * distance;
167
168 // Set the height of the camera
169 cameraTransform.position = new Vector3( cameraTransform.position.x, currentHeight, cameraTransform.position.z );
170
171 // Always look at the target
172 SetUpRotation( targetCenter, targetHead );
173 }
File name: PickupCamera.cs
Copy
180 void Cut( Transform dummyTarget, Vector3 dummyCenter )
181 {
182 float oldHeightSmooth = heightSmoothLag;
183 float oldSnapMaxSpeed = snapMaxSpeed;
184 float oldSnapSmooth = snapSmoothLag;
185
186 snapMaxSpeed = 10000;
187 snapSmoothLag = 0.001f;
188 heightSmoothLag = 0.001f;
189
190 snap = true;
191 Apply( transform, Vector3.zero );
192
193 heightSmoothLag = oldHeightSmooth;
194 snapMaxSpeed = oldSnapMaxSpeed;
195 snapSmoothLag = oldSnapSmooth;
196 }
File name: PickupCamera.cs
Copy
198 void SetUpRotation( Vector3 centerPos, Vector3 headPos )
199 {
200 // Now it's getting hairy. The devil is in the details here, the big issue is jumping of course.
201 // * When jumping up and down we don't want to center the guy in screen space.
202 // This is important to give a feel for how high you jump and avoiding large camera movements.
203 //
204 // * At the same time we dont want him to ever go out of screen and we want all rotations to be totally smooth.
205 //
206 // So here is what we will do:
207 //
208 // 1. We first find the rotation around the y axis. Thus he is always centered on the y-axis
209 // 2. When grounded we make him be centered
210 // 3. When jumping we keep the camera rotation but rotate the camera to get him back into view if his head is above some threshold
211 // 4. When landing we smoothly interpolate towards centering him on screen
212 Vector3 cameraPos = cameraTransform.position;
213 Vector3 offsetToCenter = centerPos - cameraPos;
214
215 // Generate base rotation only around y-axis
216 Quaternion yRotation = Quaternion.LookRotation( new Vector3( offsetToCenter.x, 0, offsetToCenter.z ) );
217
218 Vector3 relativeOffset = Vector3.forward * distance + Vector3.down * height;
219 cameraTransform.rotation = yRotation * Quaternion.LookRotation( relativeOffset );
220
221 // Calculate the projected center position and top position in world space
222 Ray centerRay = m_CameraTransformCamera.ViewportPointToRay( new Vector3( 0.5f, 0.5f, 1 ) );
223 Ray topRay = m_CameraTransformCamera.ViewportPointToRay( new Vector3( 0.5f, clampHeadPositionScreenSpace, 1 ) );
224
225 Vector3 centerRayPos = centerRay.GetPoint( distance );
226 Vector3 topRayPos = topRay.GetPoint( distance );
227
228 float centerToTopAngle = Vector3.Angle( centerRay.direction, topRay.direction );
229
230 float heightToAngle = centerToTopAngle / ( centerRayPos.y - topRayPos.y );
231
232 float extraLookAngle = heightToAngle * ( centerRayPos.y - centerPos.y );
233 if( extraLookAngle < centerToTopAngle )
234 {
235 extraLookAngle = 0;
236 }
237 else
238 {
239 extraLookAngle = extraLookAngle - centerToTopAngle;
240 cameraTransform.rotation *= Quaternion.Euler( -extraLookAngle, 0, 0 );
241 }
242 }
File name: ThirdPersonCamera.cs
Copy
40 void OnEnable()
41 {
42 if( !cameraTransform && Camera.main )
43 cameraTransform = Camera.main.transform;
44 if( !cameraTransform )
45 {
46 Debug.Log( "Please assign a camera to the ThirdPersonCamera script." );
47 enabled = false;
48 }
49
50 m_CameraTransformCamera = cameraTransform.GetComponent
51
52 _target = transform;
53 if( _target )
54 {
55 controller = _target.GetComponent
56 }
57
58 if( controller )
59 {
60 CharacterController characterController = (CharacterController)_target.GetComponent
61 centerOffset = characterController.bounds.center - _target.position;
62 headOffset = centerOffset;
63 headOffset.y = characterController.bounds.max.y - _target.position.y;
64 }
65 else
66 Debug.Log( "Please assign a target to the camera that has a ThirdPersonController script attached." );
67
68
69 Cut( _target, centerOffset );
70 }
Center 99 lượt xem
Gõ tìm kiếm nhanh...