World









How do I use World
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


File name: Cloud.cs Copy
31  void InitializeVariables(){
32   Vector3 topBound = Camera.main.ViewportToWorldPoint (new Vector3 (0, 1, 0));
33   Vector3 bottomBound = Camera.main.ViewportToWorldPoint (new Vector3 (0, 0, 0));
34   Vector3 temp = transform.position;
35   target = new Vector3(0, maxBottom - 20, 0);
36   maxTop = topBound.y;
37   maxBottom = bottomBound.y;
38
39   temp.y = maxTop + 11;
40
41   transform.position = temp;
42
43  }
File name: EnemySpawner.cs Copy
77  void LimitBounds(){
78   Vector3 topBoundary = Camera.main.ViewportToWorldPoint (new Vector3 (0, 1, 0));
79   maxTop = topBoundary.y;
80  }
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: PlayerController.cs Copy
77  void LimitPosition(){
78   Vector3 leftBound = Camera.main.ViewportToWorldPoint (new Vector3 (0, 0, Camera.main.transform.position.z));
79   Vector3 rightBound = Camera.main.ViewportToWorldPoint (new Vector3 (1, 0, Camera.main.transform.position.z));
80
81   maxLeft = leftBound.x;
82   maxRight = rightBound.x;
83  }
File name: Cannon.cs Copy
63  void TouchCannonShoot(){
64   if(Input.touchCount > 0){
65    Touch touch = Input.GetTouch (0);
66
67    touchPos = Camera.main.ScreenToWorldPoint (touch.position);
68
69    Vector2 touchRayHit = new Vector2 (touchPos.x, touchPos.y);
70
71    RaycastHit2D hit = Physics2D.Raycast (touchRayHit , Vector2.zero);
72
73    if(hit.collider != null){
74     if(hit.collider.CompareTag("Player")){
75      if(touch.phase == TouchPhase.Stationary){
76       if (shot != 0) {
77        UpdatePowerLevel ();
78        isCharging = true;
79       }
80      }else if(touch.phase == TouchPhase.Ended){
81       if (shot != 0) {
82        GameObject newCannonBullet = Instantiate (cannonBullet, cannonTip.position, Quaternion.identity) as GameObject;
83        newCannonBullet.transform.GetComponent ().AddForce (cannonTip.right * powerLevel.value, ForceMode2D.Impulse);
84        if (GameController.instance != null && MusicController.instance != null) {
85         if (GameController.instance.isMusicOn) {
86          audioSource.PlayOneShot (cannonShot);
87         }
88        }
89        shot--;
90        powerLevel.value = 0;
91        readyToShoot = false;
92        isCharging = false;
93       }
94      }
95     }
96    }
97
98    /*if(touch.phase == TouchPhase.Stationary){
99     if (shot != 0) {
100      UpdatePowerLevel ();
101     }
102    }else if(touch.phase == TouchPhase.Ended){
103     if (shot != 0) {
104      GameObject newCannonBullet = Instantiate (cannonBullet, cannonTip.position, Quaternion.identity) as GameObject;
105      newCannonBullet.transform.GetComponent ().AddForce (cannonTip.right * powerLevel.value, ForceMode2D.Impulse);
106      if (GameController.instance != null && MusicController.instance != null) {
107       if (GameController.instance.isMusicOn) {
108        audioSource.PlayOneShot (cannonShot);
109       }
110      }
111      shot--;
112      powerLevel.value = 0;
113      readyToShoot = false;
114     }
115    }*/
116
117   }
118  }
File name: LevelText.cs Copy
9         void Start()
10         {
11             BitmapFont fontResult = new BitmapFont("Fonts/font_result", "Fonts/font_result_xml", gameObject);
12             fontResult.setText("Level " + ((Attr.currentWorld * 15) + Attr.currentLevel + 1), -1, 15, "GUI", "GUI");
13         }
File name: PurposeLabel.cs Copy
16         void Start()
17         {
18             BitmapFont purposeFont1 = new BitmapFont("Fonts/shop_font", "Fonts/shop_font_xml", purposeLabel1);
19             BitmapFont purposeFont2 = new BitmapFont(purposeFont1, purposeLabel2);
20             BitmapFont purposeFont3 = new BitmapFont(purposeFont1, purposeLabel3);
21
22             purposeFont1.setText("5th place or better : 50 #", 0, 12, "GUI", "GUI");
23             purposeFont2.setText("3rd place or better : 100 #", 0, 12, "GUI", "GUI");
24             purposeFont3.setText("1st place : 200 #", 0, 12, "GUI", "GUI");
25
26             purposeLabel1.transform.localScale = new Vector3(0.8f, 0.8f, purposeLabel1.transform.localScale.z);
27             purposeLabel2.transform.localScale = new Vector3(0.8f, 0.8f, purposeLabel2.transform.localScale.z);
28             purposeLabel3.transform.localScale = new Vector3(0.8f, 0.8f, purposeLabel3.transform.localScale.z);
29
30             int star = Data.getData(Data.KEY_STAR + (Attr.currentWorld * 15 + Attr.currentLevel));
31             for (int i = 0; i < 3; i++)
32             {
33                 if (i < star)
34                 {
35                     stars[i].GetComponent().sprite = starSprite;
36                 }
37             }
38
39         }
File name: ResultLayer.cs Copy
48         private void saveData()
49         {
50             int starSaved = Data.getData(Data.KEY_STAR + (Attr.currentWorld * 15 + Attr.currentLevel));
51             if (star > starSaved)
52             {
53                 Data.saveData(Data.KEY_STAR + (Attr.currentWorld * 15 + Attr.currentLevel), star);
54                 Data.saveData(Data.KEY_COIN, Data.getData(Data.KEY_COIN) + m_gold);
55
56                 if (Attr.currentLevel == 14)
57                 {
58                     int worldSaved = Data.getData(Data.KEY_WORLD_MAP);
59                     if (Attr.currentWorld == worldSaved - 1)
60                     {
61                         Data.saveData(Data.KEY_WORLD_MAP, worldSaved + 1);
62                     }
63                 }
64                 else
65                 {
66                     int levelSaved = Data.getData(Data.KEY_LEVEL + Attr.currentWorld);
67                     if (Attr.currentLevel == levelSaved - 1)
68                     {
69                         Data.saveData(Data.KEY_LEVEL + Attr.currentWorld, levelSaved + 1);
70                     }
71                 }
72             }
73             else
74             {
75                 Data.saveData(Data.KEY_COIN, Data.getData(Data.KEY_COIN) + m_gold);
76             }
77         }
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("Animals/" + names[Attr.currentAnimal]));
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().Play("run");
40             //animalPlayer.GetComponent().SetBool(Animal.IS_PLAYER, true);
41
42             TextAsset xml = Resources.Load("Levels/WorldMap" + (Attr.currentWorld + 1));
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("Animals/" + animalName));
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().Play("run_blue");
71
72                     //animalObject.GetComponent().SetBool(Animal.IS_PLAYER, false);
73                 }
74             }
75         }
File name: Background.cs Copy
12         public void Start()
13         {
14             int world = Attr.currentWorld;
15
16             string[] mapnames = new string[] {"jungle","southpole","desert","volcano"};
17
18             int[] heights1 = new int[] {250, 332, 480, 218};
19             int[] heights2 = new int[] {125, 134, 304, 57};
20
21             bg0.GetComponent().setSprite(Resources.Load("Textures/Game/" + mapnames[world] + "/bg"), 0);
22             bg1.GetComponent().setSprite(Resources.Load("Textures/Game/" + mapnames[world] + "/bg1"), -240 + heights1[world]/2);
23             bg2.GetComponent().setSprite(Resources.Load("Textures/Game/" + mapnames[world] + "/bg2"), -240 + heights2[world]/2);
24         }

Download file with original file name:World

World 110 lượt xem

Gõ tìm kiếm nhanh...