NameToLayer
How do I use Name To Layer
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: JumpAndRunMovement.cs
Copy
94 void UpdateIsGrounded()
95 {
96 Vector2 position = new Vector2( transform.position.x, transform.position.y );
97
98 RaycastHit2D hit = Physics2D.Raycast( position, -Vector2.up, 0.1f, 1 << LayerMask.NameToLayer( "Ground" ) );
99
100 m_IsGrounded = hit.collider != null;
101 m_Animator.SetBool( "IsGrounded", m_IsGrounded );
102 }
File name: DialogUnity.cs
Copy
26 public void setText(string oneline)
27 {
28 bitmapFont = new BitmapFont("Fonts/shop_font", "Fonts/shop_font_xml", label1);
29 bitmapFont.setText(oneline, 0, 15);
30 Transform[] fontTransforms = label1.GetComponentsInChildren
31 for (int i = 0; i < fontTransforms.Length; i++)
32 {
33 if (fontTransforms[i].gameObject.GetComponent
34 {
35 fontTransforms[i].gameObject.layer = LayerMask.NameToLayer("GUI");
36 fontTransforms[i].gameObject.GetComponent
37 }
38 }
39 }
File name: DialogUnity.cs
Copy
41 public void setText(string line1, string line2)
42 {
43 bitmapFont = new BitmapFont("Fonts/shop_font", "Fonts/shop_font_xml", label1);
44 bitmapFont.setText(line1, 0, 15);
45 Transform[] fontTransforms = label1.GetComponentsInChildren
46 for (int i = 0; i < fontTransforms.Length; i++)
47 {
48
49 if (fontTransforms[i].gameObject.GetComponent
50 {
51 fontTransforms[i].gameObject.layer = LayerMask.NameToLayer("GUI");
52 fontTransforms[i].gameObject.GetComponent
53 }
54 }
55
56 BitmapFont bitmapFont2 = new BitmapFont(bitmapFont, label2);
57 bitmapFont2.setText(line2, 0, 15);
58 Transform[] fontTransforms2 = label2.GetComponentsInChildren
59 for (int i = 0; i < fontTransforms2.Length; i++)
60 {
61 if (fontTransforms2[i].gameObject.GetComponent
62 {
63 fontTransforms2[i].gameObject.layer = LayerMask.NameToLayer("GUI");
64 fontTransforms2[i].gameObject.GetComponent
65 }
66 }
67 }
File name: ResultLayer.cs
Copy
129 private void createStars(Vector3 position, int starIndex)
130 {
131 for (int i = 0; i < 20; i++)
132 {
133 GameObject starObject = new GameObject("StarFly");
134 starObject.layer = LayerMask.NameToLayer("GUI");
135 starObject.transform.parent = gameObject.transform;
136 starObject.transform.localPosition = new Vector3(position.x, position.y, -3);
137 starObject.AddComponent
138 starObject.GetComponent
139 starObject.transform.localScale = new Vector3(0.5f, 0.5f, starObject.transform.localScale.z);
140
141 starObject.AddComponent
142 new ActionRotateBy(720, 2),
143 new ActionScaleTo(0, 0, 1.5f)
144 ));
145
146 if (starIndex == 0)
147 {
148 starObject.AddComponent
149 new Vector2(position.x, position.y),
150 new Vector2(position.x, Random.Range(1f, 2f)),
151 new Vector2(position.x - Random.Range(2f, 3f), Random.Range(1f, 2f)),
152 new Vector2(position.x - Random.Range(2f, 5.5f), Random.Range(-2f, -1f))
153 );
154 }
155 else if (starIndex == 1)
156 {
157 starObject.AddComponent
158 new Vector2(position.x, position.y),
159 new Vector2(position.x, Random.Range(1f, 2f)),
160 new Vector2(Random.Range(-1f, 1f), Random.Range(1f, 2f)),
161 new Vector2(Random.Range(-2f, 2f), Random.Range(-4f, -2f))
162 );
163 }
164 else if (starIndex == 2)
165 {
166 starObject.AddComponent
167 new Vector2(position.x, position.y),
168 new Vector2(position.x, Random.Range(1f, 2f)),
169 new Vector2(position.x + Random.Range(2f, 3f), Random.Range(1f, 2f)),
170 new Vector2(position.x + Random.Range(2f, 5.5f), Random.Range(-4f, -2f))
171 );
172 }
173 Destroy(starObject, 2);
174 }
175 }
File name: Animal.cs
Copy
82 private void createShadow()
83 {
84 if (animalIndex != 0) return;
85 shadowCreateTime += Time.deltaTime;
86 if (shadowCreateTime >= 0.05f)
87 {
88 shadowCreateTime = 0;
89 GameObject shadowObject = new GameObject("Shadow");
90 shadowObject.transform.parent = gameScreen.shadowLayer.transform;
91 shadowObject.layer = LayerMask.NameToLayer("AnimalStand");
92 shadowObject.transform.localPosition = transform.localPosition + new Vector3(0, 0.3f, 0);
93 shadowObject.AddComponent
94 shadowObject.GetComponent
95 shadowObject.GetComponent
96 shadowObject.AddComponent
97 new ActionColorTo(0, 0, 0, 0, 0.5f),
98 new ActionRunnable(delegate ()
99 {
100 Destroy(shadowObject);
101 })
102 ));
103 }
104 }
File name: Animal.cs
Copy
453 public void setStand(bool isStand, float standTime, string state_key)
454 {
455 this.isStanding = isStand;
456 this.standTime = standTime;
457 changeToState(state_key);
458 if (isStand)
459 {
460 gameObject.layer = LayerMask.NameToLayer("AnimalStand");
461 //gameObject.GetComponent
462 }
463 else
464 {
465 gameObject.layer = LayerMask.NameToLayer("Animal" + (animalIndex + 1));
466 //gameObject.GetComponent
467 }
468 }
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: GameScreen.cs
Copy
62 public void Start()
63 {
64 InputController.Name = InputNames.DIALOG;
65
66 string[] mapNames = new string[] {"jungle","southpole","desert","volcano"};
67 int[] lvs = new int[] { 1, 2, 3, 2, 3, 4, 3, 4, 5, 1, 2, 4, 3, 4, 5 };
68
69 Debug.Log("WorldIndex : " + Attr.currentWorld + ", LevelIndex : " + Attr.currentLevel);
70
71 mapObject = (GameObject)Instantiate(Resources.Load("Maps/" + mapNames[Attr.currentWorld] + lvs[Attr.currentLevel]), new Vector3(-4, 2.5f, 0), Quaternion.identity);
72 mapObject.transform.localScale = new Vector3(0.01f, 0.01f, mapObject.transform.localScale.z);
73
74 revivalPositions = new List
75
76 getSkillChoosed();
77
78 setupMap(mapObject);
79
80 int mapLayer = LayerMask.NameToLayer("Map");
81 mapObject.layer = mapLayer;
82
83 Transform[] transforms = mapObject.GetComponentsInChildren
84 for (int i = 0; i < transforms.Length; i++)
85 {
86 transforms[i].gameObject.layer = mapLayer;
87 }
88
89 shopFont = new BitmapFont("Fonts/shop_font", "Fonts/shop_font_xml", null);
90
91 BitmapFont comboFont = new BitmapFont("Fonts/font_combo", "Fonts/font_combo_xml", null);
92 combo1.setFont(comboFont);
93 combo2.setFont(comboFont);
94
95 createDialog();
96
97 m_gold = 0;
98 m_time = 0;
99 m_score = 0;
100
101 perSkills = new int[] { 30, 30, 35, 35, 40, 40, 45, 45, 50, 55, 60, 60, 65, 65, 70 };
102
103 isRunning = true;
104 //prepareGame();//khong goi o day vi co the chua chay het cac ham Start
105 isPrepare = true;
106 }
File name: GameScreen.cs
Copy
460 public void addScore(int score, Vector3 position)
461 {
462 if (score == 5)
463 {
464 GameObject addScore = new GameObject("Score");
465 addScore.transform.parent = scoreLayer.transform;
466 addScore.transform.localPosition = new Vector3(position.x, position.y, -1);
467 addScore.AddComponent
468 addScore.layer = LayerMask.NameToLayer("Map");
469 addScore.GetComponent
470 addScore.AddComponent
471 Destroy(addScore, 1.5f);
472 }
473 else if (score == 10)
474 {
475 GameObject addScore = new GameObject("Score");
476 addScore.transform.parent = scoreLayer.transform;
477 addScore.transform.localPosition = new Vector3(position.x, position.y, -1);
478 addScore.AddComponent
479 addScore.layer = LayerMask.NameToLayer("Map");
480 addScore.GetComponent
481 addScore.AddComponent
482 new ActionMoveTo(position.x + 2, position.y + 1.5f, 1.5f),
483 new ActionRunnable(delegate() {
484 Destroy(addScore);
485 })
486 ));
487 // Destroy(addScore, 1.5f);
488 }
489 m_score += score;
490 }
File name: GameScreen.cs
Copy
492 public void eatSkillRandom(SkillType type, Vector3 position, int animalIndex)
493 {
494 if (animalIndex == 0)
495 {
496 if (useSkills)
497 {
498 GameObject skillRand = new GameObject("SkilRand");
499 skillRand.transform.parent = scoreLayer.transform;
500 skillRand.transform.localPosition = new Vector3(position.x, position.y, -1);
501 skillRand.AddComponent
502 skillRand.layer = LayerMask.NameToLayer("Map");
503 skillRand.GetComponent
504 skillRand.AddComponent
505 new ActionMoveTo(position.x + 3, position.y + 1.5f, 1.5f),
506 new ActionRunnable(delegate() {
507 Destroy(skillRand);
508 })
509 ));
510 skillRand.transform.localScale = new Vector3(0.5f, 0.5f, skillRand.transform.localScale.z);
511 //Destroy(skillRand, 1.5f);
512
513 buttonSkills.eateSkill(type);
514
515 addScore(5, position);
516 }
517 }
518 else
519 {
520 int ra = Random.Range(1, 100);
521 if(ra <= perSkills[Attr.currentLevel])
522 addSkill(type, animalIndex);
523 }
524 }
NameToLayer 154 lượt xem
Gõ tìm kiếm nhanh...