GameObjects









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

Featured Snippets


File name: DemoMecanimGUI.cs Copy
42     public void FindRemoteAnimator()
43     {
44         if( m_RemoteAnimator != null )
45         {
46             return;
47         }
48
49         // the prefab has to be tagged as Player
50         GameObject[] gos = GameObject.FindGameObjectsWithTag( "Player" );
51         for( int i = 0; i < gos.Length; ++i )
52         {
53             PhotonView view = gos[ i ].GetComponent();
54             if( view != null && view.isMine == false )
55             {
56                 m_RemoteAnimator = gos[ i ].GetComponent();
57             }
58         }
59     }
File name: NetworkingPeer.cs Copy
495     protected internal void LocalCleanupAnythingInstantiated(bool destroyInstantiatedGameObjects)
496     {
497         if (tempInstantiationData.Count > 0)
498         {
499             Debug.LogWarning("It seems some instantiation is not completed, as instantiation data is used. You should make sure instantiations are paused when calling this method. Cleaning now, despite this.");
500         }
501
502         // Destroy GO's (if we should)
503         if (destroyInstantiatedGameObjects)
504         {
505             // Fill list with Instantiated objects
506             HashSet instantiatedGos = new HashSet();
507             foreach (PhotonView view in this.photonViewList.Values)
508             {
509                 if (view.isRuntimeInstantiated)
510                 {
511                     instantiatedGos.Add(view.gameObject); // HashSet keeps each object only once
512                 }
513             }
514
515             foreach (GameObject go in instantiatedGos)
516             {
517                 this.RemoveInstantiatedGO(go, true);
518             }
519         }
520
521         // photonViewList is cleared of anything instantiated (so scene items are left inside)
522         // any other lists can be
523         this.tempInstantiationData.Clear(); // should be empty but to be safe we clear (no new list needed)
524         PhotonNetwork.lastUsedViewSubId = 0;
525         PhotonNetwork.lastUsedViewSubIdStatic = 0;
526     }
File name: NetworkingPeer.cs Copy
1937     public static void SendMonoMessage(PhotonNetworkingMessage methodString, params object[] parameters)
1938     {
1939         HashSet objectsToCall;
1940         if (PhotonNetwork.SendMonoMessageTargets != null)
1941         {
1942             objectsToCall = PhotonNetwork.SendMonoMessageTargets;
1943         }
1944         else
1945         {
1946             objectsToCall = PhotonNetwork.FindGameObjectsWithComponent(PhotonNetwork.SendMonoMessageTargetType);
1947         }
1948
1949         string methodName = methodString.ToString();
1950         object callParameter = (parameters != null && parameters.Length == 1) ? parameters[0] : parameters;
1951         foreach (GameObject gameObject in objectsToCall)
1952         {
1953             gameObject.SendMessage(methodName, callParameter, SendMessageOptions.DontRequireReceiver);
1954         }
1955     }
File name: NetworkingPeer.cs Copy
1962     public void ExecuteRPC(Hashtable rpcData, PhotonPlayer sender)
1963     {
1964         if (rpcData == null || !rpcData.ContainsKey((byte)0))
1965         {
1966             Debug.LogError("Malformed RPC; this should never occur. Content: " + SupportClass.DictionaryToString(rpcData));
1967             return;
1968         }
1969
1970         // ts: updated with "flat" event data
1971         int netViewID = (int)rpcData[(byte)0]; // LIMITS PHOTONVIEWS&PLAYERS
1972         int otherSidePrefix = 0; // by default, the prefix is 0 (and this is not being sent)
1973         if (rpcData.ContainsKey((byte)1))
1974         {
1975             otherSidePrefix = (short)rpcData[(byte)1];
1976         }
1977
1978         string inMethodName;
1979         if (rpcData.ContainsKey((byte)5))
1980         {
1981             int rpcIndex = (byte)rpcData[(byte)5]; // LIMITS RPC COUNT
1982             if (rpcIndex > PhotonNetwork.PhotonServerSettings.RpcList.Count - 1)
1983             {
1984                 Debug.LogError("Could not find RPC with index: " + rpcIndex + ". Going to ignore! Check PhotonServerSettings.RpcList");
1985                 return;
1986             }
1987             else
1988             {
1989                 inMethodName = PhotonNetwork.PhotonServerSettings.RpcList[rpcIndex];
1990             }
1991         }
1992         else
1993         {
1994             inMethodName = (string)rpcData[(byte)3];
1995         }
1996
1997         object[] inMethodParameters = null;
1998         if (rpcData.ContainsKey((byte)4))
1999         {
2000             inMethodParameters = (object[])rpcData[(byte)4];
2001         }
2002
2003         if (inMethodParameters == null)
2004         {
2005             inMethodParameters = new object[0];
2006         }
2007
2008         PhotonView photonNetview = this.GetPhotonView(netViewID);
2009         if (photonNetview == null)
2010         {
2011             int viewOwnerId = netViewID/PhotonNetwork.MAX_VIEW_IDS;
2012             bool owningPv = (viewOwnerId == this.mLocalActor.ID);
2013             bool ownerSent = (viewOwnerId == sender.ID);
2014
2015             if (owningPv)
2016             {
2017                 Debug.LogWarning("Received RPC \"" + inMethodName + "\" for viewID " + netViewID + " but this PhotonView does not exist! View was/is ours." + (ownerSent ? " Owner called." : " Remote called.") + " By: " + sender.ID);
2018             }
2019             else
2020             {
2021                 Debug.LogWarning("Received RPC \"" + inMethodName + "\" for viewID " + netViewID + " but this PhotonView does not exist! Was remote PV." + (ownerSent ? " Owner called." : " Remote called.") + " By: " + sender.ID + " Maybe GO was destroyed but RPC not cleaned up.");
2022             }
2023             return;
2024         }
2025
2026         if (photonNetview.prefix != otherSidePrefix)
2027         {
2028             Debug.LogError(
2029                 "Received RPC \"" + inMethodName + "\" on viewID " + netViewID + " with a prefix of " + otherSidePrefix
2030                 + ", our prefix is " + photonNetview.prefix + ". The RPC has been ignored.");
2031             return;
2032         }
2033
2034         // Get method name
2035         if (inMethodName == string.Empty)
2036         {
2037             Debug.LogError("Malformed RPC; this should never occur. Content: " + SupportClass.DictionaryToString(rpcData));
2038             return;
2039         }
2040
2041         if (PhotonNetwork.logLevel >= PhotonLogLevel.Full)
2042             Debug.Log("Received RPC: " + inMethodName);
2043
2044
2045         // SetReceiving filtering
2046         if (photonNetview.group != 0 && !allowedReceivingGroups.Contains(photonNetview.group))
2047         {
2048             return; // Ignore group
2049         }
2050
2051         Type[] argTypes = new Type[0];
2052         if (inMethodParameters.Length > 0)
2053         {
2054             argTypes = new Type[inMethodParameters.Length];
2055             int i = 0;
2056             for (int index = 0; index < inMethodParameters.Length; index++)
2057             {
2058                 object objX = inMethodParameters[index];
2059                 if (objX == null)
2060                 {
2061                     argTypes[i] = null;
2062                 }
2063                 else
2064                 {
2065                     argTypes[i] = objX.GetType();
2066                 }
2067
2068                 i++;
2069             }
2070         }
2071
2072         int receivers = 0;
2073         int foundMethods = 0;
2074         MonoBehaviour[] mbComponents = photonNetview.GetComponents(); // NOTE: we could possibly also cache MonoBehaviours per view?!
2075         for (int componentsIndex = 0; componentsIndex < mbComponents.Length; componentsIndex++)
2076         {
2077             MonoBehaviour monob = mbComponents[componentsIndex];
2078             if (monob == null)
2079             {
2080                 Debug.LogError("ERROR You have missing MonoBehaviours on your gameobjects!");
2081                 continue;
2082             }
2083
2084             Type type = monob.GetType();
2085
2086             // Get [RPC] methods from cache
2087             List cachedRPCMethods = null;
2088             if (this.monoRPCMethodsCache.ContainsKey(type))
2089             {
2090                 cachedRPCMethods = this.monoRPCMethodsCache[type];
2091             }
2092
2093             if (cachedRPCMethods == null)
2094             {
2095                 List entries = SupportClass.GetMethods(type, typeof(RPC));
2096
2097                 this.monoRPCMethodsCache[type] = entries;
2098                 cachedRPCMethods = entries;
2099             }
2100
2101             if (cachedRPCMethods == null)
2102             {
2103                 continue;
2104             }
2105
2106             // Check cache for valid methodname+arguments
2107             for (int index = 0; index < cachedRPCMethods.Count; index++)
2108             {
2109                 MethodInfo mInfo = cachedRPCMethods[index];
2110                 if (mInfo.Name == inMethodName)
2111                 {
2112                     foundMethods++;
2113                     ParameterInfo[] pArray = mInfo.GetParameters();
2114                     if (pArray.Length == argTypes.Length)
2115                     {
2116                         // Normal, PhotonNetworkMessage left out
2117                         if (this.CheckTypeMatch(pArray, argTypes))
2118                         {
2119                             receivers++;
2120                             object result = mInfo.Invoke((object)monob, inMethodParameters);
2121                             if (mInfo.ReturnType == typeof(IEnumerator))
2122                             {
2123                                 monob.StartCoroutine((IEnumerator)result);
2124                             }
2125                         }
2126                     }
2127                     else if ((pArray.Length - 1) == argTypes.Length)
2128                     {
2129                         // Check for PhotonNetworkMessage being the last
2130                         if (this.CheckTypeMatch(pArray, argTypes))
2131                         {
2132                             if (pArray[pArray.Length - 1].ParameterType == typeof(PhotonMessageInfo))
2133                             {
2134                                 receivers++;
2135
2136                                 int sendTime = (int)rpcData[(byte)2];
2137                                 object[] deParamsWithInfo = new object[inMethodParameters.Length + 1];
2138                                 inMethodParameters.CopyTo(deParamsWithInfo, 0);
2139                                 deParamsWithInfo[deParamsWithInfo.Length - 1] = new PhotonMessageInfo(sender, sendTime, photonNetview);
2140
2141                                 object result = mInfo.Invoke((object)monob, deParamsWithInfo);
2142                                 if (mInfo.ReturnType == typeof(IEnumerator))
2143                                 {
2144                                     monob.StartCoroutine((IEnumerator)result);
2145                                 }
2146                             }
2147                         }
2148                     }
2149                     else if (pArray.Length == 1 && pArray[0].ParameterType.IsArray)
2150                     {
2151                         receivers++;
2152                         object result = mInfo.Invoke((object)monob, new object[] { inMethodParameters });
2153                         if (mInfo.ReturnType == typeof(IEnumerator))
2154                         {
2155                             monob.StartCoroutine((IEnumerator)result);
2156                         }
2157                     }
2158                 }
2159             }
2160         }
2161
2162         // Error handling
2163         if (receivers != 1)
2164         {
2165             string argsString = string.Empty;
2166             for (int index = 0; index < argTypes.Length; index++)
2167             {
2168                 Type ty = argTypes[index];
2169                 if (argsString != string.Empty)
2170                 {
2171                     argsString += ", ";
2172                 }
2173
2174                 if (ty == null)
2175                 {
2176                     argsString += "null";
2177                 }
2178                 else
2179                 {
2180                     argsString += ty.Name;
2181                 }
2182             }
2183
2184             if (receivers == 0)
2185             {
2186                 if (foundMethods == 0)
2187                 {
2188                     Debug.LogError("PhotonView with ID " + netViewID + " has no method \"" + inMethodName + "\" marked with the [RPC](C#) or @RPC(JS) property! Args: " + argsString);
2189                 }
2190                 else
2191                 {
2192                     Debug.LogError("PhotonView with ID " + netViewID + " has no method \"" + inMethodName + "\" that takes " + argTypes.Length + " argument(s): " + argsString);
2193                 }
2194             }
2195             else
2196             {
2197                 Debug.LogError("PhotonView with ID " + netViewID + " has " + receivers + " methods \"" + inMethodName + "\" that takes " + argTypes.Length + " argument(s): " + argsString + ". Should be just one?");
2198             }
2199         }
2200     }
File name: NetworkingPeer.cs Copy
2430     public void DestroyPlayerObjects(int playerId, bool localOnly)
2431     {
2432         if (playerId <= 0)
2433         {
2434             Debug.LogError("Failed to Destroy objects of playerId: " + playerId);
2435             return;
2436         }
2437
2438         if (!localOnly)
2439         {
2440             // clean server's Instantiate and RPC buffers
2441             this.OpRemoveFromServerInstantiationsOfPlayer(playerId);
2442             this.OpCleanRpcBuffer(playerId);
2443
2444             // send Destroy(player) to anyone else
2445             this.SendDestroyOfPlayer(playerId);
2446         }
2447
2448         // locally cleaning up that player's objects
2449         HashSet playersGameObjects = new HashSet();
2450         foreach (PhotonView view in this.photonViewList.Values)
2451         {
2452             if (view.CreatorActorNr == playerId)
2453             {
2454                 playersGameObjects.Add(view.gameObject);
2455             }
2456         }
2457
2458         // any non-local work is already done, so with the list of that player's objects, we can clean up (locally only)
2459         foreach (GameObject gameObject in playersGameObjects)
2460         {
2461             this.RemoveInstantiatedGO(gameObject, true);
2462         }
2463
2464         // with ownership transfer, some objects might lose their owner.
2465         // in that case, the creator becomes the owner again. every client can apply this. done below.
2466         foreach (PhotonView view in this.photonViewList.Values)
2467         {
2468             if (view.ownerId == playerId)
2469             {
2470                 view.ownerId = view.CreatorActorNr;
2471                 //Debug.Log("Creator is: " + view.ownerId);
2472             }
2473         }
2474     }
File name: PhotonNetwork.cs Copy
473     public static void CacheSendMonoMessageTargets(Type type)
474     {
475         if (type == null) type = SendMonoMessageTargetType;
476         PhotonNetwork.SendMonoMessageTargets = FindGameObjectsWithComponent(type);
477     }
File name: PhotonNetwork.cs Copy
479     public static HashSet FindGameObjectsWithComponent(Type type)
480     {
481         HashSet objectsWithComponent = new HashSet();
482
483         Component[] targetComponents = (Component[]) GameObject.FindObjectsOfType(type);
484         for (int index = 0; index < targetComponents.Length; index++)
485         {
486             objectsWithComponent.Add(targetComponents[index].gameObject);
487         }
488
489         return objectsWithComponent;
490     }
File name: PhotonNetwork.cs Copy
2366     public static void DestroyPlayerObjects(int targetPlayerId)
2367     {
2368         if (!VerifyCanUseNetwork())
2369         {
2370             return;
2371         }
2372         if (player.isMasterClient || targetPlayerId == player.ID)
2373         {
2374             networkingPeer.DestroyPlayerObjects(targetPlayerId, false);
2375         }
2376         else
2377         {
2378             Debug.LogError("DestroyPlayerObjects() failed, cause players can only destroy their own GameObjects. A Master Client can destroy anyone's. This is master: " + PhotonNetwork.isMasterClient);
2379         }
2380     }
File name: GameplayController.cs Copy
125  void ClearAllEnemies(){
126   GameObject[] enemies = GameObject.FindGameObjectsWithTag ("Enemy");
127   GameObject[] bossbullets = GameObject.FindGameObjectsWithTag ("Boss Bullet");
128   GameObject[] coins = GameObject.FindGameObjectsWithTag ("Coin");
129   GameObject boss = GameObject.FindGameObjectWithTag ("Boss");
130
131   if(boss != null){
132    Destroy (boss);
133   }
134
135   if(enemies != null){
136    foreach (GameObject enemy in enemies) {
137     Destroy (enemy);
138    }
139   }
140
141   if(bossbullets != null){
142    foreach (GameObject bossBullet in bossbullets) {
143     Destroy (bossBullet);
144    }
145   }
146
147   if(coins != null){
148    foreach (GameObject coin in coins) {
149     Destroy (coin);
150    }
151   }
152
153  }
File name: GameplayController.cs Copy
70  void InitializeVariables(){
71   gameInProgress = true;
72   enemies = new List (GameObject.FindGameObjectsWithTag ("Enemy"));
73   objects = new List (GameObject.FindGameObjectsWithTag ("Object"));
74   distance = 10f;
75   if(GameController.instance != null){
76    GameController.instance.score = 0;
77    prevLevel = GameController.instance.currentLevel;
78    highscore.transform.GetChild (0).transform.GetComponent ().text = GameController.instance.highscore [GameController.instance.currentLevel - 1].ToString ("N0");
79
80    if(GameController.instance.highscore[GameController.instance.currentLevel - 1] > 0){
81     highscore.gameObject.SetActive (true);
82    }
83
84   }
85
86  }

GameObjects 225 lượt xem

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