GetActive









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

Featured Snippets


File name: Extensions.cs Copy
191     public static bool GetActive(this GameObject target)
192     {
193         #if UNITY_3_5
194         return target.active;
195         #else
196         return target.activeInHierarchy;
197         #endif
198     }
File name: PickupItem.cs Copy
124     public void PunPickup(PhotonMessageInfo msgInfo)
125     {
126         // when this client's RPC gets executed, this client no longer waits for a sent pickup and can try again
127         if (msgInfo.sender.isLocal) this.SentPickup = false;
128
129
130         // In this solution, picked up items are disabled. They can't be picked up again this way, etc.
131         // You could check "active" first, if you're not interested in failed pickup-attempts.
132         if (!this.gameObject.GetActive())
133         {
134             // optional logging:
135             Debug.Log("Ignored PU RPC, cause item is inactive. " + this.gameObject + " SecondsBeforeRespawn: " + SecondsBeforeRespawn + " TimeOfRespawn: " + this.TimeOfRespawn + " respawn in future: " + (TimeOfRespawn > PhotonNetwork.time));
136             return; // makes this RPC being ignored
137         }
138
139
140         // if the RPC isn't ignored by now, this is a successful pickup. this might be "my" pickup and we should do a callback
141         this.PickupIsMine = msgInfo.sender.isLocal;
142
143         // call the method OnPickedUp(PickupItem item) if a GameObject was defined as callback target
144         if (this.OnPickedUpCall != null)
145         {
146             // you could also skip callbacks for items that are not picked up by this client by using: if (this.PickupIsMine)
147             this.OnPickedUpCall.SendMessage("OnPickedUp", this);
148         }
149
150
151         // setup a respawn (or none, if the item has to be dropped)
152         if (SecondsBeforeRespawn <= 0)
153         {
154             this.PickedUp(0.0f); // item doesn't auto-respawn. must be dropped
155         }
156         else
157         {
158             // how long it is until this item respanws, depends on the pickup time and the respawn time
159             double timeSinceRpcCall = (PhotonNetwork.time - msgInfo.timestamp);
160             double timeUntilRespawn = SecondsBeforeRespawn - timeSinceRpcCall;
161
162             //Debug.Log("msg timestamp: " + msgInfo.timestamp + " time until respawn: " + timeUntilRespawn);
163
164             if (timeUntilRespawn > 0)
165             {
166                 this.PickedUp((float)timeUntilRespawn);
167             }
168         }
169     }
File name: PickupItemSimple.cs Copy
40     public void PunPickupSimple(PhotonMessageInfo msgInfo)
41     {
42         // one of the messages might be ours
43         // note: you could check "active" first, if you're not interested in your own, failed pickup-attempts.
44         if (this.SentPickup && msgInfo.sender.isLocal)
45         {
46             if (this.gameObject.GetActive())
47             {
48                 // picked up! yay.
49             }
50             else
51             {
52                 // pickup failed. too late (compared to others)
53             }
54         }
55
56         this.SentPickup = false;
57
58         if (!this.gameObject.GetActive())
59         {
60             Debug.Log("Ignored PU RPC, cause item is inactive. " + this.gameObject);
61             return;
62         }
63
64
65         // how long it is until this item respanws, depends on the pickup time and the respawn time
66         double timeSinceRpcCall = (PhotonNetwork.time - msgInfo.timestamp);
67         float timeUntilRespawn = SecondsBeforeRespawn - (float)timeSinceRpcCall;
68         //Debug.Log("msg timestamp: " + msgInfo.timestamp + " time until respawn: " + timeUntilRespawn);
69
70         if (timeUntilRespawn > 0)
71         {
72             // this script simply disables the GO for a while until it respawns.
73             this.gameObject.SetActive(false);
74             Invoke("RespawnAfter", timeUntilRespawn);
75         }
76     }
File name: ButtonManager.cs Copy
16  public void ReloadScene() {
17
18   SceneManager.LoadScene(SceneManager.GetActiveScene().name);
19  }
File name: MainMenuController.cs Copy
32  public void StartButton(){
33   SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
34  }
File name: UIgame.cs Copy
166     public void Retry_btn()
167     {
168         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
169     }
File name: GameplayController.cs Copy
242  public void RestartGame(){
243   SceneManager.LoadScene (SceneManager.GetActiveScene().buildIndex);
244   if(GameController.instance != null){
245    GameController.instance.currentLevel = prevLevel;
246   }
247  }
File name: LevelController.cs Copy
17  void Update () {
18   if(Input.GetKeyDown("Escape")){
19    SceneManager.LoadScene (SceneManager.GetActiveScene ().buildIndex - 1);
20   }
21  }
File name: Player.cs Copy
223   private void Restart ()
224   {
225    //Load the last scene loaded, in this case Main, the only scene in the game. And we load it in "Single" mode so it replace the existing one
226             //and not load all the scene object in the current scene.
227             SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex, LoadSceneMode.Single);
228   }

GetActive 124 lượt xem

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