Attributes
How do I use Attributes
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: Animals.cs
Copy
14 public void Start()
15 {
16 string[] names = new string[] { "dog", "monkey", "pig", "fox", "giraffe", "panda", "rhino", "tiger", "elephant", "lion" };
17
18 animals = new List
19 int animalIndex = 0;
20
21 UpgradeInfo upgradeInfo = new UpgradeInfo();
22
23 GameObject animalPlayer = (GameObject)Instantiate(Resources.Load
24 animalPlayer.transform.parent = gameObject.transform;
25 animalPlayer.transform.localPosition = new Vector3(-3.5f + 0.7f * animalIndex, -0.9f, 0);
26 setSortingLayer(animalPlayer, "Animal8");
27 Animal player = animalPlayer.AddComponent
28 player.setAnimalName(names[Attr.currentAnimal]);
29 player.animalIndex = animalIndex;
30 player.gameScreen = gameScreen;
31 float speedPlayer = upgradeInfo.getItem(Attr.currentAnimal, 0, false);
32 float jumpPlayer = upgradeInfo.getItem(Attr.currentAnimal, 1, false);
33 player.setAnimalProperties(speedPlayer, jumpPlayer);
34 animalPlayer.layer = LayerMask.NameToLayer("Animal1");
35
36 animals.Add(animalPlayer);
37 animalIndex++;
38
39 animalPlayer.GetComponent
40 //animalPlayer.GetComponent
41
42 TextAsset xml = Resources.Load
43 XmlDocument xmlDoc = new XmlDocument();
44 xmlDoc.Load(new StringReader(xml.text));
45
46 XmlNodeList xmlNodeList = xmlDoc.DocumentElement.ChildNodes;
47 XmlNodeList levelNodeList = xmlNodeList.Item(Attr.currentLevel).ChildNodes;
48 for (int i = 0; i < levelNodeList.Count; i++)
49 {
50 int numberAnimal = int.Parse(levelNodeList.Item(i).Attributes.Item(0).Value);
51 string animalName = levelNodeList.Item(i).Attributes.Item(1).Value.ToLower();
52
53 for (int k = 0; k < numberAnimal; k++)
54 {
55 GameObject animalObject = (GameObject)Instantiate(Resources.Load
56 animalObject.transform.parent = gameObject.transform;
57 animalObject.transform.localPosition = new Vector3(-3.5f + 0.7f*animalIndex, -0.9f, 0);
58 setSortingLayer(animalObject, "Animal" + animalIndex);
59 Animal animal = animalObject.AddComponent
60 animal.setAnimalName(animalName.ToLower() + "_blue");
61 animal.animalIndex = animalIndex;
62 animal.gameScreen = gameScreen;
63 float speed = upgradeInfo.getSpeed(i);
64 float jump = upgradeInfo.getJump(i);
65 animal.setAnimalProperties(speed, jump);
66 animalObject.layer = LayerMask.NameToLayer("Animal" + (animalIndex + 1));
67 animals.Add(animalObject);
68 animalIndex++;
69
70 animalObject.GetComponent
71
72 //animalObject.GetComponent
73 }
74 }
75 }
File name: BitmapFont.cs
Copy
26 public BitmapFont(string pathPNG, string pathXML, GameObject gameObject)
27 {
28 this.gameObject = gameObject;
29 sprites = new Dictionary
30 yoffsets_xadvances = new Dictionary
31 rects = new Dictionary
32
33 Texture2D texture = Resources.Load
34 float height = texture.height;
35 TextAsset xml = Resources.Load
36
37 XmlDocument test = new XmlDocument();
38 test.Load(new StringReader(xml.text));
39
40 //test.LoadXml(new StringReader(xml.text).ReadToEnd());
41 //string[] keys = new string[] {"x","y","width","height","yoffset","xadvance","letter"};
42
43 foreach (XmlNode node in test.DocumentElement.ChildNodes)
44 {
45 XmlAttributeCollection collection = node.Attributes;
46 Rect rect = new Rect(float.Parse(collection.Item(0).Value), height - float.Parse(collection.Item(1).Value) - float.Parse(collection.Item(3).Value), float.Parse(collection.Item(2).Value), float.Parse(collection.Item(3).Value));
47 rects.Add(collection.Item(6).Value, rect);
48 sprites.Add(collection.Item(6).Value, Sprite.Create(texture, rect, Vector2.zero));
49 yoffsets_xadvances.Add(collection.Item(6).Value, new Vector2(float.Parse(collection.Item(4).Value), float.Parse(collection.Item(5).Value)));
50 }
51 }
File name: XmlUtils.cs
Copy
31 public static bool TryGetString(this XmlNode node, string key, out string value)
32 {
33 value = default(string);
34 var attr = node.Attributes[key];
35 bool parsed = false;
36 if (attr != null)
37 {
38 parsed = true;
39 value = attr.Value;
40 }
41
42 return parsed;
43 }
File name: XmlUtils.cs
Copy
45 public static bool TryGetInt(this XmlNode node, string key, out int value)
46 {
47 value = default(int);
48 var attr = node.Attributes[key];
49 bool parsed = false;
50 if (attr != null)
51 {
52 parsed = int.TryParse(attr.Value, System.Globalization.NumberStyles.Integer, System.Globalization.CultureInfo.InvariantCulture, out value);
53 }
54
55 return parsed;
56 }
File name: XmlUtils.cs
Copy
58 public static bool TryGetFloat(this XmlNode node, string key, out float value)
59 {
60 value = default(float);
61 var attr = node.Attributes[key];
62 bool parsed = false;
63 if (attr != null)
64 {
65 parsed = float.TryParse(attr.Value, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out value);
66 }
67
68 return parsed;
69 }
File name: XmlUtils.cs
Copy
99 public static string GetString(this XmlNode node, string key, string defaultVal)
100 {
101 string value = defaultVal;
102 var attr = node.Attributes[key];
103 if (attr != null)
104 {
105 value = attr.Value;
106 }
107 return value;
108 }
File name: ImportTiled2Unity.Mesh.cs
Copy
298 private IList299 {
300 // Report an error for ICustomTiledImporter classes that don't have the CustomTiledImporterAttribute
301 var errorTypes = from a in AppDomain.CurrentDomain.GetAssemblies()
302 from t in a.GetTypes()
303 where typeof(ICustomTiledImporter).IsAssignableFrom(t)
304 where !t.IsAbstract
305 where Attribute.GetCustomAttribute(t, typeof(CustomTiledImporterAttribute)) == null
306 select t;
307 foreach (var t in errorTypes)
308 {
309 Debug.LogError(String.Format("ICustomTiledImporter type '{0}' is missing CustomTiledImporterAttribute", t));
310 }
311
312 // Find all the types with the CustomTiledImporterAttribute, instantiate them, and give them a chance to customize our prefab
313 var types = from a in AppDomain.CurrentDomain.GetAssemblies()
314 from t in a.GetTypes()
315 where typeof(ICustomTiledImporter).IsAssignableFrom(t)
316 where !t.IsAbstract
317 from attr in Attribute.GetCustomAttributes(t, typeof(CustomTiledImporterAttribute))
318 let custom = attr as CustomTiledImporterAttribute
319 orderby custom.Order
320 select t;
321
322 var instances = types.Select(t => (ICustomTiledImporter)Activator.CreateInstance(t));
323 return instances.ToList();
324 }
File name: ImportUtils.cs
Copy
92 public static void ReadyToWrite(string path)
93 {
94 // Creates directories in path if they don't exist
95 FileInfo info = new FileInfo(path);
96 info.Directory.Create();
97
98 // Make sure file is not readonly
99 if ((info.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
100 {
101 throw new UnityException(String.Format("{0} is read-only", path));
102 }
103 }
File name: frmAbout.cs
Copy
27 {
28 get
29 {
30 object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
31 if (attributes.Length > 0)
32 {
33 AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0];
34 if (titleAttribute.Title != "")
35 {
36 return titleAttribute.Title;
37 }
38 }
39 return System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase);
40 }
41 }
File name: frmAbout.cs
Copy
52 {
53 get
54 {
55 object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
56 if (attributes.Length == 0)
57 {
58 return "";
59 }
60 return ((AssemblyDescriptionAttribute)attributes[0]).Description;
61 }
62 }
Download file with original file name:Attributes
Attributes 121 lượt xem
Gõ tìm kiếm nhanh...