AddComponent









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

Featured Snippets


File name: PhotonConverter.cs Copy
351     static int ConvertNetworkView(NetworkView[] netViews, bool isScene)
352     {
353         for (int i = netViews.Length - 1; i >= 0; i--)
354         {
355             NetworkView netView = netViews[i];
356             PhotonView view = netView.gameObject.AddComponent();
357             if (isScene)
358             {
359                 //Get scene ID
360                 string str = netView.viewID.ToString().Replace("SceneID: ", "");
361                 int firstSpace = str.IndexOf(" ");
362                 str = str.Substring(0, firstSpace);
363                 int oldViewID = int.Parse(str);
364
365                 view.viewID = oldViewID;
366                 EditorUtility.SetDirty(view);
367                 EditorUtility.SetDirty(view.gameObject);
368             }
369             view.observed = netView.observed;
370             if (netView.stateSynchronization == NetworkStateSynchronization.Unreliable)
371             {
372                 view.synchronization = ViewSynchronization.Unreliable;
373             }
374             else if (netView.stateSynchronization == NetworkStateSynchronization.ReliableDeltaCompressed)
375             {
376                 view.synchronization = ViewSynchronization.ReliableDeltaCompressed;
377             }
378             else
379             {
380                 view.synchronization = ViewSynchronization.Off;
381             }
382             DestroyImmediate(netView, true);
383         }
384         AssetDatabase.Refresh();
385         AssetDatabase.SaveAssets();
386
387         return netViews.Length;
388     }
File name: PhotonNetwork.cs Copy
974     static PhotonNetwork()
975     {
976         #if UNITY_EDITOR
977         if (!EditorApplication.isPlaying && !EditorApplication.isPlayingOrWillChangePlaymode)
978         {
979             //Debug.Log(string.Format("PhotonNetwork.ctor() Not playing {0} {1}", UnityEditor.EditorApplication.isPlaying, UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode));
980             return;
981         }
982
983         // This can happen when you recompile a script IN play made
984         // This helps to surpress some errors, but will not fix breaking
985         PhotonHandler[] photonHandlers = GameObject.FindObjectsOfType(typeof(PhotonHandler)) as PhotonHandler[];
986         if (photonHandlers != null && photonHandlers.Length > 0)
987         {
988             Debug.LogWarning("Unity recompiled. Connection gets closed and replaced. You can connect as 'new' client.");
989             foreach (PhotonHandler photonHandler in photonHandlers)
990             {
991                 //Debug.Log("Handler: " + photonHandler + " photonHandler.gameObject: " + photonHandler.gameObject);
992                 photonHandler.gameObject.hideFlags = 0;
993                 GameObject.DestroyImmediate(photonHandler.gameObject);
994                 Component.DestroyImmediate(photonHandler);
995             }
996         }
997         #endif
998
999         Application.runInBackground = true;
1000
1001         // Set up a MonoBehaviour to run Photon, and hide it
1002         GameObject photonGO = new GameObject();
1003         photonMono = (PhotonHandler)photonGO.AddComponent();
1004         photonGO.name = "PhotonMono";
1005         photonGO.hideFlags = HideFlags.HideInHierarchy;
1006
1007
1008         // Set up the NetworkingPeer and use protocol of PhotonServerSettings
1009         networkingPeer = new NetworkingPeer(photonMono, string.Empty, PhotonNetwork.PhotonServerSettings.Protocol);
1010
1011
1012         // Local player
1013         CustomTypes.Register();
1014     }
File name: ShowInfoOfPlayer.cs Copy
20     void Start()
21     {
22         if (font == null)
23         {
24             #if UNITY_3_5
25             font = (Font)FindObjectsOfTypeIncludingAssets(typeof(Font))[0];
26             #else
27             font = (Font)Resources.FindObjectsOfTypeAll(typeof(Font))[0];
28             #endif
29             Debug.LogWarning("No font defined. Found font: " + font);
30         }
31
32         if (tm == null)
33         {
34             textGo = new GameObject("3d text");
35             //textGo.transform.localScale = new Vector3(0.2f, 0.2f, 0.2f);
36             textGo.transform.parent = this.gameObject.transform;
37             textGo.transform.localPosition = Vector3.zero;
38
39             MeshRenderer mr = textGo.AddComponent();
40             mr.material = font.material;
41             tm = textGo.AddComponent();
42             tm.font = font;
43             tm.anchor = TextAnchor.MiddleCenter;
44             if (this.CharacterSize > 0)
45             {
46                 tm.characterSize = this.CharacterSize;
47             }
48         }
49     }
File name: SupportLogger.cs Copy
9     public void Start()
10     {
11         GameObject go = GameObject.Find("PunSupportLogger");
12         if (go == null)
13         {
14             go = new GameObject("PunSupportLogger");
15             DontDestroyOnLoad(go);
16             SupportLogging sl = go.AddComponent();
17             sl.LogTrafficStats = this.LogTrafficStats;
18         }
19     }
File name: Grid.cs Copy
88  void CreateGrid() {
89   Vector3 bottomLeft = new Vector3(
90     transform.position.x - size.x / 2 + tileSize.x / 2,
91     transform.position.y,
92     transform.position.z - size.z / 2 + tileSize.z / 2);
93   Vector3 startPosition = bottomLeft;
94
95   GameObject tile = tilePrefab;
96
97   for (int row = 0; row < rows; row++) {
98    for (int col = 0; col < cols; col++) {
99     startPosition.z = bottomLeft.z + tileSize.z * row;
100     startPosition.x = bottomLeft.x + tileSize.x * col;
101     GameObject go = Instantiate(tile, startPosition, tile.transform.rotation) as GameObject;
102     Node dn = go.AddComponent();
103     dn.row = row;
104     dn.col = col;
105     dn.rowChess = Converter.ToChessRow(row);
106     dn.colChess = Converter.ToChessCol(col);
107     grid[row,col] = dn;
108     go.transform.parent = transform;
109     go.transform.localScale = Vector3.zero;
110
111     dn.ScaleIn(Random.Range(0f,1f),Random.Range(1f,2f),tile.transform.localScale);
112     tile = SwapTilePrefab(tile);
113    }
114    tile = SwapTilePrefab(tile);
115   }
116
117   StartCoroutine(SpawnPieces());
118  }
File name: DialogUnity.cs Copy
74         public void showDialog()
75         {
76             gameObject.SetActive(true);
77             gameObject.transform.localPosition = new Vector3(0, 4.8f, gameObject.transform.localPosition.z);
78             if (gameObject.GetComponent() != null)
79                 Destroy(gameObject.GetComponent());
80             gameObject.AddComponent().addAction(new ActionMoveTo(0, 0, 0.5f, Interpolation.swingOut));
81         }
File name: NextGuideClickListener.cs Copy
13         public void Start()
14         {
15             gameObject.AddComponent().addAction(new ActionRepeat(ActionRepeat.FOREVER, new ActionSequence(
16                 new ActionScaleTo(0.9f, 0.9f, 0.2f, Interpolation.sine),
17                 new ActionScaleTo(1, 1, 0.2f, Interpolation.sine)
18                 )));
19             guideIndex = 0;
20         }
File name: ButtonStartClickListener.cs Copy
10         public void Start()
11         {
12             gameObject.AddComponent().addAction(new ActionRepeat(ActionRepeat.FOREVER, new ActionSequence(
13                 new ActionScaleTo(0.75f, 0.75f, 0.2f, Interpolation.sine),
14                 new ActionScaleTo(0.8f, 0.8f, 0.2f, Interpolation.sine)
15                 )));
16         }
File name: ResultLayer.cs Copy
108         private void showButtons(bool showNext)
109         {
110             if (showNext)
111             {
112                 for (int i = 0; i < 3; i++)
113                 {
114                     buttons[i].SetActive(true);
115                     buttons[i].AddComponent().addAction(new ActionScaleTo(1, 1, 0.5f, Interpolation.swingOut));
116                 }
117             }
118             else
119             {
120                 for (int i = 0; i < 2; i++)
121                 {
122                     buttons[i].SetActive(true);
123                     buttons[i].AddComponent().addAction(new ActionScaleTo(1, 1, 0.5f, Interpolation.swingOut));
124                 }
125                 buttons[2].SetActive(false);
126             }
127         }
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().sprite = starSprite;
138                 starObject.GetComponent().sortingLayerName = "GUI";
139                 starObject.transform.localScale = new Vector3(0.5f, 0.5f, starObject.transform.localScale.z);
140
141                 starObject.AddComponent().addAction(new ActionParallel(
142                     new ActionRotateBy(720, 2),
143                     new ActionScaleTo(0, 0, 1.5f)
144                     ));
145
146                 if (starIndex == 0)
147                 {
148                     starObject.AddComponent().setBezier(-1,
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().setBezier(-1,
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().setBezier(-1,
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         }

AddComponent 105 lượt xem

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