Feature
How do I use Feature
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: HubGui.cs
Copy
28 void OnGUI()
29 {
30 GUI.skin = this.Skin;
31 GUILayout.Space(10);
32
33 GUILayout.BeginHorizontal();
34 GUILayout.Space(10);
35 scrollPos = GUILayout.BeginScrollView(scrollPos, GUILayout.Width(320));
36
37 GUILayout.Label("Basics", m_Headline);
38 if (GUILayout.Button("Demo Boxes", GUILayout.Width(280)))
39 {
40 demoDescription = "
41 demoBtn = new DemoBtn() { Text = "Start", Link = "DemoBoxes-Scene" };
42 }
43 if (GUILayout.Button("Demo Worker", GUILayout.Width(280)))
44 {
45 demoDescription = "
46 demoBtn = new DemoBtn() { Text = "Start", Link = "DemoWorker-Scene" };
47 }
48 if (GUILayout.Button("Movement Smoothing", GUILayout.Width(280)))
49 {
50 demoDescription = "
51 demoBtn = new DemoBtn() { Text = "Start", Link = "DemoSynchronization-Scene" };
52 }
53
54 GUILayout.Label("Advanced", m_Headline);
55 if (GUILayout.Button("Ownership Transfer", GUILayout.Width(280)))
56 {
57 demoDescription = "
58 this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoChangeOwner-Scene" };
59 this.webLink = new DemoBtn();
60 }
61 if (GUILayout.Button("Pickup, Teams, Scores", GUILayout.Width(280)))
62 {
63 demoDescription = "
64 this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoPickup-Scene" };
65 this.webLink = new DemoBtn();
66 }
67
68 GUILayout.Label("Feature Demos", m_Headline);
69 if (GUILayout.Button("Chat", GUILayout.Width(280)))
70 {
71 demoDescription = "
72 this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoChat-Scene" };
73 this.webLink = new DemoBtn();
74 }
75 if (GUILayout.Button("RPG Movement", GUILayout.Width(280)))
76 {
77 demoDescription = "
78 this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoRPGMovement-Scene" };
79 this.webLink = new DemoBtn();
80 }
81 if (GUILayout.Button("Mecanim Animations", GUILayout.Width(280)))
82 {
83 demoDescription = "
84 this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoMecanim-Scene" };
85 this.webLink = new DemoBtn();
86 }
87 if (GUILayout.Button("2D Game", GUILayout.Width(280)))
88 {
89 demoDescription = "
90 this.demoBtn = new DemoBtn() { Text = "Start", Link = "Demo2DJumpAndRunWithPhysics-Scene" };
91 this.webLink = new DemoBtn();
92 }
93 if (GUILayout.Button("Friends & Authentication", GUILayout.Width(280)))
94 {
95 demoDescription = "
96 this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoFriends-Scene" };
97 this.webLink = new DemoBtn();
98 }
99
100 GUILayout.Label("Tutorial", m_Headline);
101 if (GUILayout.Button("Marco Polo Tutorial", GUILayout.Width(280)))
102 {
103 demoDescription = "
104 this.demoBtn = new DemoBtn() { Text = "Start", Link = "MarcoPolo-Scene" };
105 this.webLink = new DemoBtn() { Text = "Open Tutorial (www)", Link = "http://tinyurl.com/nmylf44" };
106 }
107 GUILayout.EndScrollView();
108
109 GUILayout.BeginVertical(GUILayout.Width(Screen.width - 345));
110 GUILayout.Label(demoDescription);
111 GUILayout.Space(10);
112 if (!string.IsNullOrEmpty(this.demoBtn.Text))
113 {
114 if (GUILayout.Button(this.demoBtn.Text))
115 {
116 Application.LoadLevel(this.demoBtn.Link);
117 }
118 }
119 if (!string.IsNullOrEmpty(this.webLink.Text))
120 {
121 if (GUILayout.Button(this.webLink.Text))
122 {
123 Application.OpenURL(this.webLink.Link);
124 }
125 }
126 GUILayout.EndVertical();
127
128
129 GUILayout.EndHorizontal();
130 }
File name: PhotonConverter.cs
Copy
18 public static void RunConversion()
19 {
20 //Ask if user has made a backup.
21 int option = EditorUtility.DisplayDialogComplex("Conversion", "Attempt automatic conversion from Unity Networking to Photon Unity Networking \"PUN\"?", "Yes", "No!", "Pick Script Folder");
22 switch (option)
23 {
24 case 0:
25 break;
26 case 1:
27 return;
28 case 2:
29 PickFolderAndConvertScripts();
30 return;
31 default:
32 return;
33 }
34
35 //REAAAALY?
36 bool result = EditorUtility.DisplayDialog("Conversion", "Disclaimer: The code conversion feature is quite crude, but should do it's job well (see the sourcecode). A backup is therefore strongly recommended!", "Yes, I've made a backup: GO", "Abort");
37 if (!result)
38 {
39 return;
40 }
41 Output(EditorApplication.timeSinceStartup + " Started conversion of Unity networking -> Photon");
42
43 //Ask to save current scene (optional)
44 EditorApplication.SaveCurrentSceneIfUserWantsTo();
45
46 EditorUtility.DisplayProgressBar("Converting..", "Starting.", 0);
47
48 //Convert NetworkViews to PhotonViews in Project prefabs
49 //Ask the user if we can move all prefabs to a resources folder
50 bool movePrefabs = EditorUtility.DisplayDialog("Conversion", "Can all prefabs that use a PhotonView be moved to a Resources/ folder? You need this if you use Network.Instantiate.", "Yes", "No");
51
52
53 string[] prefabs = Directory.GetFiles("Assets/", "*.prefab", SearchOption.AllDirectories);
54 foreach (string prefab in prefabs)
55 {
56 EditorUtility.DisplayProgressBar("Converting..", "Object:" + prefab, 0.6f);
57
58 Object[] objs = (Object[])AssetDatabase.LoadAllAssetsAtPath(prefab);
59 int converted = 0;
60 foreach (Object obj in objs)
61 {
62 if (obj != null && obj.GetType() == typeof(GameObject))
63 converted += ConvertNetworkView(((GameObject)obj).GetComponents
64 }
65 if (movePrefabs && converted > 0)
66 {
67 //This prefab needs to be under the root of a Resources folder!
68 string path = prefab.Replace("\\", "/");
69 int lastSlash = path.LastIndexOf("/");
70 int resourcesIndex = path.LastIndexOf("/Resources/");
71 if (resourcesIndex != lastSlash - 10)
72 {
73 if (path.Contains("/Resources/"))
74 {
75 Debug.LogWarning("Warning, prefab [" + prefab + "] was already in a resources folder. But has been placed in the root of another one!");
76 }
77 //This prefab NEEDS to be placed under a resources folder
78 string resourcesFolder = path.Substring(0, lastSlash) + "/Resources/";
79 EnsureFolder(resourcesFolder);
80 string newPath = resourcesFolder + path.Substring(lastSlash + 1);
81 string error = AssetDatabase.MoveAsset(prefab, newPath);
82 if (error != "")
83 Debug.LogError(error);
84 Output("Fixed prefab [" + prefab + "] by moving it into a resources folder.");
85 }
86 }
87 }
88
89 //Convert NetworkViews to PhotonViews in scenes
90 string[] sceneFiles = Directory.GetFiles("Assets/", "*.unity", SearchOption.AllDirectories);
91 foreach (string sceneName in sceneFiles)
92 {
93 EditorApplication.OpenScene(sceneName);
94 EditorUtility.DisplayProgressBar("Converting..", "Scene:" + sceneName, 0.2f);
95
96 int converted2 = ConvertNetworkView((NetworkView[])GameObject.FindObjectsOfType(typeof(NetworkView)), true);
97 if (converted2 > 0)
98 {
99 //This will correct all prefabs: The prefabs have gotten new components, but the correct ID's were lost in this case
100 PhotonViewHandler.HierarchyChange(); //TODO: most likely this is triggered on change or on save
101
102 Output("Replaced " + converted2 + " NetworkViews with PhotonViews in scene: " + sceneName);
103 EditorApplication.SaveScene(EditorApplication.currentScene);
104 }
105
106 }
107
108 //Convert C#/JS scripts (API stuff)
109 List
110
111 EditorUtility.DisplayProgressBar("Converting..", "Scripts..", 0.9f);
112 ConvertScripts(scripts);
113
114 Output(EditorApplication.timeSinceStartup + " Completed conversion!");
115 EditorUtility.ClearProgressBar();
116
117 EditorUtility.DisplayDialog("Completed the conversion", "Don't forget to add \"PhotonNetwork.ConnectWithDefaultSettings();\" to connect to the Photon server before using any multiplayer functionality.", "OK");
118 }
File name: frmMainMenu.cs
Copy
249 public void GetData()
250 {
251 try
252 {
253 con = new SqlConnection(cs.DBConn);
254 con.Open();
255 String sql = "SELECT Product.ProductID,ProductName,Features,Price,sum(Quantity),sum(Price*Quantity) from Temp_Stock,Product where Temp_Stock.ProductID=Product.ProductID group by Product.productID,productname,Price,Features,Quantity having(Quantity>0) order by ProductName";
256 cmd = new SqlCommand(sql, con);
257 rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
258 dataGridView1.Rows.Clear();
259 while (rdr.Read() == true)
260 {
261 dataGridView1.Rows.Add(rdr[0], rdr[1], rdr[2], rdr[3], rdr[4], rdr[5]);
262 }
263 foreach (DataGridViewRow r in this.dataGridView1.Rows)
264 {
265 if (Convert.ToInt32(r.Cells[4].Value) < 10)
266 {
267 r.DefaultCellStyle.BackColor = Color.Red;
268 }
269 }
270 con.Close();
271 }
272 catch (Exception ex)
273 {
274 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
275 }
276 }
File name: frmMainMenu.cs
Copy
309 private void textBox1_TextChanged(object sender, EventArgs e)
310 {
311 try{
312 con = new SqlConnection(cs.DBConn);
313 con.Open();
314 String sql = "SELECT Product.ProductID,ProductName,Features,Price,sum(Quantity),sum(Price*Quantity) from Temp_Stock,Product where Temp_Stock.ProductID=Product.ProductID and ProductName like '" + txtProductName.Text + "%' group by product.ProductID,productname,Price,Features,Quantity having(quantity>0) order by ProductName";
315 cmd = new SqlCommand(sql, con);
316 rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
317 dataGridView1.Rows.Clear();
318 while (rdr.Read() == true)
319 {
320 dataGridView1.Rows.Add(rdr[0], rdr[1], rdr[2], rdr[3], rdr[4], rdr[5]);
321 }
322 foreach (DataGridViewRow r in this.dataGridView1.Rows)
323 {
324 if (Convert.ToInt32(r.Cells[4].Value) < 10)
325 {
326 r.DefaultCellStyle.BackColor = Color.Red;
327 }
328 }
329 con.Close();
330 }
331 catch (Exception ex)
332 {
333 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
334 }
335 }
File name: frmOrder.cs
Copy
393 private void textBox1_TextChanged(object sender, EventArgs e)
394 {
395 try
396 {
397 con = new SqlConnection(cs.DBConn);
398 con.Open();
399 String sql = "SELECT Product.ProductID,ProductName,Features,Price,sum(Quantity) from Temp_Stock,Product where Temp_Stock.ProductID=Product.ProductID and ProductName like '" + txtProduct.Text + "%' group by product.ProductID,productname,Price,Features,Quantity having(quantity>0) order by ProductName";
400 cmd = new SqlCommand(sql, con);
401 rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
402 dataGridView1.Rows.Clear();
403 while (rdr.Read() == true)
404 {
405 dataGridView1.Rows.Add(rdr[0], rdr[1], rdr[2], rdr[3], rdr[4]);
406 }
407 con.Close();
408 }
409 catch (Exception ex)
410 {
411 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
412 }
413 }
File name: frmOrder.cs
Copy
444 public void GetData()
445 {
446 try
447 {
448 con = new SqlConnection(cs.DBConn);
449 con.Open();
450 String sql = "SELECT Product.ProductID,ProductName,Features,Price,sum(Quantity) from Temp_Stock,Product where Temp_Stock.ProductID=Product.ProductID group by Product.productID,productname,Price,Features,Quantity having(Quantity>0) order by ProductName";
451 cmd = new SqlCommand(sql, con);
452 rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
453 dataGridView1.Rows.Clear();
454 while (rdr.Read() == true)
455 {
456 dataGridView1.Rows.Add(rdr[0], rdr[1], rdr[2], rdr[3],rdr[4]);
457 }
458 con.Close();
459 }
460 catch (Exception ex)
461 {
462 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
463 }
464 }
File name: frmPlaceOrders.cs
Copy
404 private void textBox1_TextChanged(object sender, EventArgs e)
405 {
406 try
407 {
408 con = new SqlConnection(cs.DBConn);
409 con.Open();
410 String sql = "SELECT Product.ProductID,ProductName,Features,Price,sum(Quantity) from Temp_Stock,Product where Temp_Stock.ProductID=Product.ProductID and ProductName like '" + txtProduct.Text + "%' group by product.ProductID,productname,Price,Features,Quantity having(quantity>0) order by ProductName";
411 cmd = new SqlCommand(sql, con);
412 rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
413 dataGridView1.Rows.Clear();
414 while (rdr.Read() == true)
415 {
416 dataGridView1.Rows.Add(rdr[0], rdr[1], rdr[2], rdr[3], rdr[4]);
417 }
418 con.Close();
419 }
420 catch (Exception ex)
421 {
422 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
423 }
424 }
File name: frmPlaceOrders.cs
Copy
455 public void GetData()
456 {
457 try
458 {
459 con = new SqlConnection(cs.DBConn);
460 con.Open();
461 String sql = "SELECT Product.ProductID,ProductName,Features,Price,sum(Quantity) from Temp_Stock,Product where Temp_Stock.ProductID=Product.ProductID group by Product.productID,productname,Price,Features,Quantity having(Quantity>0) order by ProductName";
462 cmd = new SqlCommand(sql, con);
463 rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
464 dataGridView1.Rows.Clear();
465 while (rdr.Read() == true)
466 {
467 dataGridView1.Rows.Add(rdr[0], rdr[1], rdr[2], rdr[3],rdr[4]);
468 }
469 con.Close();
470 }
471 catch (Exception ex)
472 {
473 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
474 }
475 }
File name: frmProduct.cs
Copy
76 private void Reset()
77 {
78 txtProductName.Text = "";
79 cmbSubCategory.Text = "";
80 cmbCategory.Text = "";
81 txtPrice.Text = "";
82 txtFeatures.Text = "";
83 pictureBox1.Image = Properties.Resources._12;
84 cmbSubCategory.Enabled = false;
85 btnDelete.Enabled = false;
86 btnUpdate.Enabled = false;
87 btnSave.Enabled = true;
88 txtProductName.Focus();
89 }
File name: frmProduct.cs
Copy
96 private void btnSave_Click(object sender, EventArgs e)
97 {
98 if (txtProductName.Text == "")
99 {
100 MessageBox.Show("Please enter product name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
101 txtProductName.Focus();
102 return;
103 }
104 if (cmbCategory.Text == "")
105 {
106 MessageBox.Show("Please select category", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
107 cmbCategory.Focus();
108 return;
109 }
110 if (cmbSubCategory.Text == "")
111 {
112 MessageBox.Show("Please select sub category", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
113 cmbSubCategory.Focus();
114 return;
115 }
116 if (txtPrice.Text == "")
117 {
118 MessageBox.Show("Please enter price", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
119 txtPrice.Focus();
120 return;
121 }
122 try
123 {
124 con = new SqlConnection(cs.DBConn);
125 con.Open();
126 string ct = "select ProductName from Product where ProductName='" + txtProductName.Text + "'";
127
128 cmd = new SqlCommand(ct);
129 cmd.Connection = con;
130 rdr = cmd.ExecuteReader();
131
132 if (rdr.Read())
133 {
134 MessageBox.Show("Product Name Already Exists", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
135 txtProductName.Text = "";
136 txtProductName.Focus();
137
138
139 if ((rdr != null))
140 {
141 rdr.Close();
142 }
143 return;
144 }
145 auto();
146 con = new SqlConnection(cs.DBConn);
147 con.Open();
148 string cb = "insert into Product(ProductID,ProductName,CategoryID,SubCategoryID,Features,Price,Image) VALUES ('" + txtProductID.Text + "','" + txtProductName.Text + "'," + txtCategoryID.Text+ "," + txtSubCategoryID.Text+ ",@d1,"+ txtPrice.Text +",@d2)";
149 cmd = new SqlCommand(cb);
150 cmd.Connection = con;
151 cmd.Parameters.AddWithValue("@d1", txtFeatures.Text);
152 MemoryStream ms = new MemoryStream();
153 Bitmap bmpImage = new Bitmap(pictureBox1.Image);
154 bmpImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
155 byte[] data = ms.GetBuffer();
156 SqlParameter p = new SqlParameter("@d2", SqlDbType.Image);
157 p.Value = data;
158 cmd.Parameters.Add(p);
159 cmd.ExecuteReader();
160 con.Close();
161 MessageBox.Show("Successfully saved", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information);
162 Autocomplete();
163 btnSave.Enabled = false;
164 }
165 catch (Exception ex)
166 {
167 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
168 }
169 }
Feature 150 lượt xem
Gõ tìm kiếm nhanh...